0
votes

I often make graphs that plot a mean or coefficient with a 95% error bar using -twoway scatter- and -twoway rcap-. The code below produces a legend with two entries: one for the mean marker symbol and one for the error bar. But I want the legend to display a single entry, showing the marker symbol and the error bar combined. Below is an example of how I usually make a graph.

sysuse auto

gen b = .
gen se = .

mean mpg if foreign == 1
replace b = _b[mpg] in 1
replace se = _se[mpg] in 1

mean mpg if foreign == 0
replace b = _b[mpg] in 2
replace se = _se[mpg] in 2

gen lb = b - (1.96 * se)
gen ub = b + (1.96 * se)
gen index = _n in 1/2

twoway scatter b index || rcap lb ub index, legend(order(1 "Mean" 2 "95% Interval"))

Is there an option in -legend- to allow me to overlay two legend entries in the way I want?

1

1 Answers

2
votes

I don't really know how to do exactly what you want. It seems hard.

I also hate to waste the legend real estate, so one alternative is to label the means instead of using a legend (and add "With 95%CIs" to the title):

sysuse auto
reg mpg i.foreign
margins foreign, post
estimates store means

marginsplot, recast(scatter) xscale(reverse)
coefplot means

Another is to just use ciplot without any regression/summarization:

ciplot mpg, by(foreign) xscale(reverse)

coefplot and ciplot are both user-written.