Possible Duplicate:
Shading a kernel density plot between two points
I want to produce a density plot with certain portion highlighted..
myd <- c(2,4,5, 4,3,2,2,3,3,3,2,3,3,4,2,4,3,3,3,2,2.5, 2, 3,3, 2.3, 3, 3, 2, 3)
I want to create density plot and color, the portion greater than 3 to end of the curve .
My unsuccessful codes
plot(density(myd),xlim=c(0,5))
polygon(c(3, myd, max(myd)),c(0,density(myd),0.6),col="tan")
polygon(c(3, myd, max(myd)),c(0,density(myd),0.6),col="tan")
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
polygon(c(3, myd, max(myd)),c(0,myd,0.6),col="tan")
produce a graph, obviously but awful, not closer to what I need !
You need to use the values from your density, not your original data.
That’s close but since the default plot parameters go out beyond 5 you should select values maybe out to 6. The default plot behaviour will be to just clip. You also might be best off plotting without a box, or x-axis and then placing the x-axis at 0. That way you’ll get something more like what you might expect. Try this…