Announcement

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

  • Combined graph in stata


    Hello, I would like to draw a combined graph in which the contributions of the components to the total variation of the sum variable appear on the stacked bars, together with the line that describes the sum variable (see the attached graph in excel). How can I do it? Thank you in advance. Img for stata.png Marco

  • #2
    I am not before my pc, the following code maybe need to do some adjustment.
    Code:
    twoway bar dlpft capva lavva year, stack || connect diva year, yaxis(2) ylabel(-5(1)5)

    Comment


    • #3
      There is no data example here, and in any case the graph from MS Excel (which is what I think you mean) doesn't make much sense to me. Given components A, B. C which can be positive or negative (or zero) one recipe is

      Code:
      gen AB = A + B
      gen ABC = AB + C
      and then you can go

      Code:
      twoway bar A year || rbar A AB year || rbar AB ABC year
      and ABC is automatically the resultant, which you can plot separately if you wish.

      Almost none of these stacked bars start at zero, so let's guess that I am just misunderstanding what you want.

      Independently of that, combining red and green on a display is not a good idea.

      Comment


      • #4
        Originally posted by Chen Samulsion View Post
        I am not before my pc, the following code maybe need to do some adjustment.
        Code:
        twoway bar dlpft capva lavva year, stack || connect diva year, yaxis(2) ylabel(-5(1)5)
        This code would be the right code, but unfortunately "stack" option is not allowed with twoway.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          There is no data example here, and in any case the graph from MS Excel (which is what I think you mean) doesn't make much sense to me. Given components A, B. C which can be positive or negative (or zero) one recipe is

          Code:
          gen AB = A + B
          gen ABC = AB + C
          and then you can go

          Code:
          twoway bar A year || rbar A AB year || rbar AB ABC year
          and ABC is automatically the resultant, which you can plot separately if you wish.

          Almost none of these stacked bars start at zero, so let's guess that I am just misunderstanding what you want.

          Independently of that, combining red and green on a display is not a good idea.
          I drew the excel graph very quickly, just to have a raw Idea. All the stacked bars start from 0, but on the rhs of the graph. The 0s of the 2 y axes are not aligned. The weird colours are just to have a clear idea. The graph represents the decomposition of value added (dlava) among its contributions) TFP, capital and labour. The right graph is the following (different scale).
          Img for stata.png

          Comment


          • #6
            Thanks for explaining that the labels on the left in your graph are wrong. I still can't follow what the dashed line is doing.

            There are more objections than stack being illegal to Chen's code as the bars aren't stacked even without it. So, it's not the code you seek.

            My code is not right either as I guess you want negative components to be stacked separately from positive.

            Nothing beats a good data example here!

            Comment


            • #7
              Originally posted by Nick Cox View Post
              Thanks for explaining that the labels on the left in your graph are wrong. I still can't follow what the dashed line is doing.

              There are more objections than stack being illegal to Chen's code as the bars aren't stacked even without it. So, it's not the code you seek.

              My code is not right either as I guess you want negative components to be stacked separately from positive.

              Nothing beats a good data example here!
              Below the data. dlva=dltfp+capva+lavva.
              year dlva dltfp (dx) capva (dx) lavva (dx)
              1996 0.012188 1.609203 -0.6817 -0.91531
              1997 0.022875 0.630561 -0.64504 0.03735
              1998 0.025538 2.005753 -0.58083 -1.39939
              1999 0.054229 2.840631 -0.50738 -2.27902
              2000 0.008153 4.304501 -0.30227 -3.99408
              2001 -0.02877 -0.98376 -0.63488 1.58987
              2002 -0.04827 1.721446 -0.599 -1.17071
              2003 -0.03967 1.732056 -0.58573 -1.186
              2004 0.110663 -0.33254 -0.50118 0.944377
              2005 -0.0509 1.475137 -0.48415 -1.0419
              2006 -0.00756 -0.32653 -0.35749 0.676464
              2007 0.010813 1.235326 -0.47961 -0.7449
              2008 0.020832 3.309675 -0.7994 -2.48945
              2009 -0.01023 2.350233 -1.14812 -1.21235
              2010 0.002476 1.591856 -1.01646 -0.57292
              2011 0.007717 2.662456 -0.90211 -1.75263
              2012 -0.04045 2.962323 -1.12566 -1.87712
              2013 0.012591 1.70807 -1.55302 -0.14246
              2014 -0.02155 0.556944 -1.51484 0.936341
              2015 0.046213 0.501076 -1.47827 1.023405
              2016 -0.00329 -0.243 -1.17417 1.413884
              2017 -0.04081 1.94506 -0.84066 -1.14521
              2018 0.02883 -0.95392 -0.57466 1.557407
              2019 -0.02028 1.127075 -0.4755 -0.67185
              2020 -0.0426 3.314502 -0.70852 -2.64859
              2021 -0.00276 -1.62979 -0.29625 1.923284
              2022 0.026298 -0.0801 -0.4453 0.551703
              2023 -0.0359 2.108601 -0.48639 -1.65812

              Comment


              • #8
                "stack" option is not allowed with twoway
                Sorry for that. You don't provide data example here. Let's use sp500. I recommend to use an user-written command -genstack- (ssc install genstack) to prepare the data. This command is designed to generate stacked variables for twoway stacked chart. However, I still don't know whether the following is what you want. By the way, Nick Cox​​​​​​'s outdated command -barplot-​ is very interesting in this case. Below is the code.

                Code:
                ssc install genstack
                ssc install barplot
                
                sysuse sp500
                gen low_neg=-low
                gen close_neg=-close
                genstack open high low_neg close_neg , gen(C_)
                twoway bar C_close_neg C_low_neg C_high C_open date || connecte volume date, yaxis(2)
                barplot C_close_neg C_low_neg C_high C_open date
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	472.4 KB
ID:	1771570


                Click image for larger version

