Announcement

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

  • How to expand yscale range in marginsplot with addplot

    Using Stata/SE 16.1 I made two related Figures that should be easily comparable. This means that they should have the same scale. However, one has a y-scale that runs from -20 to 40, and the other from -20 to 100.
    Click image for larger version

Name:	ME 7.png
Views:	1
Size:	94.7 KB
ID:	1563924


    Click image for larger version

Name:	ME 7a.png
Views:	1
Size:	55.0 KB
ID:	1563925


    So I want to expand the yscale on the first. I added
    Code:
    ysc(r(-20(20)100))
    , but this has no effect whatsoever.
    Here is what I did:

    Code:
    margins, dydx(incumbent) at(share_rd=(0(10)50)) vsquish vce(unconditional)
    marginsplot,                                                                                                   ///
            yline(0)                                                                                                  ///
            graphregion(color(white))                                                                          ///
            title("Effect van collegedeelname naar aandeel collegepartijen in raad")        ///
            subtitle("Marginaal effect van collegedeelname op procentuele verandering in aandeel raadszetels", size(small))  ///
            recast(line)                                                                                            ///
            recastci(rarea)                                                                                        ///
            ci1opt(fcolor(midblue%40) lcolor(midblue))                                                ///
            ytitle("Marginaal effect" "met 95% betrouwbaarheidsinterval", size(small))    ///
            ylabel(, labsize(small))                                                                            ///
            ysc(r(-20(20)100))                                                                                    ///
            legend(off)                                                                                              ///
            note("Gecontroleerd voor effect van kabinetsdeelname, aantal collegepartijen en verkiezingsjaar")      ///
            addplot(histogram share_rd if share_rd <=50,                                            ///
               frequency                                                                                           ///
               xtitle("Aandeel collegepartijen in raadszetels (%)", size(small))               ///
               xlabel(, labsize(small))                                                                         ///
               yaxis(2)                                                                                             ///
               ytitle("Aantal waarnemingen (staaf)", axis(2) size(small))                        ///
               ylabel(#6, axis(2) format(%8.0g) labsize(small))                                     ///
               ysc(axis(2) alt)                                                                                    ///
               fcolor(teal%30)                                                                                    ///
               lcolor(teal%30))
    Interestingly, if I delete addplot and all code after it, this does work. But then my Figure is not what it should be.
    Moving
    Code:
    ysc(r(-20(20)100))
    to the very end of the code, after the last
    Code:
    )
    , does not make a difference.
    What do I do wrong? Any help would be greatly appreciated.

  • #2
    You probably need to present a data example to reproduce the problem. That said, I have found previously that either changing the -ylabel()- or -yscale()- range does the trick. See, for example,
    https://www.statalist.org/forums/for...ifferent-yaxis
    https://www.statalist.org/forums/for...xes-in-tw-line

    Comment


    • #3
      Thank you, Andrew, for responding. I tried your suggestion, but it doesn't seem to do the trick.

      I simplified the exercise in order to be able to present a data example.
      Suppose I want to expand the left-hand yscale of the plot below from 100 to 150.
      I made two plots, the second one with -yscale and -ylabel. The plots are identical...

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(jaar incumbent kabinetspartij lag_aant_col_part relch_rd_led share_rd)
      1994 1 0 2          0  6.666667
      1998 0 1 3          0  6.666667
      2002 0 1 2          0  6.666667
      2006 0 1 2          0  6.666667
      2010 0 0 2          0  6.666667
      2014 0 1 2       -100  6.666667
      1994 0 0 2          0  6.666667
      1998 0 1 2          0  6.666667
      2002 0 1 3        100  6.666667
      2006 0 1 3        -50 13.333333
      2010 0 0 3          0  6.666667
      2014 0 1 2          0  6.666667
      1994 0 0 1          0 15.384615
      1998 1 1 2   50.00001 15.384615
      2002 0 1 2          0 23.076923
      2006 0 1 2  -66.66667 23.076923
      2010 0 0 2        100  7.692307
      2014 1 1 3          0 15.384615
      1994 0 0 2          0 15.384615
      1998 0 1 2        -50 15.384615
      2002 0 1 2        100  7.692307
      2006 0 1 2        -50 15.384615
      1998 1 1 3  33.333332 13.043478
      2002 1 1 3  -45.23809 17.391304
      2006 1 1 5          0  9.523809
      2010 1 0 3         50  9.523809
      2014 0 1 5          0 14.285714
      2018 1 1 4  10.526316 14.285714
      1994 0 0 3   49.99999  10.25641
      1998 0 1 3          0 15.384615
      2002 1 1 4          0 15.384615
      2006 1 1 4 -16.666664 15.384615
      2010 0 0 3  19.999996 12.820513
      2014 0 1 4        -50 15.384615
      1998 0 1 2 -33.333336        20
      2002 0 1 2          0 13.333333
      2006 1 1 3        -50 13.333333
      2010 1 0 2        100  6.666667
      2014 1 1 3          0 13.333333
      1994 1 0 3         25  23.52941
      1998 1 1 3          0 29.411764
      2002 1 1 3          0 29.411764
      2006 1 1 3        -20 29.411764
      2010 1 0 3          0  23.52941
      2014 1 1 3        -50  23.52941
      1994 0 0 3  33.333332 13.043478
      1998 1 1 4        -25 17.391304
      2002 1 1 4  33.333332 13.043478
      2006 1 1 4        -50 17.391304
      2010 0 0 3         50  8.695652
      2014 0 1 4          0 13.043478
      1994 0 0 3          0 11.764706
      1998 0 1 3          0 11.764706
      2002 0 1 3        100 11.764706
      2006 1 1 3        -25  23.52941
      2010 1 0 3          0 17.647058
      2014 1 1 3          0 17.647058
      1998 0 1 1          0  6.666667
      2002 0 1 2        100  6.666667
      2006 1 1 3        -50 13.333333
      2010 0 0 3          0  6.666667
      2014 0 1 3          0  6.666667
      2018 0 1 3  15.384615  6.666667
      1994 0 0 2          0 23.076923
      1998 0 1 2          0 23.076923
      2002 0 1 2  15.555552 23.076923
      2006 0 1 2          0 26.666666
      2010 0 0 2          0 26.666666
      2014 1 1 2        -25 26.666666
      1994 1 0 3  27.413124  21.21212
      1998 1 1 4   13.84615 27.027027
      2002 1 1 3  -33.33333  30.76923
      2006 0 1 4 -12.500007  20.51282
      2010 1 0 3          0 17.948717
      2014 1 1 3 -28.571426 17.948717
      2018 1 1 5   38.66668 12.820513
      1994 0 0 2          0  8.695652
      1998 0 1 2         50  8.695652
      2002 0 1 2 -33.333332 13.043478
      2006 0 1 3          0  8.695652
      2010 1 0 3          0  8.695652
      2014 0 1 2        -50  8.695652
      2018 0 1 4        100  4.347826
      1998 1 1 2          0 13.333333
      2002 1 1 3          0 13.333333
      2006 0 1 3          0 13.333333
      2010 1 0 3  32.352943 13.333333
      2014 1 1 3 -33.333332 17.647058
      1994 0 0 2         50  9.523809
      1998 1 1 3  33.333332 14.285714
      2002 1 1 2        -25 19.047619
      2006 1 1 4 -33.333332 14.285714
      2010 0 0 2          0  9.523809
      2014 1 1 3        -50  9.523809
      2018 1 1 4          0 4.7619047
      1994 0 0 2         50 11.764706
      1998 1 1 3          0 17.647058
      2002 1 1 2          0 17.647058
      2006 0 1 3 -33.333332 17.647058
      2010 1 0 3          0 11.764706
      end
      reg       relch_rd_led i.incumbent##c.share_rd##c.lag_aant_col_part
      margins, dydx(incumbent) at(share_rd=(0(15)30))
      marginsplot,                                                                                                   ///
              yline(0)                                                                                               ///
              graphregion(color(white))                                                                              ///
              title("Effect van collegedeelname naar aandeel collegepartijen in raad")                               ///
              subtitle("Marginaal effect van collegedeelname op procentuele verandering in aandeel raadszetels", size(small)) ///
              recast(line)                                                                                           ///
              recastci(rarea)                                                                                        ///
              ci1opt(fcolor(midblue%40) lcolor(midblue))                                                             ///
              ytitle("Marginaal effect" "met 95% betrouwbaarheidsinterval", size(small))                             ///
              legend(off)                                                                                            ///
              note("Gecontroleerd voor effect van kabinetsdeelname, aantal collegepartijen en verkiezingsjaar")      ///
              addplot(histogram share_rd if share_rd &lt;=50,                                                           ///
                 frequency                                                                                           ///
                 xtitle("Aandeel collegepartijen in raadszetels (%)", size(small))                                   ///
                 xlabel(0(5)30, labsize(small))                                                                      ///
                 yaxis(2)                                                                                            ///
                 ytitle("Aantal waarnemingen (staaf)", axis(2) size(small))                                          ///
                 ylabel(#6, axis(2) format(%8.0g) labsize(small))                                                    ///
                 ysc(axis(2) alt)                                                                                    ///
                 fcolor(teal%30)                                                                                     ///
                 lcolor(teal%30))
      marginsplot,                                                                                                   ///
              yline(0)                                                                                               ///
              graphregion(color(white))                                                                              ///
              title("Effect van collegedeelname naar aandeel collegepartijen in raad")                               ///
              subtitle("Marginaal effect van collegedeelname op procentuele verandering in aandeel raadszetels", size(small)) ///
              recast(line)                                                                                           ///
              recastci(rarea)                                                                                        ///
              ci1opt(fcolor(midblue%40) lcolor(midblue))                                                             ///
              ytitle("Marginaal effect" "met 95% betrouwbaarheidsinterval", size(small))                             ///
              ylabel(-50(25)150, labsize(small))                                                                     ///
              yscale(r(-50(25)150))                                                                                  ///
              legend(off)                                                                                            ///
              note("Gecontroleerd voor effect van kabinetsdeelname, aantal collegepartijen en verkiezingsjaar")      ///
              addplot(histogram share_rd if share_rd &lt;=50,                                                           ///
                 frequency                                                                                           ///
                 xtitle("Aandeel collegepartijen in raadszetels (%)", size(small))                                   ///
                 xlabel(0(5)30, labsize(small))                                                                      ///
                 yaxis(2)                                                                                            ///
                 ytitle("Aantal waarnemingen (staaf)", axis(2) size(small))                                          ///
                 ylabel(#6, axis(2) format(%8.0g) labsize(small))                                                    ///
                 ysc(axis(2) alt)                                                                                    ///
                 fcolor(teal%30)                                                                                     ///
                 lcolor(teal%30))
      Click image for larger version

Name:	Example.png
Views:	1
Size:	41.7 KB
ID:	1566443

      Comment


      • #4
        Works for me.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(jaar incumbent kabinetspartij lag_aant_col_part relch_rd_led share_rd)
        1994 1 0 2          0  6.666667
        1998 0 1 3          0  6.666667
        2002 0 1 2          0  6.666667
        2006 0 1 2          0  6.666667
        2010 0 0 2          0  6.666667
        2014 0 1 2       -100  6.666667
        1994 0 0 2          0  6.666667
        1998 0 1 2          0  6.666667
        2002 0 1 3        100  6.666667
        2006 0 1 3        -50 13.333333
        2010 0 0 3          0  6.666667
        2014 0 1 2          0  6.666667
        1994 0 0 1          0 15.384615
        1998 1 1 2   50.00001 15.384615
        2002 0 1 2          0 23.076923
        2006 0 1 2  -66.66667 23.076923
        2010 0 0 2        100  7.692307
        2014 1 1 3          0 15.384615
        1994 0 0 2          0 15.384615
        1998 0 1 2        -50 15.384615
        2002 0 1 2        100  7.692307
        2006 0 1 2        -50 15.384615
        1998 1 1 3  33.333332 13.043478
        2002 1 1 3  -45.23809 17.391304
        2006 1 1 5          0  9.523809
        2010 1 0 3         50  9.523809
        2014 0 1 5          0 14.285714
        2018 1 1 4  10.526316 14.285714
        1994 0 0 3   49.99999  10.25641
        1998 0 1 3          0 15.384615
        2002 1 1 4          0 15.384615
        2006 1 1 4 -16.666664 15.384615
        2010 0 0 3  19.999996 12.820513
        2014 0 1 4        -50 15.384615
        1998 0 1 2 -33.333336        20
        2002 0 1 2          0 13.333333
        2006 1 1 3        -50 13.333333
        2010 1 0 2        100  6.666667
        2014 1 1 3          0 13.333333
        1994 1 0 3         25  23.52941
        1998 1 1 3          0 29.411764
        2002 1 1 3          0 29.411764
        2006 1 1 3        -20 29.411764
        2010 1 0 3          0  23.52941
        2014 1 1 3        -50  23.52941
        1994 0 0 3  33.333332 13.043478
        1998 1 1 4        -25 17.391304
        2002 1 1 4  33.333332 13.043478
        2006 1 1 4        -50 17.391304
        2010 0 0 3         50  8.695652
        2014 0 1 4          0 13.043478
        1994 0 0 3          0 11.764706
        1998 0 1 3          0 11.764706
        2002 0 1 3        100 11.764706
        2006 1 1 3        -25  23.52941
        2010 1 0 3          0 17.647058
        2014 1 1 3          0 17.647058
        1998 0 1 1          0  6.666667
        2002 0 1 2        100  6.666667
        2006 1 1 3        -50 13.333333
        2010 0 0 3          0  6.666667
        2014 0 1 3          0  6.666667
        2018 0 1 3  15.384615  6.666667
        1994 0 0 2          0 23.076923
        1998 0 1 2          0 23.076923
        2002 0 1 2  15.555552 23.076923
        2006 0 1 2          0 26.666666
        2010 0 0 2          0 26.666666
        2014 1 1 2        -25 26.666666
        1994 1 0 3  27.413124  21.21212
        1998 1 1 4   13.84615 27.027027
        2002 1 1 3  -33.33333  30.76923
        2006 0 1 4 -12.500007  20.51282
        2010 1 0 3          0 17.948717
        2014 1 1 3 -28.571426 17.948717
        2018 1 1 5   38.66668 12.820513
        1994 0 0 2          0  8.695652
        1998 0 1 2         50  8.695652
        2002 0 1 2 -33.333332 13.043478
        2006 0 1 3          0  8.695652
        2010 1 0 3          0  8.695652
        2014 0 1 2        -50  8.695652
        2018 0 1 4        100  4.347826
        1998 1 1 2          0 13.333333
        2002 1 1 3          0 13.333333
        2006 0 1 3          0 13.333333
        2010 1 0 3  32.352943 13.333333
        2014 1 1 3 -33.333332 17.647058
        1994 0 0 2         50  9.523809
        1998 1 1 3  33.333332 14.285714
        2002 1 1 2        -25 19.047619
        2006 1 1 4 -33.333332 14.285714
        2010 0 0 2          0  9.523809
        2014 1 1 3        -50  9.523809
        2018 1 1 4          0 4.7619047
        1994 0 0 2         50 11.764706
        1998 1 1 3          0 17.647058
        2002 1 1 2          0 17.647058
        2006 0 1 3 -33.333332 17.647058
        2010 1 0 3          0 11.764706
        end
        reg       relch_rd_led i.incumbent##c.share_rd##c.lag_aant_col_part
        margins, dydx(incumbent) at(share_rd=(0(15)30))
        
        marginsplot,                                                                                                   ///
                yline(0)                                                                                               ///
                graphregion(color(white))                                                                              ///
                title("Effect van collegedeelname naar aandeel collegepartijen in raad")                               ///
                subtitle("Marginaal effect van collegedeelname op procentuele verandering in aandeel raadszetels", size(small)) ///
                recast(line)                                                                                           ///
                recastci(rarea)                                                                                        ///
                ci1opt(fcolor(midblue%40) lcolor(midblue))                                                             ///
                ytitle("Marginaal effect" "met 95% betrouwbaarheidsinterval", size(small))                             ///
                legend(off)                                                                                            ///
                note("Gecontroleerd voor effect van kabinetsdeelname, aantal collegepartijen en verkiezingsjaar")      ///
                addplot(histogram share_rd if share_rd <=50,                                                           ///
                   frequency        yscale(r(-100 150) axis(1)) ylab(0(4)20, axis(2)) ylab(-100(50)150)                ///
                   xtitle("Aandeel collegepartijen in raadszetels (%)", size(small))                                   ///
                   xlabel(0(5)30, labsize(small))                                                                      ///
                   yaxis(2)                                                                                            ///
                   ytitle("Aantal waarnemingen (staaf)", axis(2) size(small))                                          ///
                   ysc(axis(2) alt)                                                                                    ///
                   fcolor(teal%30)                                                                                     ///
                   lcolor(teal%30))

        Click image for larger version

Name:	Graph.png
Views:	1
Size:	71.1 KB
ID:	1566623

        Last edited by Andrew Musau; 02 Aug 2020, 14:58.

        Comment


        • #5
          Thank you Andrew! It works for me too.
          So, the scale for the basic marginplot needs to be adjusted within the addplot environment, where a histogram is added. I wasn't aware of that.
          Problem solved. Thank you very much for your time.

          Comment

          Working...
          X