Skip to content

Violin resolution - #5402

Merged
teunbrand merged 3 commits into
tidyverse:mainfrom
teunbrand:violin_resolution
Sep 12, 2023
Merged

teunbrand merged 3 commits into
tidyverse:mainfrom
teunbrand:violin_resolution

Conversation

@teunbrand

Copy link
Copy Markdown
Collaborator

This PR aims to fix #5396.

Briefly, geom_violin() calls resolution() to determine the default width of a violin.
In resolution(), we're (correctly) making an exception for integers and 'mapped_discrete' vectors to have resolution 1.

However, the 'mapped_discrete' class got dropped from data$x due to a mean() operation.
In this PR, we skip the mean calculation when data$x only has 1 unique value, thereby preserving the 'mapped_discrete' class that gets forwarded to resolution().

Small demo:

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

dplyr::filter(diamonds, cut %in% c("Ideal", "Good")) |>
  ggplot(aes(price, cut)) +
  geom_violin() +
  scale_y_discrete(drop = FALSE)

Created on 2023-08-29 with reprex v2.0.2

@yutannihilation yutannihilation 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.

Looks good to me.

@teunbrand

Copy link
Copy Markdown
Collaborator Author

Thanks for the review!

@teunbrand
teunbrand merged commit 0d2023d into tidyverse:main Sep 12, 2023
@teunbrand
teunbrand deleted the violin_resolution branch September 12, 2023 06:55
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.

Bug sometimes with polygon geoms, filtered factors, drop = FALSE, and width = NULL

2 participants