Announcement

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

  • X-scale change not applied using code, but can be changed with graph editor

    I'm attempting to combine a line graph and a bar graph that need to line up along the x-axis. For that to work, the bar graph needs to start exactly at 0; however, the default seems to be to add a bit of space at the left and right sides of the x-axis.
    I specified the xscale range to start at 0, but that seems not to be respected.
    When I use the graph editor, I can correct the problem by selecting the x-axis, clicking on "more", selecting "scale", ticking the "Extend range of axis scale" and entering 0 and 18 to replace the default of -0.235 and 18.325 (see screenshot). Any ideas on how to make this happen without the graph editor? The code below replicates the problem.
    I am using Stata SE version 17.

    Click image for larger version

Name:	graph editor.png
Views:	1
Size:	301.1 KB
ID:	1629423


    Code:
    webuse mumps2.dta, clear
    gen panel=1 in 1/19
    replace panel=2 in 20/38
    drop if panel==.
    gen mo=_n-1 if panel==1
    replace mo=_n-20 if panel==2
    
    set scheme s1color
    twoway (bar mumps mo if panel==1, fcolor(eltblue) barwidth(.65)  ) ///
    (bar mumps mo if panel==2, fcolor(navy%75) barwidth(.65))///
    , xlabel(0(1)18) xscale(range(0 18)) legend(off)

  • #2
    Stata's principle is that you cannot truncate the axis, but you can expand it. From the labels in your example, it is clear that the first bar is centered at zero. Therefore, forcing the axis to start at zero implies that you truncate part of the bar. Considering this, you can estimate where the bar starts (methinks -0.3) and proceed from there. By the way, it is also possible to set the plot region margin to zero. Thanks for the reproducible example.

    Code:
    webuse mumps2.dta, clear
    gen panel=1 in 1/19
    replace panel=2 in 20/38
    drop if panel==.
    gen mo=_n-1 if panel==1
    replace mo=_n-20 if panel==2
    
    set scheme s1color
    twoway (bar mumps mo if panel==1, fcolor(eltblue) barwidth(.65)  ) ///
    (bar mumps mo if panel==2, fcolor(navy%75) barwidth(.65)) ///
    , xlabel(0(1)18) xscale(range(-0.3 18)) legend(off)  plotregion(margin(zero))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	51.9 KB
ID:	1629438

    Last edited by Andrew Musau; 28 Sep 2021, 13:26.

    Comment


    • #3
      Thank you so much, Andrew! The combination of those two changes fixed the problem and allowed the two graphs to line up perfectly.

      I am still curious about why I was able to set the xscale range to 0 to 18 using the graph editor, but not able to do that with code. The actual range of the data is 0 to 18, so I wasn't attempting to truncate the axis in that sense, and indeed, the graph editor was able to do what I expected.

      Comment


      • #4
        You can see that clicking -extend range of axis scale- fixes the lower range at -0.325 and the upper range at 18.325. From #2, as we cannot truncate the axis but can expand the axis, fixing the upper range at 18 does nothing. But basically, what graph editor does in code corresponds to the option

        Code:
         xscale(range(-0.325 18.325))

        Comment

        Working...
        X