I'm looking at ggpubr::stat_mean:
Is there a way to add a label on top of the group mean point indicating the groping label cyl? Thanks!
[reproducible example:]
# Load data
library(ggpubr)
df <- mtcars
df$cyl <- as.factor(df$cyl)
# Scatter plot with ellipses and group mean points
ggscatter(df, x = "wt", y = "mpg",
color = "cyl", shape = "cyl", ellipse = TRUE)+
stat_mean(aes(color = cyl, shape = cyl), size = 4)
It's a bit unclear to me how you would do that with ggpubr
. For some reason it doesn't seem to like to let you use the geom_text with stat_mean
.
One work around is just to use ggplot
directly with a bit of dplyr
to calculate the means. You can do
library(dplyr)
means <- df %>%
group_by(cyl) %>%
summarize(across(c(wt, mpg), mean))
ggplot(df) +
aes(x=wt, y=mpg, color=cyl, shape=cyl) +
geom_point() +
stat_ellipse(aes(fill=cyl), alpha=.2, geom="polygon") +
geom_point(size=4, data=means) +
geom_label(aes(label=cyl), color="black", nudge_y =2.5, data=means) +
ggpubr::theme_pubr()