I want my plot to display how many people/observations are within each group. How can I do this? I found this answer, however I had a hard time following it. Modify x-axis labels in each facet
Rc <- iris %>%
dplyr::group_by(Species) %>%
dplyr::mutate(mean = mean(Petal.Length),
se = sd(Petal.Length)/sqrt(length(Petal.Length))) %>%
ungroup() %>%
ggplot(aes(x=Petal.Length, y=Species)) +
stat_slab(aes(fill = Species, )) +
stat_dots(aes(color = Species), side = "bottom", shape = 16) +
scale_fill_brewer(palette = "Set1", aesthetics = c("fill", "color")) +
geom_errorbar(aes(xmin = mean - 1.96 * se,
xmax = mean + 1.96 * se), width = 0.2) +
stat_summary(fun=mean, geom="point", shape=16, size=3.0) +
theme_bw(base_size = 10) +
theme(legend.position = "top")+
labs(title="Raincloud plot with ggdist")
print(Rc + labs(x = "Petal Length")) [![Result[1]][1]
One option would be to add a new column to your dataset where you glue the number obs. per species to the species name. This column could then be mapped on y
:
library(dplyr)
library(ggplot2)
library(ggdist)
iris %>%
dplyr::group_by(Species) %>%
dplyr::mutate(
mean = mean(Petal.Length),
se = sd(Petal.Length) / sqrt(length(Petal.Length)),
species_y = paste0(Species, "\n(", n(), ")")
) %>%
ungroup() %>%
ggplot(aes(x = Petal.Length, y = species_y)) +
stat_slab(aes(fill = Species)) +
stat_dots(aes(color = Species), side = "bottom", shape = 16) +
scale_fill_brewer(palette = "Set1", aesthetics = c("fill", "color")) +
geom_errorbar(aes(
xmin = mean - 1.96 * se,
xmax = mean + 1.96 * se
), width = 0.2) +
stat_summary(fun = mean, geom = "point", shape = 16, size = 3.0) +
theme_bw(base_size = 10) +
theme(legend.position = "top") +
labs(title = "Raincloud plot with ggdist", x = "Petal Length")