I recently had the need to put a single dot of a different color on a graph, and noted that stata doesn't have an option to allow you to place a single dot on a graph. You can put a line using xline and yline options (see below). I came up with the following work around:

sysuse auto

sum mpg

local avgmpg : display r(mean)

gen avgmpg = `avgmpg'

sum price

local avgprice : display r(mean)

gen avgprice = `avgprice'

twoway (scatter mpg price) ///

(scatter avgmpg price if _n==1, mcolor(red) legend(off) ///

text(`avgmpg' `avgprice' "average"))

What I did was create the average price and mpg as varabiles, then drew my primary scatter plot and then overlaid a single dot. The restriction if _n==1 tells stata to only use observation number one. This prevents me from having _N (total number of observations in the data set) dots all right on top of each other, which will use up more resources. The text option just allows me to place text at a given location.

If I wanted to intersecting lines at the averages, I would have done

twoway (scatter mpg price, xline(`avgprice') yline(`avgmpg'))

Happy Coding!

## Thursday, May 13, 2010

Subscribe to:
Post Comments (Atom)

-twoway scatteri- seems to achieve the same.

ReplyDeleteCheers

Daniel

Thanks @Sean for the code and also @Daniel for the hint to scatteri.

ReplyDeleteRoberto