Reputation: 13
I'm trying to plot the geyser data from the R library MASS and add onto it a curve representing the mixture density f(x)=1/3\phi_1(x) + 2/3\phi_2(x) where \phi_1(x) and \phi_2(x) are the normal densities with standard deviation 7 and means 52 and 80 respectively.
I was able to plot it with hist() and line(), but how do I do the same thing in ggplot?
library(MASS)
data("geyser")
hist(geyser$waiting, breaks = 25, col = "red", freq = F,
xlab = "Time", ylab = "Frequency", main = "Waiting Time For Eruption")
x.seq <- seq(40, 110, length.out = 100)
pdf <- dnorm(x.seq, mean = 52, sd = 7)/3 + 2*dnorm(x.seq, mean = 80, sd = 7)/3
lines(pdf~x.seq, col = "black")
Upvotes: 0
Views: 257
Reputation: 426
You can use something like
library(ggplot2)
df_pdf = data.frame(x = x.seq, y = pdf)
ggplot(geyser) +
geom_histogram(aes(x = waiting, y = ..density..), binwidth = 5) +
geom_line(data = df_pdf, aes(x = x, y = y), color = "red")
Upvotes: 1