I'm trying to create a bar plot using the twoway command with scatterplots "behind" the bar plots. I have tried to use the opacity option in the bar plots so that the scatter can be seen behind the bars, but even with very large amounts of opacity the data points are largely occluded. I am using Stata version 16.
To get an idea of what I mean, the code below sets opacity at a 25% fill, and produces the attached graph. As you can see, the data points are still not visible behind the bar plots. When I use the opacity option for other graphs, I haven't had an issue — it seems to be specific to graphs produced in twoway.
I realize I could rearrange the overlay order so that the data points fall "on top" of the bar chart, but would prefer not to do that for aesthetic reasons. Any suggestions would be much appreciated!
Code:
sysuse auto, clear // generating means and standard errors sum price if foreign == 0 gen mean = r(mean) if foreign == 0 gen ub = r(mean) + r(sd)/sqrt(r(N)) if foreign == 0 gen lb = r(mean) - r(sd)/sqrt(r(N)) if foreign == 0 sum price if foreign == 1 replace mean = r(mean) if foreign == 1 replace ub = r(mean) + r(sd)/sqrt(r(N)) if foreign == 1 replace lb = r(mean) - r(sd)/sqrt(r(N)) if foreign == 1 // graph twoway (scatter price foreign if foreign == 0, mcolor(orange) jitter(5) msize(tiny)) /// (scatter price foreign if foreign == 1, mcolor(red) jitter(5) msize(tiny)) /// (bar mean foreign if foreign == 0, bcolor(orange%25) barwidth(.8)) /// (bar mean foreign if foreign == 1, bcolor(red%25) barwidth(.8)) /// (rcap ub lb foreign if foreign == 0, lcolor(black) msize(0) lwidth(medium)) /// (rcap ub lb foreign if foreign == 1, lcolor(black) msize(0) lwidth(medium)), /// legend(off) xlabel(0 "Domestic" 1 "Foreign", labsize(medlarge) labcolor(black)) graphregion(color(white) lwidth(medium)) ytitle("Price") xsize(3) ysize(4) ylab(, nogrid)
0 Response to twoway scatter plot with bar overlay — opacity issues
Post a Comment