I would like to make stacked (facet_grid
) size histograms in ggplot2 by Year. The years have different sample sizes. I have not been able to get the ..density..
to produce correct proportions for each histogram bin. So, I've been using ..count..
/(sample size number). From my reading of the stat tranformations ..count..
, you cannot perform an operation with an object (e.g. nrow(data)). How can I get these stacked histograms with different sample sizes? The format in the code below would produce a figure that matches other figures for a report, which is why I would like to stick with ggplot2, but maybe there are other packages. Here is an example:
d1 <- as.data.frame(round(rnorm(121, 86, 28), 0))
colnames(d1) <- "Length"
d1$Year <- "2015"
d2 <- as.data.frame(round(rnorm(86, 70, 32), 0))
colnames(d2) <- "Length"
d2$Year <- "2016"
D <- rbind(d1, d2)
ggplot(D, aes(x = Length)) +
geom_histogram(aes(y = ..count../nrow(D)),
breaks=seq(0, 160, by = 3),
col="black",
fill="grey48",
alpha = .8)+
labs(title = "Size by Year", x = "Length", y = "frequency") +
scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) +
theme_bw() +
theme(text = element_text(size=16),
axis.text.y = element_text(size=12)) +
geom_vline(aes(xintercept = 95.25),
colour = "red", size = 1.3)+
facet_grid(Year ~ .)
This part ..count../nrow(D)
won't work and needs the sample size for each year when I facet them facet_grid(Year ~ .)