Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding mean to grouped violin plot

Tags:

r

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("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/10_OneNumSevCatSubgroupsSevObs.csv", 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!

like image 503
Nush Avatar asked Jul 14 '26 15:07

Nush


1 Answers

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("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/10_OneNumSevCatSubgroupsSevObs.csv", 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))

example_1.png

like image 67
jared_mamrot Avatar answered Jul 16 '26 10:07

jared_mamrot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!