How to Add Gaussian Curve to Histogram Created with Qplot

How to add gaussian curve to histogram created with qplot?

Have you tried stat_function?

+ stat_function(fun = dnorm)

You'll probably want to plot the histograms using aes(y = ..density..) in order to plot the density values rather than the counts.

A lot of useful information can be found in this question, including some advice on plotting different normal curves on different facets.

Here are some examples:

dat <- data.frame(x = c(rnorm(100),rnorm(100,2,0.5)), 
a = rep(letters[1:2],each = 100))

Overlay a single normal density on each facet:

ggplot(data = dat,aes(x = x)) + 
facet_wrap(~a) +
geom_histogram(aes(y = ..density..)) +
stat_function(fun = dnorm, colour = "red")

Sample Image

From the question I linked to, create a separate data frame with the different normal curves:

grid <- with(dat, seq(min(x), max(x), length = 100))
normaldens <- ddply(dat, "a", function(df) {
data.frame(
predicted = grid,
density = dnorm(grid, mean(df$x), sd(df$x))
)
})

And plot them separately using geom_line:

ggplot(data = dat,aes(x = x)) + 
facet_wrap(~a) +
geom_histogram(aes(y = ..density..)) +
geom_line(data = normaldens, aes(x = predicted, y = density), colour = "red")

Sample Image

Overlay normal curve to histogram in ggplot2

I suspect that stat_function does indeed add the density of the normal distribution. But the y-axis range just let's it disappear all the way at the bottom of the plot. If you scale your histogram to a density with aes(x = dist, y=..density..) instead of absolute counts, your curve from dnorm should become visible.

(As a side note, your distribution does not look normal to me. You might want to check, e.g. with a qqplot)

library(ggplot2)

dist = data.frame(dist = rnorm(100))

plot1 <-ggplot(data = dist) +
geom_histogram(mapping = aes(x = dist, y=..density..), fill="steelblue", colour="black", binwidth = 1) +
ggtitle("Frequences") +
stat_function(fun = dnorm, args = list(mean = mean(dist$dist), sd = sd(dist$dist)))

Sample Image

Overlay normal curve to histogram in R

Here's a nice easy way I found:

h <- hist(g, breaks = 10, density = 10,
col = "lightgray", xlab = "Accuracy", main = "Overall")
xfit <- seq(min(g), max(g), length = 40)
yfit <- dnorm(xfit, mean = mean(g), sd = sd(g))
yfit <- yfit * diff(h$mids[1:2]) * length(g)

lines(xfit, yfit, col = "black", lwd = 2)


Related Topics



Leave a reply



Submit