Announcement

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

  • costomizing the lagened - while using two yaxis

    Dear all,
    I am making a twoway graph as follows:

    Code:
    twoway ///
        rarea up90_lrgfcf_con_lremp dn90_lrgfcf_con_lremp horizon if horizon <= 5, lpattern(dash) color(blue%20) yaxis(1) || ///
        rarea up90_lrgfcf_con_lrgdp_cons dn90_lrgfcf_con_lrgdp_cons horizon if horizon <= 5, lpattern(dash) color(red%20) yaxis(2) || ///
        line b_lrgfcf_con_lremp horizon if horizon < 6, lcolor(blue) lpattern(solid) lwidth(medthick) yaxis(1) || ///
        line b_lrgfcf_con_lrgdp_cons horizon if horizon < 6, lcolor(red) lpattern(solid) lwidth(medthick) yaxis(2) || ///
        line a horizon if horizon < 6, lpattern(dash) lwidth(vthin) lcolor(blue) yaxis(1) || ///
        line a horizon if horizon < 6, lpattern(dash) lwidth(vthin) lcolor(red) yaxis(2) || ///
        function x=0, range(-1 5) lpattern(dash) lcolor(black) lwidth(thin) ///   <-- vertical dashed line at horizon=0
        , ylabel(, alternate axis(1)) ylabel(, alternate axis(2)) xlabel(-1 (1) 5) ///
        ytitle("Employment effect (%)", axis(1) size(medsmall)) ///
        ytitle("GDP effect (%)", axis(2) size(medsmall)) ///
        xtitle("Years after the shock", size(medsmall)) ///
        graphregion(color(white)) plotregion(color(white)) ///
        legend(order(3 "Employment" 4 "GDP") size(small) position(6)) ///
        title("Effect of Shock on Employment and GDP by Investment")
    when I plot is, the lagened is not correct.


    I will be grateful for your help.
    Sincerely,
    Khattereh.
    Attached Files

  • #2
    We may need a reproducible example here. Show us the results of

    Code:
    dataex horizon up90_lrgfcf_con_lremp dn90_lrgfcf_con_lremp up90_lrgfcf_con_lrgdp_cons dn90_lrgfcf_con_lrgdp_cons  b_lrgfcf_con_lremp b_lrgfcf_con_lrgdp_cons a
    Not the question, but I can't follow what you're trying to do with

    Code:
     line a horizon if horizon < 6, lpattern(dash) lwidth(vthin) lcolor(blue) yaxis(1) || /// line a horizon if horizon < 6, lpattern(dash) lwidth(vthin) lcolor(red) yaxis(2) || ///
    Last edited by Nick Cox; 22 May 2025, 04:34.

    Comment


    • #3
      Nick Cox many thanks for your reply.
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(horizon up90_lrgfcf_con_lremp dn90_lrgfcf_con_lremp up90_lrgfcf_con_lrgdp_cons dn90_lrgfcf_con_lrgdp_cons b_lrgfcf_con_lremp b_lrgfcf_con_lrgdp_cons a)
      -1         0          0        0          0           0         0 0
       0 .24164927 -.27074358 .3084162 -.12631527 -.014547148 .09105048 0
       1  .2004789   -.383395 .6184772 .010742236  -.09145804  .3146097 0
       2  .3458918 -.54460126 .7091957   .0376194  -.09935476  .3734075 0
       3  .5336255  -.5834656 .5878274  -.3468825  -.02492007 .12047244 0
       4  .5043523   -.348125 .6617512   -.420035   .07811363 .12085804 0
       5  .7187792  -.3365943 .8892213 -.24601863   .19109243  .3216013 0
       6         0          0        0          0           0         0 0
       7         0          0        0          0           0         0 0
       8         0          0        0          0           0         0 0
       9         0          0        0          0           0         0 0
      10         0          0        0          0           0         0 0
      11         0          0        0          0           0         0 0
      12         0          0        0          0           0         0 0
      13         0          0        0          0           0         0 0
      14         0          0        0          0           0         0 0
      15         0          0        0          0           0         0 0
      16         0          0        0          0           0         0 0
      17         0          0        0          0           0         0 0
      18         0          0        0          0           0         0 0
      19         0          0        0          0           0         0 0
      20         0          0        0          0           0         0 0
      21         0          0        0          0           0         0 0
      22         0          0        0          0           0         0 0
      23         0          0        0          0           0         0 0
      24         0          0        0          0           0         0 0
      25         0          0        0          0           0         0 0
      26         0          0        0          0           0         0 0
      27         0          0        0          0           0         0 0
      28         0          0        0          0           0         0 0
      29         0          0        0          0           0         0 0
      30         0          0        0          0           0         0 0
      31         0          0        0          0           0         0 0
      32         0          0        0          0           0         0 0
      33         0          0        0          0           0         0 0
      34         0          0        0          0           0         0 0
      35         0          0        0          0           0         0 0
      36         0          0        0          0           0         0 0
      37         0          0        0          0           0         0 0
      38         0          0        0          0           0         0 0
      39         0          0        0          0           0         0 0
      40         0          0        0          0           0         0 0
      41         0          0        0          0           0         0 0
      42         0          0        0          0           0         0 0
      43         0          0        0          0           0         0 0
      44         0          0        0          0           0         0 0
      45         0          0        0          0           0         0 0
      46         0          0        0          0           0         0 0
      47         0          0        0          0           0         0 0
      48         0          0        0          0           0         0 0
      49         0          0        0          0           0         0 0
      50         0          0        0          0           0         0 0
      51         0          0        0          0           0         0 0
      52         0          0        0          0           0         0 0
      53         0          0        0          0           0         0 0
      54         0          0        0          0           0         0 0
      55         0          0        0          0           0         0 0
      56         0          0        0          0           0         0 0
      57         0          0        0          0           0         0 0
      58         0          0        0          0           0         0 0
      59         0          0        0          0           0         0 0
      60         0          0        0          0           0         0 0
      61         0          0        0          0           0         0 0
      62         0          0        0          0           0         0 0
      63         0          0        0          0           0         0 0
      64         0          0        0          0           0         0 0
      65         0          0        0          0           0         0 0
      66         0          0        0          0           0         0 0
      67         0          0        0          0           0         0 0
      68         0          0        0          0           0         0 0
      69         0          0        0          0           0         0 0
      70         0          0        0          0           0         0 0
      71         0          0        0          0           0         0 0
      72         0          0        0          0           0         0 0
      73         0          0        0          0           0         0 0
      74         0          0        0          0           0         0 0
      75         0          0        0          0           0         0 0
      76         0          0        0          0           0         0 0
      77         0          0        0          0           0         0 0
      78         0          0        0          0           0         0 0
      79         0          0        0          0           0         0 0
      80         0          0        0          0           0         0 0
      81         0          0        0          0           0         0 0
      82         0          0        0          0           0         0 0
      83         0          0        0          0           0         0 0
      84         0          0        0          0           0         0 0
      85         0          0        0          0           0         0 0
      86         0          0        0          0           0         0 0
      87         0          0        0          0           0         0 0
      88         0          0        0          0           0         0 0
      89         0          0        0          0           0         0 0
      90         0          0        0          0           0         0 0
      91         0          0        0          0           0         0 0
      92         0          0        0          0           0         0 0
      93         0          0        0          0           0         0 0
      94         0          0        0          0           0         0 0
      95         0          0        0          0           0         0 0
      96         0          0        0          0           0         0 0
      97         0          0        0          0           0         0 0
      98         0          0        0          0           0         0 0
      end
      ------------------ copy up to and including the previous line ------------------

      Listed 100 out of 9890 observations
      Use the count() option to list more

      ​​​​​​​

      Comment


      • #4
        Try

        Code:
        legend(order(2 "Employment" 6  "GDP") size(small) position(6))
        The order of items in the legend appears to be yaxis(1) items first; yaxis(2) second; otherwise as mentioned in the command.

        Comment


        • #5
          Nick Cox , many thanks for your reply.
          it works!

          Best regards,
          Khatereh

          Comment


          • #6
            The debugging tip is to let the legend be the default and work out the order of variables that arises.

            With the same data I played with alternative designs. Here is one. (I now understand the reason for plotting a twice, but the result only underlines that the two scales are not aligned.

            For anyone interested the code here includes code for the graph already discussed (but the graph isn't shown) as well for the graph new to this post and shown here.

            Code:
            clear
            input float(horizon up90_lrgfcf_con_lremp dn90_lrgfcf_con_lremp up90_lrgfcf_con_lrgdp_cons dn90_lrgfcf_con_lrgdp_cons b_lrgfcf_con_lremp b_lrgfcf_con_lrgdp_cons a)
            -1         0          0        0          0           0         0 0
             0 .24164927 -.27074358 .3084162 -.12631527 -.014547148 .09105048 0
             1  .2004789   -.383395 .6184772 .010742236  -.09145804  .3146097 0
             2  .3458918 -.54460126 .7091957   .0376194  -.09935476  .3734075 0
             3  .5336255  -.5834656 .5878274  -.3468825  -.02492007 .12047244 0
             4  .5043523   -.348125 .6617512   -.420035   .07811363 .12085804 0
             5  .7187792  -.3365943 .8892213 -.24601863   .19109243  .3216013 0
             end 
             
            twoway ///
            rarea up90_lrgfcf_con_lremp dn90_lrgfcf_con_lremp horizon if horizon <= 5, lpattern(dash) color(blue%20) yaxis(1) || ///
            rarea up90_lrgfcf_con_lrgdp_cons dn90_lrgfcf_con_lrgdp_cons horizon if horizon <= 5, lpattern(dash) color(red%20) yaxis(2) || ///
            line b_lrgfcf_con_lremp horizon if horizon < 6, lcolor(blue) lpattern(solid) lwidth(medthick) yaxis(1) || ///
            line b_lrgfcf_con_lrgdp_cons horizon if horizon < 6, lcolor(red) lpattern(solid) lwidth(medthick) yaxis(2) || ///
            line a horizon if horizon < 6, lpattern(dash) lwidth(vthin) lcolor(blue) yaxis(1) || ///
            line a horizon if horizon < 6, lpattern(dash) lwidth(vthin) lcolor(red) yaxis(2) || ///
            function x=0, range(-1 5) lpattern(dash) lcolor(black) lwidth(thin) ///   <-- vertical dashed line at horizon=0
            , ylabel(, alternate axis(1)) ylabel(, alternate axis(2)) xlabel(-1 (1) 5) ///
            ytitle("Employment effect (%)", axis(1) size(medsmall)) ///
            ytitle("GDP effect (%)", axis(2) size(medsmall)) ///
            xtitle("Years after the shock", size(medsmall)) ///
            graphregion(color(white)) plotregion(color(white)) ///
            legend(order(2 "Employment" 6  "GDP") size(small) position(6)) ///
            title("Effect of Shock on Employment and GDP by Investment")
            
            local both function x=0, range(-1 5) lpattern(dash) lcolor(black) lwidth(thin) yli(0)
            local both `both' xlabel(-1/5, grid) xsc(r(-1, 5)) xtitle("Years after the shock", size(medsmall)) 
            local both `both' graphregion(color(white)) plotregion(color(white)) legend(off)
            
            twoway rarea up90_lrgfcf_con_lremp dn90_lrgfcf_con_lremp horizon, lpattern(dash) color(blue%20) || ///
            line b_lrgfcf_con_lremp horizon, lcolor(blue) lpattern(solid) lwidth(medthick) || ///
            `both' ytitle("Employment effect (%)",  size(medsmall)) fysize(45) xsc(off) name(G1, replace)
            
            twoway rarea up90_lrgfcf_con_lrgdp_cons dn90_lrgfcf_con_lrgdp_cons horizon, lpattern(dash) color(red%20) ///
            || line b_lrgfcf_con_lrgdp_cons horizon, lcolor(red) lpattern(solid) lwidth(medthick) || ///
            `both' ytitle("GDP effect (%)",  size(medsmall)) fysize(55) name(G2, replace)
            
            graph combine G1 G2, col(1) title("Effect of Shock on Employment and GDP by Investment")
            Click image for larger version

Name:	emp_gdp.png
Views:	1
Size:	79.8 KB
ID:	1777871


            Comment


            • #7
              It's easy to imagine that much of the point here is to compare employment and GDP effects, but I find the graph in #1 overloaded and hard to read.

              You could move a little nearer the original design by plotting the GDP curve as a reference curve in the employment panel (facet) and vice versa. But then you'd need a legend and/or extra labelling.

              A related idea is that of front-and-back plots. https://journals.sagepub.com/doi/pdf...6867X211025838

              Comment

              Working...
              X