You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Summary statistics (mean, median, ...) in type_violin? #441
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
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?
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.
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).
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:
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.
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.
Activity
grantmcdermott commentedon Jul 11, 2025
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.
zeileis commentedon Jul 11, 2025
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.
grantmcdermott commentedon Jul 11, 2025
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).
grantmcdermott commentedon May 5, 2026
Late follow-up, but just to quickly note that your "x as factor" examples are working now with
tinyplotv0.6.1 (specifically, following #561).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.
One option is simple to document this better in the
type_violinand/ortype_summaryExamples, so that we advice users to explicitly usexfactor variables for these type of composite plots.Another (still) failing case is your final plot with dodged groups:
Here, I think the solution would be to add equivalent support for dodging in
type_summary.grantmcdermott commentedon May 5, 2026
P.S. We might still want to enable convenience arguments that let you add summary stats directly as part of the parent
type_violincall (as opposed to requiring a second layer). But we should still make sure that layering withplt_addworks correctly.zeileis commentedon May 5, 2026
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_addwould both be nice to have.grantmcdermott commentedon Aug 30, 2026
Update: Following #694 the "x as numeric" example now works:
AFAICT the only remaining case is the dodged group case.