Name:	Untitled.png
Views:	1
Size:	570.4 KB
ID:	1771571

                Comment


                • #9
                  Thank you for the data. Here is the code, and I think it will be helpful for you. And you still need to install -genstack- command.

                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input float(year dlva dltfp capva lavva DLVA_dltfp DLVA_capva DLVA_lavva)
                  1996 .012188 1.609203   -.6817  -.91531 1.609203     -.6817   -1.59701
                  1997 .022875  .630561  -.64504   .03735  .630561    -.64504    .667911
                  1998 .025538 2.005753  -.58083 -1.39939 2.005753    -.58083   -1.98022
                  1999 .054229 2.840631  -.50738 -2.27902 2.840631    -.50738    -2.7864
                  2000 .008153 4.304501  -.30227 -3.99408 4.304501    -.30227   -4.29635
                  2001 -.02877  -.98376  -.63488  1.58987  -.98376   -1.61864    1.58987
                  2002 -.04827 1.721446    -.599 -1.17071 1.721446      -.599   -1.76971
                  2003 -.03967 1.732056  -.58573   -1.186 1.732056    -.58573   -1.77173
                  2004 .110663  -.33254  -.50118  .944377  -.33254    -.83372    .944377
                  2005  -.0509 1.475137  -.48415  -1.0419 1.475137    -.48415   -1.52605
                  2006 -.00756  -.32653  -.35749  .676464  -.32653    -.68402    .676464
                  2007 .010813 1.235326  -.47961   -.7449 1.235326    -.47961   -1.22451
                  2008 .020832 3.309675   -.7994 -2.48945 3.309675     -.7994   -3.28885
                  2009 -.01023 2.350233 -1.14812 -1.21235 2.350233   -1.14812   -2.36047
                  2010 .002476 1.591856 -1.01646  -.57292 1.591856   -1.01646   -1.58938
                  2011 .007717 2.662456  -.90211 -1.75263 2.662456    -.90211   -2.65474
                  2012 -.04045 2.962323 -1.12566 -1.87712 2.962323   -1.12566   -3.00278
                  2013 .012591  1.70807 -1.55302  -.14246  1.70807   -1.55302   -1.69548
                  2014 -.02155  .556944 -1.51484  .936341  .556944   -1.51484   1.493285
                  2015 .046213  .501076 -1.47827 1.023405  .501076   -1.47827   1.524481
                  2016 -.00329    -.243 -1.17417 1.413884    -.243   -1.41717   1.413884
                  2017 -.04081  1.94506  -.84066 -1.14521  1.94506    -.84066   -1.98587
                  2018  .02883  -.95392  -.57466 1.557407  -.95392   -1.52858   1.557407
                  2019 -.02028 1.127075   -.4755  -.67185 1.127075     -.4755 -1.1473501
                  2020  -.0426 3.314502  -.70852 -2.64859 3.314502    -.70852   -3.35711
                  2021 -.00276 -1.62979  -.29625 1.923284 -1.62979   -1.92604   1.923284
                  2022 .026298   -.0801   -.4453  .551703   -.0801 -.52540004    .551703
                  2023  -.0359 2.108601  -.48639 -1.65812 2.108601    -.48639   -2.14451
                  end
                  
                  genstack dltfp capva lavva, gen(DLVA_)
                  twoway bar DLVA_lavva DLVA_capva DLVA_dltfp year, barwidth(0.7 0.7 0.7) || connected dlva year, yaxis(2) xlabel(1996(2)2022) ytitle("", axis(2)) legend(pos(6) row(1))
                  Click image for larger version

