[R] Superimposing Line over Histogram in Density Plot

Carl Witthoft carl at witthoft.com
Mon Jun 16 23:20:42 CEST 2008


I do it a bit differently:
Once you've got myhist, you can plot a line on the existing histogram 
plot with something like:

smoo<-spline(myhist$breaks[1:length(myhist$breaks)-1],myhist$counts)
lines(smoo$x,smoo$y,col='green')

You'll have to muck a bit with scale factors to make the spline curve 
"match up" with the histogram plot's actual x-axis values.
(actually, that snippet is part of my conversion from histogram data to 
a bar chart that "looks" like a histogram, with the spline curve overlaid).

Carl





try something like this:

x <- rnorm(200)
hist(x, col = "blue", freq = FALSE)
lines(density(x), col = "red", lwd = 2)

I hope it helps.

Best,
Dimitris


 >
 > Currently I have a density plot generated with this
 > snippet.
 >
 > Is there a way I can add a line curve on top of it?
 > I mean in one figure
 >
 >
 > __BEGIN__
 > myhist <- hist(x
 > col="blue",
 > main = "Density Plot",
 > xlab = "Exp Level",
 > )
 > __END__
 >
 >



More information about the R-help mailing list