Hello Listers,
I hope this is not redundant but I've encountered a problem for which I could not find a solution, yet.
Here is what I want to do:
I want a kdensity plot and split it up by vertical lines that mark the quartiles of the distribution - so far so good.
But here comes where I get stuck:
I would like the area under the density plot to be shaded in different colors for each quartile, i.e. a different color for different intervals on the x-axis.
I thought about using multiple kdensity plots with if-conditions for the xaxis, but of course then Stata will plot the density within the quartiles rather just a subinterval of the overall distribution (see Second try in code) -- this is not what I am after.
The desired end result should look something like this:

I made a little toy example here:
I hope this is not redundant but I've encountered a problem for which I could not find a solution, yet.
Here is what I want to do:
I want a kdensity plot and split it up by vertical lines that mark the quartiles of the distribution - so far so good.
But here comes where I get stuck:
I would like the area under the density plot to be shaded in different colors for each quartile, i.e. a different color for different intervals on the x-axis.
I thought about using multiple kdensity plots with if-conditions for the xaxis, but of course then Stata will plot the density within the quartiles rather just a subinterval of the overall distribution (see Second try in code) -- this is not what I am after.
The desired end result should look something like this:
I made a little toy example here:
Code:
sysuse auto, clear sum weight, d local min = r(min) local p25 = r(p25) local p50 = r(p50) local p75 = r(p75) local max = r(max) local g1 = `min' + (`p25'-`min')/2 local g2 = `p25' + (`p50'-`p25')/2 local g3 = `p50' + (`p75'-`p50')/2 local g4 = `p75' + (`max'-`p75')/2 * First try: #delimit ; twoway (kdensity weight, recast(area) col(%50)) (scatteri 0.00015 `g1' "G1", msym(none) mlabpos(12)) (scatteri 0.00015 `g2' "G2", msym(none) mlabpos(12)) (scatteri 0.00015 `g3' "G3", msym(none) mlabpos(12)) (scatteri 0.00015 4000 "G4", msym(none) mlabpos(12)) , xline(`p25' `p50' `p75') legend(off) name(first_try,replace) ; #delimit ct * Second try: #delimit ; twoway (kdensity weight, recast(area) col(%50)) (kdensity weight if inrange(weight, `min',`p25'), recast(area) col(%50)) (kdensity weight if inrange(weight, `p25',`p50'), recast(area) col(%50)) (scatteri 0.00015 `g1' "G1", msym(none) mlabpos(12)) (scatteri 0.00015 `g2' "G2", msym(none) mlabpos(12)) (scatteri 0.00015 `g3' "G3", msym(none) mlabpos(12)) (scatteri 0.00015 4000 "G4", msym(none) mlabpos(12)) , xline(`p25' `p50' `p75') legend(off) name(second_try,replace) ; #delimit ct
Comment