Name:	dlva.png
Views:	1
Size:	269.7 KB
ID:	1771575

                  Comment


                  • #10
                    Thanks for the data example. Meanwhile I was hacking away at this.

                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input float(year A B C)
                    1 -1  5 1
                    2 -2  4 2
                    3  3  3 3
                    4  4 -2 2
                    5  5 -1 1
                    end
                    
                    gen zero = 0 
                    gen posA = max(A, 0)
                    gen posAB = max(A, 0) + max(B, 0)
                    gen posABC = max(A, 0) + max(B, 0) + max(C, 0)
                    gen negA = min(A, 0)
                    gen negAB = min(A, 0) + min(B, 0)
                    gen negABC = min(A, 0) + min(B, 0) + min(C, 0)
                    gen total = A + B + C 
                    
                    twoway bar posA year, col(stc1) barw(0.8) /// 
                    || rbar posA posAB year, col(stc2) barw(0.8) /// 
                    || rbar posABC posAB year, col(stc3) barw(0.8) /// 
                    || bar negA year, col(stc1) barw(0.8) /// 
                    || rbar negA negAB year, col(stc2) barw(0.8) /// 
                    || rbar negABC negAB year, col(stc3) barw(0.8) ///
                    || line total year, lc(black) lw(medthick) legend(order(1 "A" 2 "B" 3 "C" 7 "Total"))
                    Click image for larger version

Name:	whatever.png
Views:	1
Size:	22.1 KB
ID:	1771577

                    Comment


                    • #11
                      If you want to reproduce what you get in Excel, you need some adjust on the code above. For example, set xtitle as none (year is obvious and thus unnecessary), exchange the yaxis (use yaxis(2) to display the stacked bar), and lastly, rename the variables with stubname of DLAV_, etc.

                      Edit: here is my adjusted code.
                      Code:
                      rename (dltfp capva lavva) =_org
                      genstack dltfp_org capva_org lavva_org, gen(c_)
                      rename c_*_org *
                      twoway bar lavva capva dltfp year, barwidth(0.7 0.7 0.7) yaxis(2) || connected dlva year, yaxis(1) xlabel(1996(2)2022) xtitle("") ytitle("", axis(1)) legend(pos(6) row(1))
                      Last edited by Chen Samulsion; 27 Jan 2025, 08:00.

                      Comment


                      • #12
                        Here's another take. I am not a great fan of stacked bars in general and not at all when magnitudes differ so much (and also in sign).

                        I used multiline from SSC.
                        https://www.statalist.org/forums/for...ailable-on-ssc

                        The data example in #7 won't run as code without minor surgery.

                        Code:
                        clear 
                        input year    dlva    dltfp     capva     lavva 
                        1996    0.012188    1.609203    -0.6817    -0.91531
                        1997    0.022875    0.630561    -0.64504    0.03735
                        1998    0.025538    2.005753    -0.58083    -1.39939
                        1999    0.054229    2.840631    -0.50738    -2.27902
                        2000    0.008153    4.304501    -0.30227    -3.99408
                        2001    -0.02877    -0.98376    -0.63488    1.58987
                        2002    -0.04827    1.721446    -0.599    -1.17071
                        2003    -0.03967    1.732056    -0.58573    -1.186
                        2004    0.110663    -0.33254    -0.50118    0.944377
                        2005    -0.0509    1.475137    -0.48415    -1.0419
                        2006    -0.00756    -0.32653    -0.35749    0.676464
                        2007    0.010813    1.235326    -0.47961    -0.7449
                        2008    0.020832    3.309675    -0.7994    -2.48945
                        2009    -0.01023    2.350233    -1.14812    -1.21235
                        2010    0.002476    1.591856    -1.01646    -0.57292
                        2011    0.007717    2.662456    -0.90211    -1.75263
                        2012    -0.04045    2.962323    -1.12566    -1.87712
                        2013    0.012591    1.70807    -1.55302    -0.14246
                        2014    -0.02155    0.556944    -1.51484    0.936341
                        2015    0.046213    0.501076    -1.47827    1.023405
                        2016    -0.00329    -0.243    -1.17417    1.413884
                        2017    -0.04081    1.94506    -0.84066    -1.14521
                        2018    0.02883    -0.95392    -0.57466    1.557407
                        2019    -0.02028    1.127075    -0.4755    -0.67185
                        2020    -0.0426    3.314502    -0.70852    -2.64859
                        2021    -0.00276    -1.62979    -0.29625    1.923284
                        2022    0.026298    -0.0801    -0.4453    0.551703
                        2023    -0.0359    2.108601    -0.48639    -1.65812
                        end 
                        
                        multiline dlva    dltfp     capva     lavva year, xla(1996(4)2020 2023) yli(0) xtitle("")
                        Click image for larger version

