Skip to content

Add drop argument to stat_ydensity() - #5129

Merged
teunbrand merged 7 commits into
tidyverse:mainfrom
teunbrand:violin_drop
Mar 23, 2023
Merged

teunbrand merged 7 commits into
tidyverse:mainfrom
teunbrand:violin_drop

Conversation

@teunbrand

Copy link
Copy Markdown
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)

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Member

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