ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Dodge points vertically

Open teunbrand opened this issue 2 months ago • 0 comments

This PR aims to fix #5809.

Briefly, it adds an orientation argument to position_dodge() that can be set to "y" in order to dodge vertically if has_flipped_aes() cannot discern an orientation. In order to make this work, has_flipped_aes() can now take custom default that is returned when other attempts to discern the orientation have been exhausted.

Reprex from the issue:

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

df <- data.frame(
  trt = c(1,1,2,2),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
)

dodge <- position_dodge(0.2, orientation = "y")

ggplot(df, aes(resp, trt, colour = group)) +
  geom_linerange(
    aes(xmin = lower, xmax = upper), 
    position = dodge
  ) +
  geom_point(
    aes(group = interaction(group, trt)),
    position = dodge
  )

Created on 2024-04-18 with reprex v2.1.0

teunbrand avatar Apr 18 '24 05:04 teunbrand