I'm trying to add a legend to ggplot to differentiate between a simulated normal distribution and a generated one. Below is my code
set.seed(1)
lambda = .2
n = 40
sim = 10000
means = replicate(sim, expr = mean(rexp(n,lambda)))
ggplot(data.frame(means), aes(x=means)) +
geom_density() +
stat_function(fun = dnorm, color = "blue",
arg = list(mean = 1/lambda, sd=sqrt(lambda^-2/n))) +
scale_colour_manual("Legend title", values = c("red", "blue"))
I tried using scale_colour_manual as given another stackoverflow answer but I can't get a legend to show up.
Reference answer Using legend with stat_function in ggplot2