Skip to content

preserve = "single" breaks violin plots #2801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
willgearty opened this issue Aug 3, 2018 · 1 comment · Fixed by #5928
Closed

preserve = "single" breaks violin plots #2801

willgearty opened this issue Aug 3, 2018 · 1 comment · Fixed by #5928
Labels
bug an unexpected problem or unintended behavior positions 🥇

Comments

@willgearty
Copy link

willgearty commented Aug 3, 2018

I'm trying to use preserve = "single" as you would with boxplots to maintain the sizes of the boxes when there are unused factors. While I figured it would work just the same way for violin plots, it appears to completely break them.

p <- ggplot(mtcars, aes(factor(cyl), mpg))
#works fine
p + geom_violin(aes(fill = factor(vs)))
#works fine
p + geom_violin(aes(fill = factor(vs)), position = "dodge")
#this doesn't work
p + geom_violin(aes(fill = factor(vs)), position = position_dodge(preserve = "single"))

The latter attempt above produces this:

image

@clauswilke
Copy link
Member

The problem is that position_dodge() tries to find the maximum number of groups by counting the maximum number of rows in the data with the same xmin value:

ggplot2/R/position-dodge.r

Lines 100 to 101 in 4d2ca99

ns <- vapply(panels, function(panel) max(table(panel$xmin)), double(1))
n <- max(ns)

This works for boxplots, because each boxplot is one line in the processed data. But it doesn't work for violin plots, because each violin plot consists of many lines (512 by default). I suspect the solution would be to count the number of identical xmin values with distinct group values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior positions 🥇
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants