Announcement

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

  • Can't set the scale of y axis in twoway plot

    I'm puzzled by problem despite reading manuals and searching this forum. I'm trying to plot two lines and the percent difference between them. I need the bar plot to be first so it doesn't cover the lines, and I want it to be on the second yaxis. I also want the bar plot to have a scale for 0-100. Notably, the data is split by group, so I'm using the by() option in each plot. I'm using Stata 15.1

    The command I think should work:
    Code:
    twoway (bar diff1 year,  by(reg_wesp) yscale(range(0 100)) ylabel(0 (20) 100) yaxis(2)) (line gdpc_sc0 gdpc_sc1 year, by(reg_wesp))
    I also tried moving the yaxis options inside the by(), like this:
    Code:
    twoway (bar diff1 year,  by(reg_wesp, yscale(range(0 100)) ylabel(0 (20) 100) yaxis(2)) ) (line gdpc_sc0 gdpc_sc1 year, by(reg_wesp, yaxis(1)))
    Nothing I do can get what I think should happen. The bar plot yaxis is always on the left. And its scale doesn't go from 0-100. At most I can get it to go to zero by using "base(0)".

    I'm attaching a problem plot (I hope it works).

    broken.gph



    Sample data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year long reg_wesp float(gdpc_sc0 gdpc_sc1 diff1)
    2015 1 19.475634  22.33815 14.697938
    2016 1   19.3123  22.11383  14.50645
    2017 1  19.45104  22.23611 14.318353
    2018 1 19.600586 22.424105 14.405277
    2019 1  19.63441  22.44581 14.318737
    2015 2  47.26918  61.90598  30.96479
    2016 2  49.40838  64.93085  31.41667
    2017 2  51.57659  68.03413 31.908924
    2018 2  53.62329  70.99496  32.39575
    2019 2  55.26569  73.41037 32.831726
    2015 3  54.85363  69.13499  26.03539
    2016 3  54.80115  69.34748 26.543846
    2017 3  55.58055  70.72945  27.25575
    2018 3   56.9198  72.63471  27.60888
    2019 3  57.87658  74.12664 28.077093
    2015 4 267.81012  371.8127  38.83447
    2016 4  268.7362  372.6435  38.66516
    2017 4  272.0571  375.1159 37.881313
    2018 4 273.46848  374.3576 36.892406
    2019 4 273.30478  372.3015   36.2221
    end
    label values reg_wesp reg_wesp
    label def reg_wesp 1 "Africa", modify
    label def reg_wesp 2 "Asia", modify
    label def reg_wesp 3 "CIS and Georgia", modify
    label def reg_wesp 4 "Developed Asia and Pacific", modify


  • #2
    Your values to be plotted extend way above 100 so the axis scale option is ignored, except that 0 should be plotted— which should happen any way. See the help for axis scale options, which explains.

    Comment


    • #3
      I saw that condition, but I thought I'm just plotting the bar chart on the axis with the scale argument. The bar chart uses data from the "diff1" column and it does not go above 40. I'm ok with the line chart to use whichever scale it needs.

      Comment


      • #4
        Use transparency to see the lines that lie within the bars.

        Code:
        twoway (line gdpc_sc0 gdpc_sc1 year, by(reg_wesp)) ///
        (bar diff1 year,  by(reg_wesp) ylabel(0 (20) 100, axis(2))  bcolor(gray%20) yaxis(2))
        Last edited by Andrew Musau; 19 Jan 2022, 14:48.

        Comment


        • #5
          Thanks Andrew. That indeed gets me to the result I wanted.

          I was curious why my version didn't work and I figured out why. A combination of commas and parenthesis, that you have in red.

          For those that didn't follow:
          First, I don't need the -yscale()- option. The -ylabel()- option gets the scale right.
          Second, as your code shows, the -axis(2)- needs to be inside -ylabel-.
          Third, I still need -yaxis(2)-
          With that, I can get the bar plot scale to work well.

          But interestingly, the order of the twoway still matters. I tried reversing the order (first bar, then line) and despite telling twoway to plot the bar on axis(2), it still puts it in axis(1). Why would it not respect the option? Maybe it gets confused by the by() option?

          Anyways, I'll use your code. Thanks for the help.
          Last edited by Marcelo LaFleur; 19 Jan 2022, 15:13.

          Comment


          • #6
            Marcelo LaFleur , great that the problem has been resolved. However, please note the forum policy on attaching graphs outlined here in section 12.4
            Roman

            Comment


            • #7
              My first reading was too hasty, but on a second reading I don't understand what you want to show. How does diff1 relate to the GDP variables?

              I got this far:

              Code:
              clear
              input int year long reg_wesp float(gdpc_sc0 gdpc_sc1 diff1)
              2015 1 19.475634  22.33815 14.697938
              2016 1   19.3123  22.11383  14.50645
              2017 1  19.45104  22.23611 14.318353
              2018 1 19.600586 22.424105 14.405277
              2019 1  19.63441  22.44581 14.318737
              2015 2  47.26918  61.90598  30.96479
              2016 2  49.40838  64.93085  31.41667
              2017 2  51.57659  68.03413 31.908924
              2018 2  53.62329  70.99496  32.39575
              2019 2  55.26569  73.41037 32.831726
              2015 3  54.85363  69.13499  26.03539
              2016 3  54.80115  69.34748 26.543846
              2017 3  55.58055  70.72945  27.25575
              2018 3   56.9198  72.63471  27.60888
              2019 3  57.87658  74.12664 28.077093
              2015 4 267.81012  371.8127  38.83447
              2016 4  268.7362  372.6435  38.66516
              2017 4  272.0571  375.1159 37.881313
              2018 4 273.46848  374.3576 36.892406
              2019 4 273.30478  372.3015   36.2221
              end
              label values reg_wesp reg_wesp
              label def reg_wesp 1 "Africa", modify
              label def reg_wesp 2 "Asia", modify
              label def reg_wesp 3 "CIS and Georgia", modify
              label def reg_wesp 4 "Developed Asia and Pacific", modify
              
              twoway connected gdpc_sc0 gdpc_sc1 year, by(reg_wesp, yrescale) ysc(log)

              Click image for larger version

Name:	gdpc.png
Views:	1
Size:	37.5 KB
ID:	1645916

              Comment


              • #8
                diff1 is simply the percentage difference (gen diff1 = gdpc_sc1 / gdpc_sc0 -1)

                Comment


                • #9
                  OK. Perhaps the aim of showing absolute values and relative values on the same graph is not going to work well.

                  Here is perhaps part of your story:


                  Code:
                  clear
                  input int year long reg_wesp float(gdpc_sc0 gdpc_sc1 diff1)
                  2015 1 19.475634  22.33815 14.697938
                  2016 1   19.3123  22.11383  14.50645
                  2017 1  19.45104  22.23611 14.318353
                  2018 1 19.600586 22.424105 14.405277
                  2019 1  19.63441  22.44581 14.318737
                  2015 2  47.26918  61.90598  30.96479
                  2016 2  49.40838  64.93085  31.41667
                  2017 2  51.57659  68.03413 31.908924
                  2018 2  53.62329  70.99496  32.39575
                  2019 2  55.26569  73.41037 32.831726
                  2015 3  54.85363  69.13499  26.03539
                  2016 3  54.80115  69.34748 26.543846
                  2017 3  55.58055  70.72945  27.25575
                  2018 3   56.9198  72.63471  27.60888
                  2019 3  57.87658  74.12664 28.077093
                  2015 4 267.81012  371.8127  38.83447
                  2016 4  268.7362  372.6435  38.66516
                  2017 4  272.0571  375.1159 37.881313
                  2018 4 273.46848  374.3576 36.892406
                  2019 4 273.30478  372.3015   36.2221
                  end
                  label values reg_wesp reg_wesp
                  label def reg_wesp 1 "Africa", modify
                  label def reg_wesp 2 "Asia", modify
                  label def reg_wesp 3 "CIS & Georgia", modify
                  label def reg_wesp 4 "Developed Asia & Pacific", modify
                  
                  gen ratio = gdpc_sc1 / gdpc_sc0
                  separate ratio, by(reg_wesp) veryshortlabel
                  twoway connected ratio? year, ysc(log) ms(O D S T) mc(red blue black orange) ytitle("version 1 / version 0") xtitle("") ///
                  legend(off) || scatter ratio? year if year == 2019, ms(none ..) mlabc(red blue black orange) mla(reg_wesp reg_wesp reg_wesp reg_wesp) mlabpos(3) xsc(r(. 2020.6))
                  Click image for larger version

Name:	ratio.png
Views:	1
Size:	26.8 KB
ID:	1646023


                  Spelling out that the line colours match the other colours used would have been a good idea too.
                  Last edited by Nick Cox; 20 Jan 2022, 11:37.

                  Comment


                  • #10
                    Thanks Nick. I'm going to think carefully about presenting the data using ratios as you suggest. This has been very helpful.

                    Comment

                    Working...
                    X