tidyverse / ggplot2

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

Expose position arg on geom_hline() and geom_vline() #5965

Open yutannihilation opened 4 days ago

yutannihilation commented 4 days ago

Reopen #4286, which aimed to fix #4285.

As suggested by @teunbrand, I confirmed now the code works without the as.numeric() tweak if I merge the changes of #5640.

mydata <- data.frame(
  x = factor(c("1", "2", "3")),
  y = c(3, 4, 5)
)
mydata$splits <- factor(mydata$x, labels = c("baseline", "Cycle 1", "Cycle 1"))

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

pos <- position_nudge(x = -0.1)

# works
ggplot(mydata, aes(x, y)) +
  geom_point(size = 3, position = pos) +
  geom_vline(
    aes(xintercept = as.numeric(x)),
    alpha = 0.2, linewidth = 5,
    colour = "red",
    position = pos
  )


# didn't work, but now works!
ggplot(mydata, aes(x, y)) +
  geom_point(size = 3, position = pos) +
  geom_vline(
    aes(xintercept = x),
    alpha = 0.2, linewidth = 5,
    colour = "red",
    position = pos
  )

Created on 2024-07-01 with reprex v2.1.0

yutannihilation commented 4 days ago

Thanks! Added a news bullet. I think this should be merged after #5640 gets merged because the soundness relies on it, but please feel free to merge early if you want.