I'd like to display the sample size of each species on my boxplot, but the plot is fairly cluttered. Is there a way to display the sample size on the "outside" of the plot with "N" as the header (sample size should go in the red area I highlighted)?
df <- data.frame(year = rep(c(2019, 2020), each = 10),
month = rep(c("March", "October"), each = 1),
site = rep(c("1", "2", "3", "4", "5"), each = 2),
type = rep(c("baitfish", "shark"), each = 1),
salinity = sample(x = 20:35, size = 20, replace = TRUE),
num = sample(x = 0:10, size = 20, replace = TRUE))
count_by_row <- data.frame(df[rep(row.names(df), df$num), 1:5])
ggplot(data=subset(count_by_row, !is.na(salinity)), aes(x = reorder(type, -salinity, FUN = median), y = salinity)) +
geom_boxplot(outlier.shape = 1, outlier.size = 2) +
coord_flip() +
xlab("") +
ylab("salinity (PSU)")
You could add a custom annotation after increasing the right margin: