0

I would like to plot a normal curve over top of some data that has been plotted in a histogram format. I thought this would be relatively straightforward. So far I have:

x = rnorm(2000, mean=-5, sd=4)

a_hist = ggplot() + 
  geom_histogram(aes(x), fill='darkred') +
  stat_function(fun=dnorm, args(list(mean=-5, 4)))

This gets me everything except the normal curve. I'm guessing it's because of the axes but when I tried to mess around with the mean/SD/counts it still didn't show. I also thought it might be because of the aes mappings. But ggplot wouldn't allow me to put the data into the base ggplot call since its a vector and not a dataframe. Trying to coerce it into a data frame and passing it in as such gave me other errors. I explored the stat_function() parameter position but couldn't find any documentation on how to use it other than 'it takes a string'.

DChaps
  • 482
  • 5
  • 12
  • Does this answer your question? [Overlay normal curve to histogram in R](https://stackoverflow.com/questions/20078107/overlay-normal-curve-to-histogram-in-r) – Limey Jun 05 '20 at 07:26
  • Unfortunately, that's in base R. I tried to mimic a couple of their solutions but no luck. – DChaps Jun 06 '20 at 01:19

1 Answers1

0

There are two ways to get these overlays. Either you scale histogram to density or you scale density to count scale. Here are two examples:

Scaling histogram to density

No prior knowledge needed, except from the dnorm() arguments.

library(ggplot2)

n <- 2000
x = rnorm(n, mean=-5, sd=4)

ggplot() + 
  geom_histogram(aes(x, y = after_stat(density)), fill='darkred') +
  stat_function(fun=dnorm, args= list(mean=-5, sd = 4))

Scaling density to counts

Need to know the number of observations n and set a known binwidth.

binwidth <- 1
ggplot() + 
  geom_histogram(aes(x), fill='darkred', binwidth = binwidth) +
  stat_function(fun=dnorm, args= list(mean=-5, sd = 4),
                aes(y = after_stat(y * binwidth * n)))
teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Unfortunately didn't work. My histogram is now in a more logical density plot but there's still no curve overlayed. I tried changing the color of the curve and making it geom="points" to see if it was hiding. No luck. – DChaps Jun 06 '20 at 01:37
  • What version of ggplot2 are you running? Code above assumes v3.3.0 – teunbrand Jun 06 '20 at 19:44
  • Yep. I went and updated everything to make sure. R, ggplot, and all other packages up to date. – DChaps Jun 07 '20 at 03:20