Announcement

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

  • Shading in Graphs

    I am trying to plot a single time series on a graph, with dates on the x axis. I would like to shade the part in between two dates to indicate a period of time. How may I do this?

    For example, I plot using the command:

    twoway line series1 date, xline(500 600)

    How may I shade the part between points 500 and 600?

  • #2
    Take a look at this thread:
    https://www.statalist.org/forums/for...vertical-lines

    Comment


    • #3
      Thanks. But unfortunately, the other thread does not provide a flexible solution for shading when the numbers on my x-axis may vary. Is there a way to directly shade between 2 data points?

      Comment


      • #4
        You will need to incorporate their solution to your problem. You have the option of using macros to set your range based on your criteria. As far as I know there isn’t a simple option for this.

        Comment


        • #5
          See also

          Blog . . . . . . . . . . . . Adding recession shading to time-series graphs
          . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D. Schenck
          2/20 http://blog.stata.com/2020/02/13/add...ssion-shading-
          to-time-series-graphs/


          SJ-16-3 gr0067 . Speaking Stata: Shading zones on time series and other plots
          . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
          Q3/16 SJ 16(3):805--812 (no commands)
          discusses background shading of time series and other plots
          https://www.stata-journal.com/articl...article=gr0067

          Comment


          • #6
            I am trying to indicate/shade recession periods on my time series graph. I have tried the two suggestions Nick mentioned above, but I can't seem to figure it out.

            Here is my code:

            local linecall GDP_PERCT_CHG CFNAI_INDEX date if inrange(date, 1983, 2023), lpattern(dash solid) xscale(r(1983 2023))

            local scattericall -10 2023 "GDP" 10 2023 "CFNAI", mlabsize(*1.0) msymbol(none)

            twoway line `linecall' || scatteri `scattericall'


            P.S: Both variables contain positive and negative values. Data is in quarterly format.
            Stata version: Stata/SE 15.1
            Attached Files

            Comment


            • #7
              Please show us the results of

              Code:
              summarize date GDP_PERCT_CHG CFNAI_INDEX
              as if you have a quarterly date variable you need to phrase calls accordingly.

              .
              Code:
               di yq(1983,1) "  " yq(2023, 4)
              92  255
              
               . di %tq 2023
              2465q4
              As above for example 2023 as a Stata quarterly date is more than 400 years into the future,

              Comment


              • #8
                Thanks Nick. Here is the summary result:

                Variable Obs Mean Std. Dev. Min Max

                date 164 173.5 47.48684 92 255
                GDP_PERCT_~G 164 1.299443 1.201946 -8.2619 8.71031
                CFNAI_INDEX 164 -.0148171 .4884651 -2.373333 1.43

                Comment


                • #9
                  So, I think you need something more like

                  Code:
                  local scattericall -10 252  "GDP" 10 252 "CFNAI", mlabsize(*1.0) msymbol(none)

                  Comment


                  • #10
                    Thanks, Nick. I have been able to figure it out.
                    However, I want the recession bars to start at the base of the graph. Is there a way to fix it?
                    Here is my code:

                    Code:
                    summ GDP_PERCT_CHG
                    gen recs = r(max) if recession == 1
                    replace recs = r(min) if recession == 0
                    summ GDP_PERCT_CHG
                    local min = r(min)
                    twoway (bar recs date, color(gs10) base(-2.373333) base(-8.2619)) (tsline GDP_PERCT_CHG CFNAI_INDEX, lcolor(black)), tlabel(, format(%tqCCYY))
                    Attached Files
                    Last edited by Dami Giwa; 24 Mar 2024, 21:14.

                    Comment


                    • #11
                      I can't quite follow the question. You have two calls to base(), neither of which seems to be what you want. Why not use the value you want?

                      Comment


                      • #12
                        Right! I tried that and it worked! Thanks for your input! Your comments helped me think through my codes.

                        Comment

                        Working...
                        X