Reputation: 169
I have the following data frame
x1<-data.frame(n = rnorm(1000000, mean=0, sd=1), nombre= "x1")
x2<-data.frame(n=rnorm(1500000, mean=3, sd=1), nombre= "x2")
x<-rbind(x1, x2)
ggplot(x, aes(n, fill=nombre))+
geom_histogram(alpha=0.5, binwidth=0.25, position = "identity")+
geom_density()
I would like to overlay the density plot to the histogram, but it just appears like a thin line in 0
Upvotes: 6
Views: 3905
Reputation: 375
I have come up with an idea that allow you to scale your density plot according to your histogram.
You can get density data using stat:density
function and scale them manually, then plot them using geom_line
:
ggplot(x, aes(n, fill=nombre))+
geom_histogram(alpha=0.5, binwidth=0.25, position = "identity") +
geom_line(aes(x,y, group=nombre),
~ .x %>% group_by(nombre) %>%
mutate(n_cut = cut(n, seq(min(n), max(n), 0.25))) %>%
summarize(
y = density(n)$y * max(table(n_cut)) / max(density(n)$y),
x = density(n)$x
)
)
Upvotes: 2
Reputation: 13873
You'll need to get geom_histogram
and geom_density
to share the same axis. In this case, I've specified both to plot against density by adding the aes(y=..density)
term to geom_histogram
. Note also some different aesthetics to avoid overplotting and so that we are able to see both geoms a bit more clearly:
ggplot(x, aes(n, fill=nombre))+
geom_histogram(aes(y=..density..), color='gray50',
alpha=0.2, binwidth=0.25, position = "identity")+
geom_density(alpha=0.2)
As initially specified, the aesthetics fill=
applies to both, so you have the histogram and density geoms showing you distribution grouped according to "x1" and "x2". If you want the density geom for the combined set of x1 and x2, just specify the fill=
aesthetic for the histogram geom only:
ggplot(x, aes(n))+
geom_histogram(aes(y=..density.., fill=nombre),
color='gray50', alpha=0.2,
binwidth=0.25, position = "identity")+
geom_density(alpha=0.2)
Upvotes: 6