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")
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")
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)))
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
Retain Attributes When Using Gather from Tidyr (Attributes Are Not Identical)
Calculate Summary Statistics (E.G. Mean) on All Numeric Columns Using Data.Table
Plot Negative Values in Logarithmic Scale with Ggplot 2
R: Ggplot2 Make Two Geom_Tile Plots Have Equal Height
Warning: Replacing Previous Import 'Head' When Loading 'Utils' in R
Adding a Table of Values Below the Graph in Ggplot2
Plot Linear Regressions Lines Without Interaction in Ggplot2
Order of Dates Is Not Chronological in Ggplot2
How to Create a Variable of Rownames
How to Extract Multiples of a Number from a Vector
R Histogram with Multiple Populations
How to Include Custom CSS in HTMLwidgets for R And/Or Leafletr
Installing R on Osx Big Sur (Edit: and Apple M1) for Use with Rcpp and Openmp
How to Move the Bibliography in Markdown/Pandoc
Subtract Every Column from Each Other Column in a R Data.Table