Name:	whatever2.png
Views:	1
Size:	40.6 KB
ID:	1771580

                        Comment


                        • #13
                          Originally posted by Chen Samulsion View Post
                          If you want to reproduce what you get in Excel, you need some adjust on the code above. For example, set xtitle as none (year is obvious and thus unnecessary), exchange the yaxis (use yaxis(2) to display the stacked bar), and lastly, rename the variables with stubname of DLAV_, etc.

                          Edit: here is my adjusted code.
                          Code:
                          rename (dltfp capva lavva) =_org
                          genstack dltfp_org capva_org lavva_org, gen(c_)
                          rename c_*_org *
                          twoway bar lavva capva dltfp year, barwidth(0.7 0.7 0.7) yaxis(2) || connected dlva year, yaxis(1) xlabel(1996(2)2022) xtitle("") ytitle("", axis(1)) legend(pos(6) row(1))
                          The graph you drew above is what I want, but when I try to reproduce I get all the points connected:
                          Click image for larger version

Name:	Img for stata.png
Views:	1
Size:	105.8 KB
ID:	1771583

                          Comment


                          • #14
                            The graph you drew above is what I want, but when I try to reproduce I get all the points connected
                            Well, just add sort option after twoway connected. Here it is:
                            Code:
                            rename (dltfp capva lavva) =_org
                            genstack dltfp_org capva_org lavva_org, gen(c_)
                            rename c_*_org *
                            twoway bar lavva capva dltfp year, barwidth(0.7 0.7 0.7) yaxis(2) || connected dlva year, sort yaxis(1) xlabel(1996(2)2022) xtitle("") ytitle("", axis(1)) legend(pos(6) row(1))
                            Click image for larger version

Name:	dltfp.png
Views:	1
Size:	269.5 KB
ID:	1771586

                            Comment


                            • #15
                              Originally posted by Nick Cox View Post
                              Here's another take. I am not a great fan of stacked bars in general and not at all when magnitudes differ so much (and also in sign).

                              I used multiline from SSC.
                              https://www.statalist.org/forums/for...ailable-on-ssc

                              The data example in #7 won't run as code without minor surgery.

                              Code:
                              clear
                              input year dlva dltfp capva lavva
                              1996 0.012188 1.609203 -0.6817 -0.91531
                              1997 0.022875 0.630561 -0.64504 0.03735
                              1998 0.025538 2.005753 -0.58083 -1.39939
                              1999 0.054229 2.840631 -0.50738 -2.27902
                              2000 0.008153 4.304501 -0.30227 -3.99408
                              2001 -0.02877 -0.98376 -0.63488 1.58987
                              2002 -0.04827 1.721446 -0.599 -1.17071
                              2003 -0.03967 1.732056 -0.58573 -1.186
                              2004 0.110663 -0.33254 -0.50118 0.944377
                              2005 -0.0509 1.475137 -0.48415 -1.0419
                              2006 -0.00756 -0.32653 -0.35749 0.676464
                              2007 0.010813 1.235326 -0.47961 -0.7449
                              2008 0.020832 3.309675 -0.7994 -2.48945
                              2009 -0.01023 2.350233 -1.14812 -1.21235
                              2010 0.002476 1.591856 -1.01646 -0.57292
                              2011 0.007717 2.662456 -0.90211 -1.75263
                              2012 -0.04045 2.962323 -1.12566 -1.87712
                              2013 0.012591 1.70807 -1.55302 -0.14246
                              2014 -0.02155 0.556944 -1.51484 0.936341
                              2015 0.046213 0.501076 -1.47827 1.023405
                              2016 -0.00329 -0.243 -1.17417 1.413884
                              2017 -0.04081 1.94506 -0.84066 -1.14521
                              2018 0.02883 -0.95392 -0.57466 1.557407
                              2019 -0.02028 1.127075 -0.4755 -0.67185
                              2020 -0.0426 3.314502 -0.70852 -2.64859
                              2021 -0.00276 -1.62979 -0.29625 1.923284
                              2022 0.026298 -0.0801 -0.4453 0.551703
                              2023 -0.0359 2.108601 -0.48639 -1.65812
                              end
                              
                              multiline dlva dltfp capva lavva year, xla(1996(4)2020 2023) yli(0) xtitle("")
                              [ATTACH=CONFIG]n1771580[/ATTACH]
                              My fault. There was a bug in the code generating variables so that very different scale.

                              Comment

                              Working...
                              X