tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.54k stars 2.03k forks source link

Quantiles used in `geom_violin` seem a little off #5695

Closed ltierney closed 9 months ago

ltierney commented 9 months ago

In R 4.3.2 with ggplot2 3.4.4 the plot produced by this code shows a discrepancy between the quantiles used by geom_violin and the ones used by geom_boxplot as well as the ones returned by quantile():

library(ggplot2)
ggplot(faithful) +
    geom_boxplot(aes(y = eruptions, x = "Box")) +
    geom_violin(aes(y = eruptions, x = "Violin"),
                trim = FALSE,
                draw_quantiles=c(.25, .5, .75)) +
                geom_hline(yintercept = quantile(faithful$eruptions, c(.25, .5, .75)),
                           color = "red")

Using trim = FALSE changes the locations a little but the discrepancy remains.

teunbrand commented 9 months ago

Thanks for the report! The quantiles are based on the density estimation, not on the actual data. As #4120 proposes to change this to the actual data, I'll consider this issue a duplicate to keep discussion in one place.