Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • exact, loopable area shading of line graph between pairs of observations

    Dear list members,

    I have a question on the placement of shaded areas in graphs. I had a look at SJ articles and topics, but haven't been able to solve the problem.

    I have many graphs of the following type, generated via loops from a panel of cluster-years with binary indicators for notable years, which I use to create shaded areas with bars (extract from loop):

    Code:
    tw bar curfew_any year , yaxis(2) ylabel(none, ax(2)) yscale(ax(2) alt lcolor(bg) lwidth(none)) plotregion(margin(zero)) col(gs14) ///
    || line cdr year , sort yaxis(1) yscale(ax(1) alt) by(district, iy ix)
    The problem with this solution is that, while I do want to show the time trend in the levels of the plotted variable -hence the line graphs- I would really like to shade the changes between the levels -not the levels themselves. In other words, I would like the shaded areas to be all shifted leftwards to begin on the first data point they encounter.

    Given that the bar approach shades an area horizontally centered on the data point, and I know of no ways to change that, I guess the only way to shade areas between pairs of dates is to abandon the bar approach in favor of the scatteri, recast(area) approach. Is it? My feeling is that, still working with bars, subdividing the timevar and plotting observations selectively (still only one per year for the line graph) can alleviate but not entirely solve the problem - at least without creating something like a cluster-days panel, which I would like to avoid. I would be very happy to be proved wrong on this, because the bar approach demands literally no change to my data structure - indeed, not even the dummy variable had to be created for purpose. On the other hand, I don't see any way to automatize the production of those graphs using the scatteri recast(area) approach to shading, given that there are many such graphs, and that the shading in them will have to be specific, with multiple intervals of varying length - which I guess would again require substantially altering the panel, perhaps in a way that actually prevents generating these line graphs. So I guess the question is: is there a way to do what I want without substantially altering my data structure?

    Many thanks in advance
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	101.0 KB
ID:	1590192

    Last edited by Matteo Pinna Pintor; 19 Jan 2021, 05:54.
    I'm using StataNow/MP 18.5

  • #2
    I cannot follow what you want. I would suggest that you use one of Stata's datasets to better explain the issue and what is wanted.

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      I cannot follow what you want. I would suggest that you use one of Stata's datasets to better explain the issue and what is wanted.
      Forgive me, but I can't see how a Stata example can help further clarify what I want - considering that indeed what I want is of course something I can't produce an example of. What I want is:

      ... while I do want to show the time trend in the levels of the plotted variable -hence the line graphs- I would really like to shade the changes between the levels -not the levels themselves. In other words, I would like the shaded areas to be all shifted leftwards to begin on the first data point they encounter.
      Let me try to reformulate once more. In my graph, as you can see, the shaded areas are centered on (the x coordinate of) the plotted observations. I would like these areas to lie in between the plotted observations instead, so that what is shaded is the change between one year and the previous one, the segment connecting the plotted observations.

      [edit] consider the fifth graph, reading from top-left to bottom-right. The shaded area corresponds to year 2016, and the shading covers the local peak of the line graph. I would like the shading to cover not the peak, with some shading on both sides, but rather to cover fully the increasing side of the peak, the segment that connects the observations 2015 to 2016.
      Last edited by Matteo Pinna Pintor; 19 Jan 2021, 07:40.
      I'm using StataNow/MP 18.5

      Comment


      • #4
        Forgive me, but I can't see how a Stata example can help further clarify what I want - considering that indeed what I want is of course something I can't produce an example of. What I want is:
        From the FAQ's (which you are prompted to read before posting)

        12. What should I say about the commands and data I use?


        Help us to help you by producing self-contained questions with reproducible examples that explain your data, your code, and your problem. This helps yet others too, as they will find it easier to learn from your questions and the answers to them.

        The problem with your description in #1 is, suppose that someone wants to give you a suggestion. Then the onus is on them to create an example that matches your problem first, something that most people will be unlikely to do. I do not mean that you provide an example of what you want, but an example of your graphs with an explanation of what is wanted (in other words, a reproducible example).


        Let me try to reformulate once more. In my graph, as you can see, the shaded areas are centered on (the x coordinate of) the plotted observations. I would like these areas to lie in between the plotted observations instead, so that what is shaded is the change between one year and the previous one, the segment connecting the plotted observations

        So for example, you want the area 2014- 2015 to be shaded in your figures?

        Comment


        • #5
          Originally posted by Andrew Musau View Post
          The problem with your description in #1 is, suppose that someone wants to give you a suggestion. Then the onus is on them to create an example that matches your problem first, something that most people will be unlikely to do. I do not mean that you provide an example of what you want, but an example of your graphs with an explanation of what is wanted (in other words, a reproducible example)
          Well, I don't really need a fully worked out solution, pointers would be enough, but a working example might help, sure. I'll provide later.

          So for example, you want the area 2014- 2015 to be shaded in your figures?
          Yes, areas such as 2014-2015, 2015-2016, 2015-2017, intervals between years. As it is now, the shaded rectangles are horizontally centered on the years coded by the indicator variable curfew_any [0,1]. For each of them, this results in the shading of an area that covers part of the segment that connects the coded year (say 2016, to refer again to the fifth graph in my image) with the previous one (2015), and part of the segment that connects the coded year with the following one (2017). This is bad, because, again in the graph I take as example, I want the shading to emphasize the increase between 2015 and 2016, not the higher level of 2016 (which entails shading partly the later decrease towards the 2017 value). I would like the shaded areas to lie between two data points, not centered on one data point. And I would like a method that is easy to automatize and implementable in a panel such as mine.

          [edit] I forgot to mention I'm using Stata 16.1, and that the SJ article alluded to is this one
          Last edited by Matteo Pinna Pintor; 19 Jan 2021, 08:32.
          I'm using StataNow/MP 18.5

          Comment


          • #6
            Here is a reproducible example, but bear in mind the FAQ Advice for you future posts. As years are in units of 1, you can define a second time variable that takes into account Stata's centering of bars.

            Code:
            webuse grunfeld, clear
            set seed 01192020
            keep if company==1
            gen curfew= int(rnormal(0.5, 0.6))>=1
            list, sep(0)
            gen year2= year-0.5
            tw bar curfew year2 , yaxis(2) ylabel(none, ax(2)) ///
            yscale(ax(2) alt lcolor(bg) lwidth(none)) ///
            plotregion(margin(zero)) col(gs14) || line invest year, ///
            xlab(1934(1)1955, labsize(tiny)) xtitle("")
            Res.:

            Code:
            . list, sep(0)
            
                 +-----------------------------------------------------------+
                 | company   year   invest   mvalue   kstock   time   curfew |
                 |-----------------------------------------------------------|
              1. |       1   1935    317.6   3078.5      2.8      1        1 |
              2. |       1   1936    391.8   4661.7     52.6      2        1 |
              3. |       1   1937    410.6   5387.1    156.9      3        1 |
              4. |       1   1938    257.7   2792.2    209.2      4        1 |
              5. |       1   1939    330.8   4313.2    203.4      5        0 |
              6. |       1   1940    461.2   4643.9    207.2      6        0 |
              7. |       1   1941      512   4551.2    255.2      7        0 |
              8. |       1   1942      448   3244.1    303.7      8        0 |
              9. |       1   1943    499.6   4053.7    264.1      9        0 |
             10. |       1   1944    547.5   4379.3    201.6     10        1 |
             11. |       1   1945    561.2   4840.9      265     11        1 |
             12. |       1   1946    688.1   4900.9    402.2     12        0 |
             13. |       1   1947    568.9   3526.5    761.5     13        0 |
             14. |       1   1948    529.2   3254.7    922.4     14        1 |
             15. |       1   1949    555.1   3700.2   1020.1     15        0 |
             16. |       1   1950    642.9   3755.6     1099     16        0 |
             17. |       1   1951    755.9     4833   1207.7     17        1 |
             18. |       1   1952    891.2   4924.9   1430.5     18        0 |
             19. |       1   1953   1304.4   6241.7   1777.3     19        0 |
             20. |       1   1954   1486.7   5593.6   2226.3     20        0 |
                 +-----------------------------------------------------------+
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	72.9 KB
ID:	1590241

            Last edited by Andrew Musau; 19 Jan 2021, 08:59.

            Comment


            • #7
              Oh wow, yes, that simple indeed. I was needlessly committed to timedate-formatted x axes. Thanks a lot.
              I'm using StataNow/MP 18.5

              Comment

              Working...
              X