Draw Boxplot with Means in R (2 Examples) | Add Mean Values | points, text & stat_summary [ggplot2]

preview_player
Показать описание
R code of this video:

group = LETTERS[1:4])

data_means <- aggregate(data$values, # Means by group
list(data$group),
mean)

boxplot(data$values ~ data$group) # Draw boxplot in Base R
points(x = 1:nrow(data_means), # Add points to plot
y = data_means$x,
col = "red",
pch = 16)
text(x = 1:nrow(data_means), # Add text to plot
y = data_means$x - 0.15,
labels = paste("Mean:", round(data_means$x, 1)),
col = "red")

library("ggplot2")

ggplot(data, aes(x = group, y = values)) + # Draw ggplot2 boxplot
geom_boxplot() +
stat_summary(fun = mean, geom = "point", col = "red") + # Add points to plot
stat_summary(fun = mean, geom = "text", col = "red", # Add text to plot
vjust = 1.5, aes(label = paste("Mean:", round(..y.., digits = 1))))

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

thanks! was very usefull with my college stuff!!

inaydweller
Автор

Took me out of my 2 hours serch! short and just what I needed! Master!

czr
Автор

Super clear and useful! How would you do to add Mean+SD as label? Is there a way to add the number of observations for each group? Thanks

johannafedorovsky
Автор

Thanks! Do you perhaps know how to show the different means for each boxplot, in the case you add a third binomial variable on the graph (aes= x, y, fill=z)? In this case, the means stay in the middle of both boxplots and are not placed on their own boxplots...

cchen
Автор

What does the ..y.. accomplish in the round function?

russtin
join shbcf.ru