I previously asked a question about using stat_summary to add a vertical line for the median value in my density plot. I'm now trying to adapt this to also add vertical lines for the 95% confidence interval. I've been using another question as the base for my code, but it doesn't add any lines to my plot. How can I get this to work?
library(ggplot2)
set.seed(42)
dat <- data.frame(
site = rep(LETTERS[1:4], each = 25),
sex = rep(c("f", "m"), 50),
mass = rnorm(100, 50, 5)
)
dat |>
ggplot(aes(x = mass, col = sex)) +
geom_density() +
stat_summary(
aes(xintercept = after_stat(x), y = 0),
fun = mean, geom = "vline", orientation = "y"
) +
stat_summary(
aes(xintercept = after_stat(x), y = 0),
fun.min = function(pred) { quantile(pred, probs = 0.025) },
fun.max = function(pred) { quantile(pred, probs = 0.975) },
geom = "vline", orientation = "y", linetype = "dashed"
) +
facet_wrap(~site)

orientation = "y". But you'll also have to make other changes to get what you want... [It would have been helpful to report the warning you get with your current code: "Removed 8 rows containing missing values or values outside the scale range (geom_vline())" is a big clue to what's going on.]