Announcement

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

  • Combining stacked barchart (with negative values) with line using twoway

    Dear all,

    I'm trying to overlay a stacked bar chart with a line chart.

    I plot 7 variables (some of which are negative most of the time) in the barchart with the "graph bar, stack" routine:

    Code:
    graph bar (asis) var1 var2 var3 var4 var5 var6 var7, stack
    Click image for larger version

Name:	image_22582.png
Views:	1
Size:	41.1 KB
ID:	1609157


    I now want to add a variable that is the difference between all positive and negative variables as a line to the chart.

    It seems that "graph bar" does not allow this and that I'd need to switch to a "twoway" routine with "bar", "rbar" and "line".

    However, trying this out and reading the discussion HERE, I'm not sure how this could be done if some of my variables are negative - or if this can be done in my situation at all.

  • #2
    I am running into the same problem as Andy, so I hope that someone might have a solution for how to create a bar chart with both positive and negative values.
    graph_inflation_decomp_main_reg.png


    This is what the graph looks like using graph bar but obviously this won't allow me to plot another line. You can see that 3 of the four components oscillate between being positive and negative, so I can't combine them in any one specific order to ensure there won't be any overlap. I also am trying to use the over() option with a quarterly date variable, but I can't figure out how to only display some of the labels. If I have to stick with bar graph, then it would be helpful to have an idea of how to do this.

    This is the code I currently have, but obviously the way I am generating the bars for the twoway plot is not correct given that not all the values are positive.

    Code:
    graph bar (asis) comp_ugap comp_pc_urate comp_infl_exp comp_pc_infl if inrange(date_yq, tq(1984q1), tq(2019q4)), over(date_yq, label(angle(45) alternate labsize(tiny))) bargap(.1) name(inflation_components_v1, replace) stack title("Inflation Decomposition")  legend(order(1 "Unemployment Gap" 2 "PC of Unemployment Gap" 3 "Inflation Expectations" 4 "PC of Inflation Deviations"))
    
    * Generate bars for the decomposition chart
    gen bar_pc_infl = comp_pc_infl
    gen bar_infl_exp = comp_infl_exp + comp_pc_infl 
    gen bar_pc_urate = comp_pc_urate + comp_infl_exp + comp_pc_infl
    gen bar_ugap = comp_ugap + comp_pc_urate + comp_infl_exp + comp_pc_infl
           
    twoway (bar bar_ugap date_yq, barwidth(.75)) || ///
           (bar bar_pc_urate date_yq, barwidth(.75)) || ///
           (bar bar_infl_exp date_yq, barwidth(.75)) || ///
           (bar bar_pc_infl date_yq, barwidth(.75)) || ///
           (line cpi_infl_yoyUSA date_yq) if inrange(date_yq, tq(1984q1), tq(2019q4)), title("Inflation Decomposition") legend(order(1 "Unemployment Gap" 2 "PC of Unemployment Gap" 3 "Inflation Expectations" 4 "PC of Inflation Deviations" 5 "YoY CPI Inflation")) xlabel(,angle(45))
    Here is a snippet of the data from 2010-2019:

    Code:
    clear
    input double date_yq float(cpi_infl_yoyUSA infl_exp ugap pc_infl pc_urate b_pc_infl b_infl_exp b_pc_urate b_ugap comp_pc_infl comp_infl_exp comp_pc_urate comp_ugap)
    201   1.767765 1.8775   4.789333  1.1343961   .8520799 .3012544 .850033 -.12143505 -.072333366   .3417418  1.595937  -.10347237   -.3464286
    202  1.1756089 1.7209  4.6296663  -.2795476  .17345235 .3012544 .850033 -.12143505 -.072333366 -.08421494  1.462822 -.021063196   -.3348793
    203   1.270248  1.609      4.671  -.5092422  2.2899835 .3012544 .850033 -.12143505 -.072333366 -.15341145  1.367703  -.27808428   -.3378691
    204   2.141127 1.7247  4.2133336   .8880997 -1.9988465 .3012544 .850033 -.12143505 -.072333366   .2675439 1.4660518   .24273004   -.3047646
    205  3.4303954 2.1304  4.2556667  1.4024277 -4.3533335 .3012544 .850033 -.12143505 -.072333366   .4224875 1.8109102    .5286473   -.3078267
    206   3.756174 2.0125      4.198    .826742  -.8935137 .3012544 .850033 -.12143505 -.072333366  .24905966 1.7106915   .10850388   -.3036555
    207  3.2937765 1.9692  3.8413334  1.2592603   1.966702 .3012544 .850033 -.12143505 -.072333366   .3793577  1.673885  -.23882657  -.27785656
    208   2.815192  2.075   3.484666   .3722294   .6017069 .3012544 .850033 -.12143505 -.072333366  .11213575 1.7638185  -.07306831  -.25205764
    209   1.889765 2.1831      3.429 -1.1387343  .36911225 .3012544 .850033 -.12143505 -.072333366  -.3430487  1.855707  -.04482317   -.2480311
    210  1.6977844 2.1098   3.271334   .3159398 -1.0183913 .3012544 .850033 -.12143505 -.072333366  .09517825 1.7933997    .1236684   -.2366266
    211  1.8893654 2.1931      3.048   .7706487   .6093867 .3012544 .850033 -.12143505 -.072333366   .2321613 1.8642073  -.07400091  -.22047213
    212   1.681829    2.1   2.990333  -.9458265   2.596865 .3012544 .850033 -.12143505 -.072333366  -.2849344 1.7850692   -.3153504  -.21630086
    213  1.3928498 2.0414   2.799333  -.8478221   2.510469 .3012544 .850033 -.12143505 -.072333366 -.25541013 1.7352573   -.3048589   -.2024852
    214  1.5533587 1.8626   2.507333   .5671202  .09379639 .3012544 .850033 -.12143505 -.072333366  .17084745 1.5832714  -.01139017  -.18136385
    215   1.233471  1.984  2.2153335  -.5762158  -.6486745 .3012544 .850033 -.12143505 -.072333366 -.17358755 1.6864654   .07877183  -.16024253
    216  1.4054564 1.8881  1.9556665 -.28374568  1.1163388 .3012544 .850033 -.12143505 -.072333366 -.08547963 1.6049473  -.13556267  -.14145994
    217  2.0508456 1.9583  1.4959998  1.0971627  -.9334263 .3012544 .850033 -.12143505 -.072333366   .3305251 1.6646196   .11335067   -.1082107
    218   1.783154  2.099  1.3696666   .5602407 -.50588816 .3012544 .850033 -.12143505 -.072333366  .16877496 1.7842193   .06143256   -.0990726
    219  1.2480277 1.9162  1.0099998  -.4613005  .23187394 .3012544 .850033 -.12143505 -.072333366  -.1389688 1.6288333 -.028157625  -.07305668
    220 -.06269593 1.8938   .8493333 -1.9720105    .896744 .3012544 .850033 -.12143505 -.072333366  -.5940768 1.6097925  -.10889616  -.06143514
    221 -.03827201 1.9731   .7563334  -.0876354   .1153743 .3012544 .850033 -.12143505 -.072333366 -.02640055    1.6772 -.014010483  -.05470814
    222   .1095034 2.0422   .4289999  .14783333 -1.2434194 .3012544 .850033 -.12143505 -.072333366  .04453544 1.7359375    .1509947 -.031031007
    223   .4662646 2.0435   .3693333  .58983105  .08079761 .3012544 .850033 -.12143505 -.072333366   .1776892 1.7370423 -.009811662  -.02671512
    224  1.0802671 1.9849  .24300003  .19646227  1.0364263 .3012544 .850033 -.12143505 -.072333366  .05918512 1.6872305  -.12585849  -.01757701
    225  1.0470631 2.0574   .2833333  -.7048873    .196718 .3012544 .850033 -.12143505 -.072333366  -.2123504  1.748858  -.02388846  -.02049445
    226  1.1176153 2.2276  .25699997  -1.342066  -.5964288 .3012544 .850033 -.12143505 -.072333366  -.4043033 1.8935336   .07242736 -.018589674
    227  1.8006208 2.2029  .13066673  -.7500671   .7978568 .3012544 .850033 -.12143505 -.072333366   -.225961 1.8725376  -.09688778 -.009451564
    228  2.5393205 2.3596 -.06133318  2.4358275   .5196755 .3012544 .850033 -.12143505 -.072333366   .7338037 2.0057378  -.06310682  .004436435
    229  1.9019914  2.275 -.25433302  .16263594  -.8419599 .3012544 .850033 -.12143505 -.072333366  .04899479  1.933825   .10224345  .018396763
    230   1.966925 2.2053 -.27966642 .069986925   1.259212 .3012544 .850033 -.12143505 -.072333366  .02108387  1.874578  -.15291247  .020229213
    231  2.1175575 2.0907  -.4383335    .354585  -.5408554 .3012544 .850033 -.12143505 -.072333366  .10682028  1.777164  .065678805   .03170614
    232  2.2141945 2.2117  -.5636668  -1.298792 -1.5147632 .3012544 .850033 -.12143505 -.072333366  -.3912668  1.880018   .18394536   .04077192
    233   2.711887 2.2597  -.6546664 -.57885927  -.8058838 .3012544 .850033 -.12143505 -.072333366  -.1743839 1.9208196   .09786254   .04735423
    234    2.64094 2.2589  -.8123333  1.4723552  -.9567183 .3012544 .850033 -.12143505 -.072333366   .4435535 1.9201394   .11617914   .05875881
    235  2.2031314 2.3658  -.7356665   .8729657   .7209505 .3012544 .850033 -.12143505 -.072333366  .26298475  2.011008  -.08754867   .05321323
    236   1.644936 2.2651  -.6923335  -.3188704 -.04583866 .3012544 .850033 -.12143505 -.072333366  -.0960611 1.9254097   .00556642   .05007881
    237  1.8113756 2.0855  -.9146664   .7916945  -1.063224 .3012544 .850033 -.12143505 -.072333366  .23850144 1.7727438   .12911266    .0661609
    238  1.7574885 2.0369  -.9370003  -.9528717 -1.0080968 .3012544 .850033 -.12143505 -.072333366  -.2870568 1.7314322    .1224183   .06777638
    239   2.032914 2.1384  -.9250002 -1.0397493   -.311735 .3012544 .850033 -.12143505 -.072333366   -.313229 1.8177106   .03785555  .066908374
    240          .      .          .          .          . .3012544 .850033 -.12143505 -.072333366          .         .           .           .
    241          .      .          .          .          . .3012544 .850033 -.12143505 -.072333366          .         .           .           .
    242          .      .          .          .          . .3012544 .850033 -.12143505 -.072333366          .         .           .           .
    end
    format %tq date_yq

    Comment


    • #3
      I gather that these components are considered to be additive, but not only they often negative, they seem to have quite different magnitudes.

      Here is a different take. Anyone liking the main idea would probably want to move on to different colours.

      Code:
      rename (comp_ugap comp_pc_urate comp_infl_exp comp_pc_infl) (comp#), addnumber
      
      reshape long comp, i(date_yq) j(which)
      
      label def which 1 "Unemployment Gap" 2  "PC of Unemployment Gap" 3  "Inflation Expectations" 4  "PC of Inflation Deviations"
      
      label val which which
      
      twoway bar comp date_yq, by(which, yrescale col(1) note("")) ytitle(%) xla(200(4)240, format(%tq)) xtitle("") fcolor(stc1*0.2)
      Click image for larger version

Name:	ue.png
Views:	1
Size:	59.1 KB
ID:	1752739

      Comment


      • #4
        Shortly after posting I found this post on Statalist: https://www.statalist.org/forums/for...nd-a-line-plot

        I was able to get the chart that I needed with the following adaptations:

        Code:
        gen top = 0 
        gen bottom = 0 
        
        foreach j in infl_exp pc_urate ugap pc_infl { 
            
            gen base_`j' = cond(comp_`j' >=  0, top, bottom)
            gen show_`j' = base_`j' + comp_`j'
            
            replace top = top + comp_`j' if comp_`j' >= 0 
            replace bottom = bottom + comp_`j' if comp_`j' < 0 
        }
        
        local opts barw(0.75)
        twoway (rbar base_ugap show_ugap date_yq, `opts') || /// 
        (rbar base_pc_urate show_pc_urate date_yq, `opts') || ///
        (rbar base_infl_exp show_infl_exp date_yq, `opts') || ///
        (rbar base_pc_infl show_pc_infl date_yq, `opts') || ///
        (line cpi_infl_yoyUSA date_yq) if inrange(date_yq, tq(1984q1), tq(2019q4)) ,  title("Inflation Decomposition") legend(order(1 "PC of Inflation Deviations" 2 "PC of Unemployment Gap" 3 "Inflation Expectations" 4 "Unemployment Gap" 5 "YoY CPI Inflation")) xlabel(,angle(45))
        twoway_inflation_decomp_main_reg.png

        Comment


        • #5
          Thanks for the suggestion, Nick. Part of the point of the graph is to highlight the different magnitudes that you noted. Your method from a different post was helpful in getting what I needed, though.

          Comment

          Working...
          X