I am trying to create a plot that has a mean values over time, and associated confidence intervals (CIs), for two groups (foreign==0 and foreign==1) using twoway scatter and rcap. However, it is difficult to distinguish between the CIs when they overlap, even if I use different colours or line styles.
I tried to use the jitter option to randomly offset the points on the plot. While this works for scatter, it does not appear to work for rcap, which I am using to plot the CIs. It accepts the option, there is no error, but it does not jitter the CIs. I had hoped that by using the jitterseed option with the same seed, I would be able to offset both the marker from scatter and the CIs from rcap to the same location on the plot.
This post on Statalist from 2005 suggests that rcap does not support jitter. I can't find reference to the jitter option in the current rcap documentation, so I assume this is still the case. I am open to solutions that plot CIs around means using a plotting command other than rcap. TIA.
Reproducible example:
sysuse auto, clear
* Generate required statistics by repair record and foreign
collapse (mean) mean_price = price ///
(sd) sd_price = price ///
(count) n_price = price , by(foreign rep78)
* Compute confidence intervals
gen lb_price = mean_price - invttail(n_price-1,0.025)*(sd_price / sqrt(n_price))
gen ub_price = mean_price + invttail(n_price-1,0.025)*(sd_price / sqrt(n_price))
* No jitter
twoway (scatter mean_price rep78 if foreign == 0, ///
c(L) lcol(black) msym(O) mcol(black) ) ///
///
(scatter mean_price rep78 if foreign == 1, ///
c(L) lcol(black) msym(O) mcol(black) ) ///
///
(rcap lb_price ub_price rep78 if foreign == 0, ///
lcol(black) ) ///
///
(rcap lb_price ub_price rep78 if foreign == 1, ///
lcol(black) )
* With jitter
twoway (scatter mean_price rep78 if foreign == 0, ///
c(L) lcol(black) msym(O) mcol(black) jitter(10) jitterseed(123)) ///
///
(scatter mean_price rep78 if foreign == 1, ///
c(L) lcol(black) msym(O) mcol(black) jitter(10) jitterseed(456)) ///
///
(rcap lb_price ub_price rep78 if foreign == 0, ///
lcol(black) jitter(10) jitterseed(123)) ///
///
(rcap lb_price ub_price rep78 if foreign == 1, ///
lcol(black) jitter(10) jitterseed(456))
