I am wanting to put geom_histogram
and geom_density
on the same scale. I can accomplish this in one direction by using geom_histogram(aes(y = stat(density)))
. This works great if I want everything on the density scale. However, I am wanting things on the count scale. But adding geom_density(aes(y = stat(count)))
results in a density line that is not on the same scale as the counts in the histogram. Is there a way to get both density and histogram on the same count scale?
library(tidyverse)
df <- tibble(x = rnorm(10000))
ggplot(df, aes(x)) +
geom_histogram(aes(y = stat(density))) +
geom_density()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(df, aes(x)) +
geom_histogram() +
geom_density(aes(y = stat(count)))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Created on 2019-05-01 by the reprex package (v0.2.1)