-
Notifications
You must be signed in to change notification settings - Fork 2k
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Aligning plots: Implementation of drop = FALSE for position_dodge #3988
Comments
@karawoo can you comment on the amount of work this would require, seeing that you were the last to dive head-first into dodging 🙂 |
In the dev version of ggplot2 I can't reproduce this specific issue; having only two observations still generates a violin: library("ggplot2")
set.seed(5)
dat <- data.frame(x = rep(1, times = 5), y = rnorm(5, 1), z = c("A", "A", "A", "B", "B"))
ggplot(dat, aes(x = x, y = y, fill = z)) +
geom_boxplot(position = position_dodge(width = 0.9)) +
geom_violin(alpha = 0.5) Created on 2020-08-31 by the reprex package (v0.3.0.9001) That said, this is a similar question to others that have come up before (#3022 (comment), #2076, #2813). The short answer is that having more control over placement of a geom within a group is not very easy. It may be possible, and it would be nice if we could improve the situation, but right now it's not straightforward. |
@char4816 can you see if you can reproduce my results with the development version of ggplot2? You can install it with @clauswilke I'm not against merging #2813 since it would make the |
@karawoo Agreed. I suspect a proper fix for |
Using the development version of ggplot2, that example works great now. However, the overarching issue is bigger than just violin plots (and whether or not two points is enough to create the violin plot). For example, if we wanted to plot the following dataframe with boxplots,
the two missing values will make the dodging look bad. Here is some reproducible code:
The best solution I have seen so far for this problem is to make one of the NAs equal to zero so a box plot is generated, and then to cover up that boxplot... (not a very pretty solution).
|
I'm closing this issue for the following reasons. library(ggplot2)
data <- data.frame(
cat=c('A','A','B','B','A','A','B','B'),
ind=c('x','x','x','x','y','y','y','y'),
values=c(4,2,NA,NA,4,5,6,9)
)
ggplot(data) +
geom_boxplot(
aes(x=as.factor(cat), y=values, fill=ind),
position=position_dodge(width=.90, preserve = 'single'),
na.rm=T
) The original issue of misalignment between boxplots and violin plots with <2 observations can be mitigateed using set.seed(5)
dat <- data.frame(x = rep(1, times = 4), y = rnorm(4, 1), z = c("A", "A", "A", "B"))
ggplot(dat, aes(x = x, y = y, fill = z)) +
geom_boxplot(position = position_dodge(width = 0.9)) +
geom_violin(alpha = 0.5, drop = FALSE)
#> Warning: Cannot compute density for groups with fewer than two datapoints. Created on 2024-07-24 with reprex v2.1.1 |
As others have pointed out (#688), having a position_dodge option such as
drop = FALSE
would be really helpful for plots like this:I know Hadley has given some helpful workaround suggestions (e.g. faceting) and has mentioned that this isn't within the scope of position_dodge but I am hoping that one more person raising the issue might help to make the change.
What's happening for some of my plots' x-categories is that there are only 2 data points (e.g. cases in Homozygous Alt in my plot), which is enough to generate a boxplot, but not a violin plot
Because the 3rd green violin plot doesn't have a counterpart to "dodge", the violin plot is centered and therefore wider and not aligned with its corresponding boxplot which is quite sad to look at.
While faceting works to an extent:
It would look much cleaner for publication if I could do
position = position_dodge(width=0.9), drop = FALSE)
I hope this suggestion is taken seriously and I really appreciate your help & time.
Chris
The text was updated successfully, but these errors were encountered: