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

From: Carl Witthoft <carl_at_witthoft.com>
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).

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__
>
>

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html 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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.