Skip to content

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

Open

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?

Activity

  1. grantmcdermott commented on Jul 11, 2025

    @grantmcdermott
    Owner

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

    Hmmm, agree. "Raincloud" versions would be nice too, but that might have to be a separate, dedicated type.

    We should fix some of those other issues that you've identified here too.

  2. zeileis commented on Jul 11, 2025

    @zeileis
    CollaboratorAuthor

    OK, thanks for the feedback! Unfortunately, I won't have any time to look at this soon (first exams and then a couple of days off) but wanted to post this because I was asked about it today by one of my colleagues.

  3. grantmcdermott commented on Jul 11, 2025

    @grantmcdermott
    Owner

    All good. I'm on vacation now too... Managed to sneak off that last submission with my laptop battery deep in the red (zero power coord = forced screen time constraint).

  4. grantmcdermott commented on May 5, 2026

    @grantmcdermott
    Owner

    Late follow-up, but just to quickly note that your "x as factor" examples are working now with tinyplot v0.6.1 (specifically, following #561).

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

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

    The "x as numeric" example still doesn't work, but I think that this is possibly correct behaviour. At the least, it seems risky to try and guard for these specific cases.

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

    One option is simple to document this better in the type_violin and/or type_summary Examples, so that we advice users to explicitly use x factor variables for these type of composite plots.

    Another (still) failing case is your final plot with dodged groups:

    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"))

    Here, I think the solution would be to add equivalent support for dodging in type_summary.

  5. grantmcdermott commented on May 5, 2026

    @grantmcdermott
    Owner

    P.S. We might still want to enable convenience arguments that let you add summary stats directly as part of the parent type_violin call (as opposed to requiring a second layer). But we should still make sure that layering with plt_add works correctly.

  6. zeileis commented on May 5, 2026

    @zeileis
    CollaboratorAuthor

    Thanks, Grant, looks good to me! I agree that the "x as numeric" example is "as expected" and it is enough to document it a bit better. I also agree that both a convenience summary option and a dodged groups in tinyplot_add would both be nice to have.

  7. grantmcdermott commented on Aug 30, 2026

    @grantmcdermott
    Owner

    Update: Following #694 the "x as numeric" example now works:

    # x numeric
    tinypolt(len ~ dose, data = ToothGrowth, type = "violin")
    tinypolt_add(type = type_summary(median, type = "p"))

    AFAICT the only remaining case is the dodged group case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions