Adding Mean to Grouped Violin Plot

Good evening, I have a dataset like the following and I'm trying to compute grouped violinplots. That all works. But now I'd like to add mean values + Standard deviations and if I add the order for that it looks very weird, so I guess the approach is different for grouped violin plots.

library(tidyverse)

# Load dataset from github
data <- read.table("", header=T, sep=",") %>%
  mutate(tip = round(tip/total_bill*100, 1))

# Grouped
data %>%
  ggplot(aes(fill=sex, y=tip, x=day)) + 
  geom_violin(trim = FALSE, position="dodge", alpha=0.5) +
  xlab("") +
  ylab("Tip (%)") +
  ylim(0,40) +
stat_summary(fun.data=mean_sdl, fun.args = list(mult = 1),
               geom="pointrange", color="black", shape = 18, size = 0.75)


I would be very glad if somebody could help me out :)

thank you!

1 Answer

If you specify "position" and "width" you should be able to get them into the correct alignment, e.g.

library(tidyverse)

# Load dataset from github
data <- read.table("", header=T, sep=",") %>%
  mutate(tip = round(tip/total_bill*100, 1))

# Grouped
data %>%
  ggplot(aes(fill=sex, y=tip, x=day)) + 
  geom_violin(trim = FALSE, position="dodge", alpha=0.5) +
  xlab("") +
  ylab("Tip (%)") +
  ylim(0,40) +
  stat_summary(fun.data=mean_sdl, fun.args = list(mult = 1),
               geom="pointrange", color="black",
               shape = 18, size = 0.75,
               position = position_dodge(width = 0.9))
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest