Announcement

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

  • Add dot to bar graph

    Hi all,

    I have a couple of countries with simulated changes in tax revenues, revenues from contributions, and benefit payments. this also done for three reforms (ref = 1,2,3). So far, I plot them as follows:

    Code:
    graph bar dtax dsic dben, over(ref, label(angle(90) labsize(vsmall))) over(country) stack legend(rows(1)  label(1 "TAX") label(2 "SIC") label(3 "BEN"))
    This works fine, but you the total effect (tax + sic - ben) is not easily visible this way. Thus, I'd like to add a dot to each indicating the sum of the three, in the spirit graphs like this one: dx.doi.org/10.1787/888933005473

    I know that this is possible with twoway bar , but then I face many other problems:
    - no nesting possible (by variable ref)
    - labelling the x-axis doesn't work if it's a categorical variable (country)
    - no control about the space between the bars.

    Is this somehow feasible when sticking to graph bar?
    Last edited by Eric Sommer; 28 Jul 2015, 03:10.

  • #2
    I would not rule out the twoway graph, as it is a powerful and flexible module to design your own graph. If I understood correctly the design of the graph you wish to build, the limitations you specified can be solved. Please include some data so we can see how your file is built and offer you possible examples.

    Comment


    • #3
      Dear Oded,

      attached a sample data set containing three countries and two reforms (in wide format).

      I want to have the following for one reform:

      Code:
      tw (bar dtax1 dsic1 dben1 co) (dot dtot1 co)
      I guess adding the second reform besides can be achieved by using a new country variable in between:

      Code:
      gen co2 = co + 0.5
      
      tw (bar dtax1 dsic1 dben1 co, barw(0.1 0.1 0.1)) (dot dtot1 co) (bar dtax2 dsic2 dben2 co2,barw(0.1 0.1 0.1)) (dot dtot2 co2)
      It does the job, but I find the "graph bar" much more appealing visually.
      The main problem left now: How can I make the x-axis be labelled according to the label of co, i.e. the country names?

      thanks a lot
      Eric

      Attached Files
      Last edited by Eric Sommer; 29 Jul 2015, 01:35.

      Comment


      • #4
        I am not sure which bars and dots represent which country but the command below should lead you in the right direction.
        Code:
        tw (bar dtax1 dsic1 dben1 co, barw(0.1 0.1 0.1)) (dot dtot1 co) (bar dtax2 dsic2 dben2 co2,barw(0.1 0.1 0.1)) (dot dtot2 co2), xlabel(1.25 "Canada" 2.25 "France" 3.25 "USA")

        Comment


        • #5
          Here is an example to reproduce exactly the same graph of bar with the twoway module. in order to move from stacked bar to twoway graph I used two tricks: first, the rbar is used to get the cumulative results and second, the major/minor labels to get the two layers of the x axis.

          Code:
          ​clear*
          input dtax1    dsic1    dben1    dtot1    dtax2    dsic2    dben2    dtot2    co
          -.03190133    -.30408451    -.29179422    -.04419162    .00912327    .0750711    -.13262563    .21682    1
          -.03633074    -.029038    .03522088    -.10058963    .04296726    .05202636    .00295562    .092038    2
          -.01462963    -.01812054    -.04095193    .00820176    .07392905    .06597166    -.07517433    .21507504    3
          end
          la de co 1 "Canada" 2 "France" 3 "USA"
          la val co co
          reshape long dtax dsic dben dtot, i(co)j(A)
          
          * Your example with graph bar
          
          graph bar dtax dsic dben, over(A, label(angle(90) labsize(vsmall))) ///
          over(co) stack legend(rows(1)  label(1 "TAX") label(2 "SIC") label(3 "BEN"))
          
          * The suggested example with the twoway approach for the same style of bars.
          
          g order=_n
          g base=0
          g dsic2=dtax+dsic
          g dben2=cond((dben<0 & dsic<0)|(dben>0 & dsic>0),dsic2+dben,dben)
          
           twoway (rbar base dtax order, color(navy) fint(full) barwidth(0.33)) ///
           (rbar dsic2 dtax order, color(maroon) fint(full) barwidth(0.33)) ///
           (rbar base dben2 order if (dben>0 & dsic<0)|(dben<0 & dsic>0), color(forest_green) fint(full) barwidth(0.33)) ///
           (rbar dben2 dsic2 order if (dben<0 & dsic<0)|(dben>0 & dsic>0), color(forest_green) fint(full) barwidth(0.33)) ///
           (scatter dtot order, color(orange)), ///
           xtitle(, size(zero)) ///
           xlabel(1 "1" 2 "2" 3 "1" 4 "2" 5 "1" 6 "2", labsize(small) angle(forty_five) labgap(small) noticks) ///
           xmtick(1.5 "Canada" 3.5 "France" 5.5 "USA", labels labsize(medium) angle(horizontal) labgap(medium) noticks) ///
           legend(row(1) order(1 "TAX" 2 "SIC" 3 "BEN" 5 "DOT"))

          Comment


          • #6
            Thanks, this is awesome!
            Last edited by Eric Sommer; 30 Jul 2015, 04:38.

            Comment

            Working...
            X