Announcement

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

  • graph options - using scale(range) and rescale together

    Hi there

    I wonder if anyone could help with this problem?

    Using nonsensical data for illustration, I would like to be able rescale my x-axis for the different groups AND show a vertical line through a fixed value (i.e. 40), but Stata won't let me do both:

    Code:
    use https://stats.idre.ucla.edu/stat/stata/notes/hsb2, clear
    replace write = write-40 if female == 0
    twoway (scatter read write), by(ses female, xrescale cols(2)) xline(40) xscale(range(40))
    Any advice very much appreciated!

    With thanks

  • #2
    Not sure if there's a way to force a range on a rescaled axis, but you can always achieve the same effect using graph combine (albeit a little less efficiently):

    Code:
    clear all
    use https://stats.idre.ucla.edu/stat/stata/notes/hsb2
    replace write = write-40 if female == 0
    sort female ses
    levelsof ses,local(ses)
    levelsof female,local(female)
    foreach y of local female{
        foreach x of local ses{
            twoway scatter read write if ses==`x' & female==`y', xla(40,add) xscale(range(40)) xline(40) name(gr`x'`y') ytitle("") xtitle("") subtitle("`:label sl `x'', `:label fl `y''")
            local comb `comb' gr`x'`y'
        }
    }
    gr combine `comb', b1title("writing score") l1title("reading score") cols(2) rows(3) colfirst
    Producing:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	90.2 KB
ID:	1604645

    Comment


    • #3
      With twoway -by()-, you can use -dropline- in place of -xline-.

      Code:
      use https://stats.idre.ucla.edu/stat/stata/notes/hsb2, clear
      replace write = write-40 if female == 0
      qui sum read
      gen y= r(max)+ (0.1*r(max))
      gen x=40
      twoway (scatter read write) (dropline y x, lcolor(red) lw(thin) mcolor(none)), ///
      by(ses female, xrescale cols(2)) plotregion(margin(zero)) ///
      xscale(range(40)) leg(order(1))

      Comment


      • #4
        I did not quite get what you want, but maybe this does the job for you.
        Code:
        use https://stats.idre.ucla.edu/stat/stata/notes/hsb2, clear
        replace write = write-40 if female == 0
        
        loc xscale "-10 (10) 70"
        twoway (scatter read write), by(ses female, xrescale cols(2)) ///
        xlab(`xscale') xline(40, lstyle(grid) lcolor(yblue) lp(longdash_dot))
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	78.2 KB
ID:	1604687

        Comment


        • #5
          Extremely helpful, thank you to all.

          Comment

          Working...
          X