Skip to content

Add drop argument to stat_ydensity() #5129

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

Merged
merged 7 commits into from
Mar 23, 2023
Merged

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #5002.

Briefly, it adds a drop = TRUE/FALSE argument to stat_ydensity(), so that groups with less than 2 observations can still be considered for position adjustment purposes (i.e. dodging).

I have some doubts whether the default for drop should be TRUE or FALSE, but I've opted for preserving current behaviour by default.

As a visual example:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

df <- data.frame(
  x = factor(rep(1:2, each = 4)),
  y = rep(1:2, 4),
  g = rep(c("A", "A", "B", 'B'), 2)
)

dodge <- position_dodge(0.9)
p <- ggplot(head(df, -1), mapping = aes(x, y, fill = g)) +
  geom_boxplot(position = dodge)

p + geom_violin(alpha = 0.5, position = dodge, drop = TRUE)
#> Warning: Groups with fewer than two datapoints have been dropped.
#> ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.

p + geom_violin(alpha = 0.5, position = dodge, drop = FALSE)
#> Warning: Cannot compute density for groups with fewer than two datapoints.

Created on 2023-01-04 by the reprex package (v2.0.1)

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM... I think we should preserve the current behaviour as the code does now

@teunbrand teunbrand merged commit 64814b0 into tidyverse:main Mar 23, 2023
@teunbrand teunbrand deleted the violin_drop branch March 23, 2023 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dropped cases are taken into account for the dodging of geom_boxplot but not geom_violin
2 participants