-2

Possible Duplicate:
Fitting a density curve to a histogram in R

I'd like to plot on the same graph the histogram and various pdf's. I've tried for just one pdf with the following code (adopted from code I've found in the web):

hist(data, freq = FALSE, col = "grey", breaks = "FD")
.x <- seq(0, 0.1, length.out=100)
curve(dnorm(.x, mean=a, sd=b), col = 2, add = TRUE)

It gives me an error. Can you advise me? For multiple pdf's what's the trick? And I've observed that the histogram seems to be plot the density (on y-y axis) instead of the number of observations.... how can I change this?

Many thanks!

Community
  • 1
  • 1
jpcgandre
  • 1,487
  • 5
  • 31
  • 55
  • 2
    Can you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that demonstrates your question / problem? We would find it easier to answer. – Andrie Aug 22 '12 at 19:40
  • 2
    Also, whenever one says "it gives me an error" it's a good idea to post what the error is. – David Robinson Aug 22 '12 at 19:56
  • 1
    See this question: http://stackoverflow.com/questions/1497539/fitting-a-density-curve-to-a-histogram-in-r – Dirk Eddelbuettel Aug 22 '12 at 20:09
  • 1
    Suggest searching SO [r] postings with search term 'layout' if you want multiple plots on a page, or with terms "multiple pdfs" if that is your interest. In any case, please take more care in searching and writing questions before submitting. – IRTFM Aug 23 '12 at 00:04

2 Answers2

2

It plots the density instead of the frequency because you specified freq=FALSE. It is not very fair to complain about it doing exactly what you told it to do.

The curve function expects an expression involving x (not .x) and it does not require you to precompute the x values. You probably want something like:

a <- 5
b <- 2
hist( rnorm(100, a, b), freq=FALSE )
curve( dnorm(x,a,b), add=TRUE )

To head of your next question, if you specify freq=TRUE (or just leave it out for the default) and add the curve then the curve just runs along the bottom (that is the whole purpose of plotting the histogram as a density rather than frequencies). You can work around this by scaling the expression given to curve by the width of the bins and the number of total points:

out <- hist( rnorm(100, a, b) )
curve( dnorm(x,a,b)*100*diff(out$breaks[1:2]), add=TRUE )

Though personally the first option (density scale) without tickmark labels on the y-axis makes more sense to me.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
0
h<-hist(data, breaks="FD", col="red", xlab="xTitle", main="Normal pdf and     histogram")
xfit<-seq(min(data),max(data),length=100)
x.norm<-rnorm(n=100000, mean=a, sd=b)
yfit<-dnorm(xfit,mean=mean(x.norm),sd=sd(x.norm))
yfit <- yfit*diff(h$mids[1:2])*length(loose_All)
lines(xfit, yfit, col="blue", lwd=2)
jpcgandre
  • 1,487
  • 5
  • 31
  • 55
  • First question and last one are answered! What about if I want to add multiple pdfs in the same plot? Thanks – jpcgandre Aug 22 '12 at 22:21