Re: [R] Superimposing Line over Histogram in Density Plot

From: Carl Witthoft <>
Date: Mon, 16 Jun 2008 17:20:42 -0400

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).


try something like this:

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

I hope it helps.


> 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__
> mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 16 Jun 2008 - 21:38:35 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 16 Jun 2008 - 22:30:50 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive