Medical physicist
Medical physicist

Reputation: 2594

Superimpose a normal distribution to a density using ggplot in R

I am trying to superimpose a normal distribution to a density using ggplot in R:

ggplot(Data, aes(x=Rel, y=..density..)) +
    geom_density(aes(fill=factor(cut)), position="stack") +
    stat_function(fun = dnorm, args = list(mean = Rel.mean, sd = Rel.sd))

But I keep getting this error:

Error in eval(expr, envir, enclos) : object 'density' not found
Calls: print ... <Anonymous> -> as.data.frame -> lapply -> FUN -> eval

Why? Any solution?

Upvotes: 4

Views: 8969

Answers (1)

Medical physicist
Medical physicist

Reputation: 2594

Following @aosmith advice:

ggplot(Data, aes(x=Rel)) +
    geom_density(aes(y=..density.., fill=factor(cut)), position="stack") +
    stat_function(fun = dnorm, args = list(mean = Rel.mean, sd = Rel.sd))

Works!

Upvotes: 6

Related Questions