-
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
WIP: Aesthetics for positions #6100
base: main
Are you sure you want to change the base?
Conversation
@teunbrand As you're working on this, I'd suggest you also look at the long history of issues with misplaced boxplots or violins when categories are missing. I realized long ago that the only way to truly fix this is to somehow have position adjustments be aware of aesthetics. Not sure whether your current work can address this, but conceptually it seems related. See for example the long discussion here: #3022, and various other issues quoted therein. Also, here is a reprex that highlights the issue. There is no way I am aware of that can properly format and place boxplots when categories are missing. library(ggplot2)
# boxes for water polo, netball, and gymnastics have the wrong size
ggplot(ggridges::Aus_athletes, aes(height, sport, fill = sex)) +
geom_boxplot() # box for water polo is in the wrong location
ggplot(ggridges::Aus_athletes, aes(height, sport, fill = sex)) +
geom_boxplot(position = position_dodge(preserve = "single")) # boxes for water polo, netball, and gymnastics are in the wrong location
ggplot(ggridges::Aus_athletes, aes(height, sport, fill = sex)) +
geom_boxplot(position = position_dodge2(preserve = "single")) Created on 2024-09-12 with reprex v2.0.2 Let me know if you think I should open a new issue for this. I don't think we have one currently, but am not entirely certain. |
Hi @clauswilke, I think that the issue you're highlight is fundamentally the same as #3345. I have pondered them, but haven't been able to come up with a good solution for them (yet). In any case, that issue will not be fixed by this PR. |
Yes, agreed, it's the same as #3345. |
On second thought, this PR might be able to fix it if we have something like an |
I've added an As an example without devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
set.seed(0)
df <- data.frame(
x = rep(rep(LETTERS[1:7], c(3, 2, 2, 2, 1, 1, 1)), each = 10),
y = rnorm(120),
group = rep(c("X", "Y", "Z")[c(1:3, 1:2, 1, 3, 2:3, 1:3)], each = 10)
)
ggplot(df, aes(x, y, fill = group)) +
geom_boxplot(position = position_dodge(preserve = "single")) Now with the last_plot() + aes(order = group) Created on 2024-09-18 with reprex v2.1.1 |
This PR aims to fix #3026 and fix #5445.
Briefly, it adds the ability for position adjustments to declare their own aesthetics. It is currently only implemented for
position_nudge()
.On any layer where
position = "nudge"
, one can usenudge_x
andnudge_y
either as a mapped aesthetic insideaes()
, or static aesthetic passed throughlayer(params)
.A few examples:
Created on 2024-09-12 with reprex v2.1.1