tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

Reverse dodging #5923

Open teunbrand opened 1 month ago

teunbrand commented 1 month ago

This PR aims to fix #3610.

Briefly, it adds a reverse argument to position_dodge() and position_jitterdodge() that reverses the groups within a position in terms of dodge order.

As a detail, for consistency with position_dodge2(), we actually pass !reverse to collide().

Adapted reprex from issue:

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

p <- ggplot(mtcars) +
  aes(
    as.factor(cyl), mpg, 
    fill = as.factor(vs), 
    group = interaction(cyl, vs)
  )

p + 
  geom_boxplot(position = position_dodge(reverse = FALSE)) +
  geom_point(position = position_jitterdodge(reverse = FALSE), shape = 21)


p + 
  geom_boxplot(position = position_dodge(reverse = TRUE)) +
  geom_point(position = position_jitterdodge(reverse = TRUE), shape = 21)

Created on 2024-05-31 with reprex v2.1.0