Announcement

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

  • stata: graphical question related to rarea & tempvar


    I would like to create
    Hi everyone,

    I am trying to do a graph with shaded areas. The graph enclosed is what I am trying to do.



    but I have a little problem. I want that the shaded areas go from the bottom to the top. I tried to create the following code, but works partially:

    Code:
    sum N100
    tempvar ylow yhigh
    gen `ylow' = floor(r(min))
    gen `yhigh' = ceil(r(max))
    *
    *
    *
    #d ;
    xtline N100, over
        addplot(
            line N100 t if c=="GE":clab, lw(*2) lc(red)
            
            || scatter N100 t if !inlist(c,10,26) & t==ym(2023,3), ms(i) mlab(c) mlabcol(gs0) mlabpos(3)
            || scatter N100 t if inlist(c,10,26) & t==ym(2023,3), ms(i) mlab(c) mlabcol(gs0) mlabpos(2)
            
            || rarea `ylow' `yhigh' lockdown1_0320_0620, color(gs3%10)
            || rarea `ylow' `yhigh' lockdown2_0121_0521, color(gs3%10)
        )
        xline(`=ym(2017,8)' `=ym(2017,11)' `=ym(2020,3)' `=ym(2020,11)'
        , lc(gs0) lp(-) lw(thin))
        text(
            150 `=ym(2017,8)'  "W{sup:NE}{sub:min}" // sal.min. 08/2017
            150 `=ym(2018,2)'  "W{sup:JU}{sub:min}" // sal.min. 02/2018
            150 `=ym(2020,3)'  "Mesures Covid"
            150 `=ym(2020,11)' "W{sup:GE}{sub:min}"
            , place(n) m(b=3) size(vsmall)
        )
        ylab(60(10)150, labs(small))
        xlab(`=ym(2015,1)'(12)`=ym(2023,3)', format(%tmNN.CCYY) labs(small))
        ysc(r(60 150))
        yti("Nombre de chômeurs") xti("")
        legend(order(7 1 2 3 4 6) lab(7 "GE") row(1))
        ysiz(15) xsiz(30)
        graphr(m(t=7))
        name(uXcanton_index, replace)
    ;
    #d cr
    Could anyone help me with that? I would like that my local chooses directly the good `ylow' and `yhigh` with a loop. Here, I am trying to "estimate" the good `ylow' by trial and error.
    Thanks in advance.

    Michael
    Attached Files
    Last edited by Michael Duarte Goncalves; 09 Jun 2023, 01:48.

  • #2
    You're insisting that your y-axis goes from 60 to 150 at least. so if the range of N100 is less, your shading won't extend to the top and bottom of the graph.

    Perhaps you want the shading to extend from min(60, floor of data minimum) to max(150, ceiling of data maximum).

    Comment


    • #3
      Hi Nick Cox,

      Thanks for your feedback. Yes, that's exactly what I want to do:


      shading from floor of data minimum to max(i.e. ceiling of data maximum).

      Code:
      . sum N100
      
          Variable |        Obs        Mean    Std. dev.       Min        Max
      -------------+---------------------------------------------------------
              N100 |        594    97.03722    13.24345   55.54866   131.3101

      Comment


      • #4
        Hi again Nick Cox,

        Just a little precision:

        But I want that the shading area goes from the bottom to the top, graphically speaking.

        Michael

        Comment


        • #5
          Sorry, but I can't follow what you want. #3 seems to imply that you want to respect the range of a variable and #4 that the shading should go from the bottom of the graph to the top. Evidently, the two goals may clash.

          Comment


          • #6
            I apologize for my poor explanation.

            Basically, here's an example of what I'm looking to achieve, in terms of shading. It comes from "When Losses Turn into Loans: The Cost of Weak Banks" (2023), published in the American Economic Review, vol.113, no.6 (June 2023):

            Click image for larger version

Name:	AER_June2023.png
Views:	1
Size:	193.9 KB
ID:	1716588


            Comment


            • #7
              In #6 and generally for such graphs, (1) the shaded range exceeds (2) the range of any of the variables plotted. Whether for your application you need to calculate (2) before you can decide on (1) is the practical question. Not my field at all, but my guess is that in #6 the limits of about 0.11 and -0.11 are not limits of principle but just chosen to accommodate the data comfortably.

              Comment


              • #8
                Ah great, that's why.

                Thanks for your feedback.
                Best,

                Michael

                Comment


                • #9
                  I've had success using levelsof and xline rather than having to guess the y-axis range (I'm pretty sure I learned this from Nick somewhere on Statalist).

                  Code:
                  clear all 
                  
                  
                  import delimited using "https://fred.stlouisfed.org/series/UNRATE/downloaddata/UNRATE.csv", clear 
                  ren value urate 
                  tempfile temp
                  save `temp'
                  
                  import delimited using "https://fred.stlouisfed.org/series/USRECM/downloaddata/USRECM.csv", clear 
                  ren value rec
                  
                  merge 1:1 date using `temp', keep(match) nogen 
                  gen month = mofd(date(date, "YMD")), before(date)
                  format month %tmMon-YY
                  
                  levelsof month if rec, local(months) clean
                  tw line urate month, xline(`months', lpattern(solid) lcolor(gs14)) xtitle("") ytitle("%") lcolor(blue)
                  Code:
                  
                  
                  Click image for larger version

Name:	urate.png
Views:	1
Size:	138.4 KB
ID:	1716616

                  Comment


                  • #10
                    Hi Justin Niakamal,

                    Your suggestion works perfectly.
                    Thank you so much.

                    Best,

                    Michael

                    Comment

                    Working...
                    X