Question

I have the following twoway scatter and I am looking to plot two ylines and have them start and stop at set x-values.

clear all
set more off

sysuse auto

twoway scatter  mpg weight, /// 
    connect(l) sort ///
    xline(2500) ///
    yline(25)

I want an xline at 2500 and two yline one from 1500 to 2800 and the other xline from 2900 to 4100. I also want the data points to connect.

Was it helpful?

Solution 2

One hack would be to use auxiliary variables:

clear all
set more off

sysuse auto

*----- first yline -----

gen xli = .
replace xli = 1500 in 1
replace xli = 2800 in l

gen yli = 25

*----- second yline -----

gen xxli = .
replace xxli = 2900 in 1
replace xxli = 4100 in l

gen yyli = 20

*----- first xline -----

gen yyyli = .
replace yyyli = 15 in 1
replace yyyli = 40 in l

gen xxxli = 2500

*----- graph -----

twoway scatter  mpg weight, connect(l) sort || ///
    line yli xli || line yyli xxli /// ylines
        || line yyyli xxxli // xline

I doubt it's the best technique, but it seems to work. You would need to work at least on the legend and line colors. help <command> and the Stata manuals have a lot on that (and much more).

OTHER TIPS

Consider this:

sysuse auto 
scatter mpg weight, connect(l) sort || scatteri 25 1500 25 2800, recast(line)

scatteri by default just adds points at the coordinates mentioned, i.e. 25 1500 and 25 2800 which are (y, x) pairs following scatter and twoway convention that the y variable is named first.

The recast() option recasts the scatter as a line graph. To get separate line segments, add separate scatteri calls.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top