Announcement

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

  • Scatter plot - how to move x axis to y=0?

    Hello, I'm just wondering if someone could help me.

    Using scatter t3wload Country1, mlabel(Country) I have made the following graph below. I am trying to move the x axis so that it goes through y=0 instead of y=-0.50 but so far have had no success!

    Can anyone advise?

    Many thanks.
    Click image for larger version

Name:	Graph1.png
Views:	1
Size:	39.5 KB
ID:	1548082



  • #2
    You can put a line at y = 0, using yline(0). and specify its colour, thickness, and pattern. Unlike say MS Excel, Stata deliberately makes it difficult to mess up your graph otherwise.

    Comment


    • #3
      Though I agree with Nick that yline(0) and possibly grid lines would be clearer, here is a way using -scatteri-:

      Code:
      sysuse auto,clear
      sum price
      gen p2  = price -r(mean)
      scatter p2 mpg,  yline(0, lp(solid) lc(black)) xscale(off)  /// 
       || scatteri 0 10 (6) "10" 0 20 (6) "20" 0 30 (6) "30" 0 40 (6) "40", /// 
       ms(|) mlc(black) mlabcolor(black) mlwidth(medthick) msize(*1.5)

      Comment


      • #4
        Hello. I thought I would share to this to hopefully help someone else in the future as I spent some time trying to figure this out. You can shift the axes to center on 0 using "plotregion(margin(0))":

        I have also added some made up code. Run each "scatter" line code to see how each changes the appearance of a graph. I would suggest you specify the range of both your axes (both major [using label] and minor [using mtick]) when using for your data as the "plotregion(margin(0))" can make your data intrude on the axes without.


        Code:
        clear
        
        input x y
        x y
        1 4
        3 7
        5 8
        7 9
        2 4
        3 6
        end
        
        scatter y x
        scatter y x, ylabel(0(2)10) xlabel(0(2)8)
        scatter y x, plotregion(margin(0))
        scatter y x, ylabel(0(2)10) xlabel(0(2)8) plotregion(margin(0))
        scatter y x, ylabel(0(2)10) ymtick(0(1)10) xlabel(0(2)8) xmtick(0(1)8) plotregion(margin(0))
        scatter y x, ylabel(2(2)10, angle(0) nogrid) ymtick(0(1)10) xlabel(0(2)8) xmtick(0(1)8) plotregion(margin(0))

        Comment


        • #5
          #4 OK, but the big issue is people wanting an axis at y = 0 when there are zero or negative values for y.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            #4 OK, but the big issue is people wanting an axis at y = 0 when there are zero or negative values for y.
            I see the that I misread the original question. Apologies. Hopefully my previous solution still helps someone else who needs it and stumbles upon it. Otherwise, Nick's and Scott's advice seems to work well. I created a second dataset and line of code to produce a graph that perhaps would have satisfied Hannah's request. I could not get the "pipe" marker option symbolstyle to work in Stata as suggested by Scott's coding ("ms(|)"), and I replaced it with a "+". Not sure if there is another way to get it to work.

            Code:
            clear
            
            input x y
            x y
            2 -0.4
            25 0.7
            35 0.2
            15 -0.3
            4 0.6
            28 -0.2
            45 0.9
            end
            
            scatter y x, yline(0, lw(medthick) lp(solid) lc(black)) xline(0, lw(medthick) lp(solid) lc(black)) xscale(r(0 53) off) ///
            ylabel(-1 -0.5 0.5 1, labsize(medium) angle(0) tposition(crossing) nogrid) ytitle("t3wload", size(medium))  /// 
            || scatteri -0.003 0 (7) "0" -0.003 10 (6) "10" -0.003 20 (6) "20" -0.003 30 (6) "30" -0.003 40 (6) "40" -0.003 50 (6) "50", /// 
            msymbol(+) mcolor(black) msize(*1.3) mlwidth(vvvthin) mlabsize(medium) mlabcolor(black) /// 
            || scatteri -1.01 26 (6) "Country1", msymbol(i) mlabsize(medium) mlabcolor(black) /// 
            plotregion(margin(0)) graphregion(margin(l+4 r+6 t+6 b+6) color(white)) legend(off)

            Comment


            • #7
              Thanks for your careful answer. You are right: much of the point of the forum is that answers can benefit others with a similar question.

              The pipe symbol was added in Stata 15. See https://www.stata.com/help.cgi?whatsnew14to15 #54 I guess you're using an earlier version: see https://www.statalist.org/forums/help#version

              Comment


              • #8
                Thanks for your careful answer. You are right: much of the point of the forum is that answers can benefit others with a similar question.

                The pipe symbol was added in Stata 15. See https://www.stata.com/help.cgi?whatsnew14to15 #54 I guess you're using an earlier version: see https://www.statalist.org/forums/help#version
                Thanks for the response. I am using Stata 13, and see that indicating which version one uses is important to these posts.

                Comment

                Working...
                X