I have a multi-panelled density plot with two density curves/panel. I'd like to add the mean value for each density curve as a vertical line in each panel (i.e., 2 lines/panel). How do I do this with stat_summary?
set.seed(42)
data_frame(site = rep(LETTERS[1:4], each = 25),
sex = rep(c("f", "m"), 50),
mass = rnorm(100, 50, 5)) %>%
ggplot(aes(x = mass, col = sex)) +
geom_density() +
stat_summary(fun = mean, geom = "vline") +
facet_wrap(~site)
Error in `stat_summary()`:
! Problem while computing stat.
ℹ Error occurred in the 2nd layer.
Caused by error in `compute_layer()`:
! `stat_summary()` requires the following missing aesthetics: y
Run `rlang::last_trace()` to see where the error occurred.
When I add a term for y, I get another error message:
data_frame(site = rep(LETTERS[1:4], each = 25),
sex = rep(c("f", "m"), 50),
mass = rnorm(100, 50, 5)) %>%
ggplot(aes(x = mass, col = sex)) +
geom_density() +
stat_summary(fun = mean, geom = "vline", aes(y = 0)) +
facet_wrap(~site)
Error in `stat_summary()`:
data_frame(site = rep(LETTERS[1:4], each = 25),
sex = rep(c("f", "m"), 50),
mass = rnorm(100, 50, 5)) %>%
ggplot(aes(x = mass, col = sex)) +
geom_density() +
stat_summary(fun = mean, geom = "vline", aes(y = 0)) +
facet_wrap(~site)
! Problem while setting up geom.
ℹ Error occurred in the 2nd layer.
Caused by error in `compute_geom_1()`:
! `geom_vline()` requires the following missing aesthetics: xintercept
Run `rlang::last_trace()` to see where the error occurred.
and when I add a term for xintercept, then I get the value for every individual instead of the mean:
data_frame(site = rep(LETTERS[1:4], each = 25),
sex = rep(c("f", "m"), 50),
mass = rnorm(100, 50, 5)) %>%
ggplot(aes(x = mass, col = sex)) +
geom_density() +
stat_summary(fun = mean, geom = "vline", aes(xintercept = mass, y = 0)) +
facet_wrap(~site)

