I'd like to draw a line through my rideplots for the mean. The built-in quantile arguments draw a line in the style I want, but at the median. How can I draw one at the mean, preferably without using geom_vline() or pluck at the ggplot build object but staying in the ggridges ecology?
#adding a column for the mean of each Species of the iris dataframe
iris_meaned <- iris %>%
group_by(Species) %>%
mutate(mean_petal_len = mean(Petal.Length)) %>%
ungroup()
iris_meaned %>%
ggplot() +
geom_density_ridges(aes(x = Petal.Length, y = Species, fill = Species),
quantile_lines = T, quantiles = 2) + #adding lines for the median
geom_text(aes(x = mean_petal_len, y = Species,
label = round(mean_petal_len, 2)), size = 2, nudge_x = 0.03, nudge_y = 0.35) +
theme_classic() +
theme(axis.title = element_blank(),
legend.position = "None")
You can provide an arbitrary function to the argument
quantile_fun
. The function has to take a numeric vector as the first argument, and the number of quantiles as the second. But it's fine to ignore the second argument. Themean()
function satisfies these criteria, and thusquantile_fun = mean
creates vertical lines at the mean.请注意,在您的示例中,您将一遍又一遍地绘制文本标签。我已经修复了代码,使其可以正常工作。
Created on 2020-05-23 by the reprex package (v0.3.0)