Discrepancy between draw_quantiles within geom_violin and stat_summary

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)

See here:

As explained in the link. I suspect that the difference is that geom_violin calculates the quantiles based on the density estimate and the quantile() function is using the data. Also, the quantile() function has 9 different methods of calculating the quantiles, of which number 7 is the default.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.