Skip to content

Feature request: quantiles based on observations instead of density estimates with geom_violin #4120

Closed
@SarenT

Description

@SarenT

geom_violin uses density estimate to plot quantiles, which doesn't align with box plot.

This has already been asked and is document: https://stackoverflow.com/questions/36033341/differing-quantiles-boxplot-vs-violinplot
issue: #2088

I think it would be useful to have an optional parameter to choose quantiles computed based on actual observations instead of density estimates. violin plots are useful to see rough distribution of the data but quantiles of actual observations also matter. Therefore, I think it would be great to have such an option without breaking existing behaviour.

PS: reprex didn't work for me but here is a simple reproducible code.

library(ggplot2)
set.seed(5)
type1 = rnorm(n = 20, mean = 5)
type2 = c(10, 9, 9, 9, 9, 7, 7, 6, 5, 4, 1)
type3 = c(rnorm(n = 10, mean = 2.5), rnorm(n = 10, mean = 7.5, sd = 0.5))

#median(example1)
#median(example2)

df = data.frame(type = c(rep("Type 1", length(type1)), rep("Type 2", length(type2)), rep("Type 3", length(type3))), 
				val = c(type1, type2, type3))

ggplot(df, aes(x = type, y = val)) + theme_classic() + 
	geom_boxplot(alpha = 0.5) + 
	geom_violin(scale = "area", alpha = 0.5, draw_quantiles = c(0.25, 0.5, 0.75)) +
	geom_dotplot(binaxis = "y", stackdir = "center", alpha = 0.3, dotsize = 0.4)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions