Skip to content

Summary statistics (mean, median, ...) in type_violin? #441

@zeileis

Description

@zeileis

Is there a sufficiently easy way to add summary statistics like mean or median to violin plots? If not, then maybe this could be added to type_violin() itself?

At first, I thought that type_summary() could be used for this but this is not fully straightforward because it

Examples:

Image

In the case with numeric x the violin and the points are misaligned (see left panel) because type_violin always treats x as factor and type_summary always expects numeric:

tinyplot(len ~ dose, data = ToothGrowth, fill = 0.2,  type = "violin")
tinyplot_add(type = type_summary(median, type = "p"))

Using a factor x is currently not possible in type_summary:

tinyplot(len ~ factor(dose), data = ToothGrowth, fill = 0.2,  type = "violin")
tinyplot_add(type = type_summary(median, type = "p"))
## Error in Summary.factor(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  : 
##   'range' not meaningful for factors

Only when the numeric values are 1, ..., k then violin and summary points are aligned (center panel):

tinyplot(len ~ as.numeric(as.factor(dose)), data = ToothGrowth, fill = 0.2,  type = "violin")
tinyplot_add(type = type_summary(median, type = "p"))

However, even in that case, by groups are handled differently with vs without juxtaposition (right panel):

tinyplot(len ~ as.numeric(as.factor(dose)) | supp, data = ToothGrowth, fill = 0.2,  type = "violin", legend = FALSE)
tinyplot_add(type = type_summary(median, type = "p"))

I guess this would indicate that adding some summary option to type_violin directly would be best?

Aside: Setting pch does not seem to be supported in type_summary?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions