Using the code below, I'm generating a set of simple histogram:
data(mtcars); Vectorize(require)(package = c("ggplot2", "ggthemes", "dplyr"))
mtcars %>%
add_rownames(var = "model") %>%
gather(var, value, -model, -am) %>%
filter(var %in% c("hp")) %>%
# Define chart
ggplot(aes(value)) +
geom_histogram(aes(y = ..ncount..), colour = "black", fill = "gray58",
binwidth = 15) +
geom_density(aes(y = ..scaled..), colour = "red") +
facet_wrap( ~am, ncol = 3, scales = "free")
I would like to maintain scale that is produced when generating a histogram without the ..ncount..
special variable, as in the example:
mtcars %>%
add_rownames(var = "model") %>%
gather(var, value, -model, -am) %>%
filter(var %in% c("hp")) %>%
# Define chart
ggplot(aes(value)) +
geom_histogram(colour = "black", fill = "gray58",
binwidth = 15) +
geom_density(aes(y = ..scaled..), colour = "red") +
facet_wrap( ~am, ncol = 3, scales = "free")
But it makes the geom_density
look poor.
Task
So what I want boils down to:
- keep scale of
y
axis from the second one - keep graphics from the first one