Announcement

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

  • Twoway bar and line with 2 y-axes and labels

    Hello
    I am having some data from IMF for a panel data for countries. Countries are classified by income and region. I provide a sample of my data below at the end of my inquiry.

    Where index1, index2a, and index2b are quality indexes I have produced and ka is the variable of interest. I am trying to reproduce the attached graph.


    Click image for larger version

Name:	Screen Shot 2022-12-20 at 01.42.35.png
Views:	1
Size:	24.0 KB
ID:	1694312


    I am using the following code in order to create a bar graph classifying countries by their IMF region.

    Code:
    recode  ts (1980/1989 =1 "1980/1989")(1990/1999=2 "1990/1999")(2000/2009 =3 "2000/2009 ")(2010/2020=4 "2010/2020"), gen(decades)  
    graph bar (mean) ka, over(imf_region) asyvar over(decades)
    I have been troubled in adding a second y line at the right with a second variable such is index1


    I have also tried with no success


    Code:
    line kaopen ts if ts>=1950 & imf_region1==1, lw(medthick) || bar kaopen ts if ts>=1950 &  imf_income1==1, yaxis(2) fi(20) ///
        || line kaopen ts if ts>=1950 & imf_region2==2, lc(navy) lp(dash) || line kaopen ts if ts>=1950 & region3==1 || ///
    line kaopen ts if ts>=1950 & region4==1 || line kaopen ts if ts>=1950 & region5==1, /// 
        lc(navy) lp(dash) graphr(c(white))  title("A: Case1 {sup:a}", size(medsmall)) ///
        ylabel(, axis(1)) ylabel(, axis(2)) xlabel(1950(10)2020) ///
        xtitle("") yline(0, lc(navy) lp(dash)) ytitle("", axis(1)) ///
        legend(label(3 "")) ytitle("", axis(2)) legend(label(2 "")) ///
        legend(order(1 4)) legend(col(1)) name(case1) nodraw




    After that, while keeping the first Y constant, change the second Y and graph for index2a index2b.
    Note that instead of varname, the label of the variables should be displayed.


    Also, while I do have a code for the line graphs and the code I used should work, again with a second y at the right, I don't know how to draw the lines inside the same graph. I tried the by option, however it produces three different types of graphics. Not the desired way.


    My first thought was to collapse, but then I got confused.

    I would like to reproduce the graph in the image for countries and for the ka variable in the data according to their classification, with a second y at the right.

    Finally, how is it possible to split it into two regions above and below, say region A on the top with title and region B at the bottom title with their title, and then a general title?


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float ts str28(imf_region imf_income) str97 country float(ka index1 index2a index2b) byte(cat1 cat2) float(d1 d2)
    1986 "Asia and Pacific" "AM" "Australia"    .8800636        8.6         0        1  4 4 1 0
    1987 "Asia and Pacific" "AM" "Australia"    .9400318  2.7697804         0        1  4 4 1 0
    1988 "Asia and Pacific" "AM" "Australia"           1       -4.5         0        1  4 4 1 0
    1989 "Asia and Pacific" "AM" "Australia"           1       -4.5         0        1  4 4 1 0
    1996 "Asia and Pacific" "AM" "Australia"    .9400318  18.228453         0        1  4 4 1 0
    1997 "Asia and Pacific" "AM" "Australia"    .8800636     22.593         0        1  4 4 1 0
    1998 "Asia and Pacific" "AM" "Australia"    .8200954    27.6381         0        1  4 4 1 0
    1999 "Asia and Pacific" "AM" "Australia"    .7601272     48.458         0        1  1 4 1 0
    2006 "Asia and Pacific" "AM" "Australia"     .700159     31.889         0        1  1 4 1 0
    2007 "Asia and Pacific" "AM" "Australia"     .700159   29.87246         0        1  1 4 1 0
    2008 "Asia and Pacific" "AM" "Australia"     .700159      5.674         0        1  1 4 1 0
    2009 "Asia and Pacific" "AM" "Australia"     .700159      5.674         0        1  1 4 1 0
    2010 "Asia and Pacific" "AM" "Australia"     .700159  -6.130934  .2967033 .7032967  1 4 1 0
    2011 "Asia and Pacific" "AM" "Australia"     .700159    -34.113         1        0  1 4 1 0
    2012 "Asia and Pacific" "AM" "Australia"    .7601272    -34.113         1        0  1 4 1 0
    2013 "Asia and Pacific" "AM" "Australia"    .8200954 -17.802141  .7142857 .2857143  1 4 1 0
    2014 "Asia and Pacific" "AM" "Australia"    .8800636     22.975         0        1  1 4 1 0
    1986 "Asia and Pacific" "EM" "India"       .16434518          0         0        1  8 2 1 0
    1987 "Asia and Pacific" "EM" "India"       .16434518          0         0        1  8 2 1 0
    1988 "Asia and Pacific" "EM" "India"       .16434518          0         0        1  8 2 1 0
    1989 "Asia and Pacific" "EM" "India"       .16434518          0         0        1  8 2 1 0
    1996 "Asia and Pacific" "EM" "India"       .16434518          0         1        0  7 2 1 0
    1997 "Asia and Pacific" "EM" "India"       .16434518          0         1        0  7 2 1 0
    1998 "Asia and Pacific" "EM" "India"       .16434518          0 .21153846 .7884616  7 2 1 0
    1999 "Asia and Pacific" "EM" "India"       .16434518          0  .4917582 .5082418  7 2 1 0
    2006 "Asia and Pacific" "EM" "India"       .16434518          0         1        0  8 2 1 0
    2007 "Asia and Pacific" "EM" "India"       .16434518          0         1        0  8 2 1 0
    2008 "Asia and Pacific" "EM" "India"       .16434518          0         1        0  8 2 1 0
    2009 "Asia and Pacific" "EM" "India"       .16434518          0         1        0 10 3 1 0
    2010 "Asia and Pacific" "EM" "India"       .16434518          .         .        . 10 3 1 0
    2011 "Asia and Pacific" "EM" "India"       .16434518          .         .        . 10 3 1 0
    2012 "Asia and Pacific" "EM" "India"       .16434518          .         .        . 10 3 1 0
    2013 "Asia and Pacific" "EM" "India"       .16434518          .         .        .  8 2 1 0
    2014 "Asia and Pacific" "EM" "India"       .16434518          .         .        .  8 2 1 0
    1986 "Asia and Pacific" "AM" "New Zealand"  .8800636     -9.788         0        1 12 3 1 0
    1987 "Asia and Pacific" "AM" "New Zealand"  .9400318 -14.278473         0        1 12 3 1 0
    1988 "Asia and Pacific" "AM" "New Zealand"         1    -21.986         0        1 12 3 1 0
    1989 "Asia and Pacific" "AM" "New Zealand"         1    -21.986         0        1 12 3 1 0
    1996 "Asia and Pacific" "AM" "New Zealand"         1  -6.882123         0        1 12 3 1 0
    1997 "Asia and Pacific" "AM" "New Zealand"         1     -7.235         0        1 12 3 1 0
    1998 "Asia and Pacific" "AM" "New Zealand"         1     -7.235 .33516484 .6648352 12 3 1 0
    1999 "Asia and Pacific" "AM" "New Zealand"         1  -8.191456         1        0 12 3 1 0
    2006 "Asia and Pacific" "AM" "New Zealand"         1    -29.707         1        0 12 3 1 0
    2007 "Asia and Pacific" "AM" "New Zealand"         1    -29.707         1        0 12 3 1 0
    2008 "Asia and Pacific" "AM" "New Zealand"         1 -21.978655         1        0 12 3 1 0
    2009 "Asia and Pacific" "AM" "New Zealand"         1     37.456         1        0 12 3 1 0
    2010 "Asia and Pacific" "AM" "New Zealand"         1     37.456         1        0 12 3 1 0
    2011 "Asia and Pacific" "AM" "New Zealand"         1  35.647743         1        0 12 3 1 0
    2012 "Asia and Pacific" "AM" "New Zealand"         1     -1.262         1        0 12 3 1 0
    2013 "Asia and Pacific" "AM" "New Zealand"         1     -1.262         1        0 12 3 1 0
    2014 "Asia and Pacific" "AM" "New Zealand"         1  -2.527769         1        0 12 3 1 0
    1986 "Europe"           "EM" "Turkey"      .16434518        6.5         0        1 14 5 0 1
    1987 "Europe"           "EM" "Turkey"      .16434518   6.464286         0        1 14 5 0 1
    1988 "Europe"           "EM" "Turkey"      .16434518        5.2         0        1 14 5 0 1
    1989 "Europe"           "EM" "Turkey"      .41687185        5.2         0        1 14 5 0 1
    1996 "Europe"           "EM" "Turkey"      .16434518  29.073414 .49041095  .509589 14 5 1 0
    1997 "Europe"           "EM" "Turkey"      .16434518  31.695776         0        1 14 5 1 0
    1998 "Europe"           "EM" "Turkey"      .16434518     29.936         0        1 10 3 1 0
    1999 "Europe"           "EM" "Turkey"      .16434518  14.872792  .3763736 .6236264 10 3 1 0
    2006 "Europe"           "EM" "Turkey"      .16434518      5.187         0        1 13 4 1 0
    2007 "Europe"           "EM" "Turkey"      .16434518      5.187         0        1 13 4 1 0
    2008 "Europe"           "EM" "Turkey"       .4476323          .         .        . 13 4 1 0
    2009 "Europe"           "EM" "Turkey"       .4476323          .         .        . 13 4 1 0
    2010 "Europe"           "EM" "Turkey"       .4476323          .         .        . 12 3 1 0
    2011 "Europe"           "EM" "Turkey"       .4476323          .         .        . 12 3 1 0
    2012 "Europe"           "EM" "Turkey"       .4476323          .         .        . 12 3 1 0
    2013 "Europe"           "EM" "Turkey"       .4476323          .         .        . 12 3 1 0
    2014 "Europe"           "EM" "Turkey"       .4476323          .         .        . 12 3 1 0
    end





  • #2
    Your sample graph isn't to me clearly related to your data and the rest of your text. One thing is certain: graph bar can't be combined with twoway line.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Your sample graph isn't to me clearly related to your data and the rest of your text. One thing is certain: graph bar can't be combined with twoway line.
      Nick, I have two distinct codes where I tried to combine them in order to produce a single graph as in the picture in #1. I am aware that graph bar can't be combined with twoway line, hence I am looking for a way to go.

      For the first bar graph, I run:
      Code:
      graph bar (median )  ka, over(imf_region) asyvar over(decade)
      But cannot add a second y variable there, nor the labels

      For the line I have a code where I usually run it when I have two or more variables. I usually run this

      Code:
      twoway (line ka ts if ts>0, yaxis(1)) (line index1 ts if ts>0, yaxis(2) lpattern(dash)) ,   ytitle(capital % ) ///
       ytitle((index1 %), axis(2)) ttitle(year) legend(label(1 "capital % %")) ///
       legend(label(2 "index1 %")) graphregion(color(white)) bgcolor(white) ///
       by(id, r2title("index1 %") title("A: Case1 {sup:a}", size(medsmall)) ///
        legend(off) yl(,format(%03.1f))

      But in this case I'm interested only in the evolution of the ka variable over time by each group showing a second y, index1 in this case, and its corresponding values to index1


      My problem here is while the code works, having the by option produces a graph by id, for each country as expected, but not aggregated by groups. When I tried the option by, by group, say (by imf_region1 imf_region2) somehow it failed. My first idea was to collapse, but then I got confused on how to precede and then how to combine with the first graph.

      In sort, I am trying to combine them in a way, hence on the top of the graph to show the lines by group and at the bottom to have the bar graph by groups again. Both with two y variables at left and right with their label

      The second code in #1 I tried to run came from a stata replication code, so the image. I can provide it in full in case it is necessary. Probably it was me who did not get that right. I hope now it is clear and would appreciate any help.





      Comment

      Working...
      X