0

I am attempting to plot a simple violin plot with the added quantiles (0.25,0.5,0.75). I use two methods here, draw_quantiles within geom_violin (straight line) and stat_summary (red points). Is there a reason why the line and points appear to be offset?

Thanks in advance!

data <- c(62, 60, 63, 59, 63, 67, 71, 64, 65, 66, 68, 66, 
      71, 67, 68, 68, 56, 62, 60, 61, 63, 64, 63, 59)
grp <- factor(rep(LETTERS[1:4], c(4,6,6,8)))
df <- data.frame(group=grp, dt=data)

ggplot(df,
   aes(x=group,y=dt))+
  geom_violin(width=0.4,draw_quantiles = c(0.25, 0.5, 0.75))+
  stat_summary(fun.y= function(x) quantile(x,0.75), geom="point", size=2, color="red")+
  stat_summary(fun.y= function(x) quantile(x,0.25), geom="point", size=2, color="red")+
  stat_summary(fun.y= function(x) quantile(x,0.5), geom="point", size=2, color="red")+
  theme_classic()+
  theme(
     axis.title.x = element_blank())+
  guides(fill=FALSE)

enter image description here

markus
  • 25,843
  • 5
  • 39
  • 58
Andrew Hamel
  • 336
  • 1
  • 13
  • 1
    A likely explanation is that the quantiles are calculated in different ways, see https://github.com/tidyverse/ggplot2/issues/2088#issuecomment-291003446 – Thomas K Feb 10 '20 at 19:54
  • 1
    The `quantile` function has 9 ways of calculating an empirical quantile - not sure whether that would have any impact in your case, but might be worth looking into. – Valeri Voev Feb 10 '20 at 20:04

0 Answers0