ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

geom_vline() and geom_hline() affect x and y scales

Open dboutwell3 opened this issue 1 month ago • 3 comments

In prior versions of ggplot2, geom_vline and geom_hline did not affect the x and y scales, which was/is still currently the documented behavior. However, as of 4.0.1 (and possibly earlier - I have not explicitly tested 4.0.0) these functions do affect the x and y scales. Note that geom_abline continues to behave as documented, e.g. it continues to not affect the x and y scales.

Here is the code to reproduce the bug:

library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p + geom_vline(xintercept = 5)
p + geom_vline(xintercept = 10) # affects scale
p + geom_hline(yintercept = 30)
p + geom_hline(yintercept = 60) # affects scale
p + geom_abline(slope = 1, intercept = 10)
p + geom_abline(slope = 1, intercept = 50) # does not affect scale

dboutwell3 avatar Dec 08 '25 19:12 dboutwell3

Thanks for the report! The intent is to let these layers affect scales, like any ordinary layer. The relevant news entry is here:

https://github.com/tidyverse/ggplot2/blob/60f5ed5f999e1534b9e6feb624ccf1ea4617a9b0/NEWS.md?plain=1#L195-L197

The slope and intercept aesthetics do not contribute to a range. Likewise, geom_hline() does not affect the x-scale and geom_vline() does not affect the y-scale.

is still currently the documented behavior

Can you point to the documentation that incorrectly describes the interaction between these layers and scales?

teunbrand avatar Dec 08 '25 19:12 teunbrand

From ?geom_vline under "Details":

Unlike most other geoms, these geoms do not inherit aesthetics from the plot default, because they do not understand x and y aesthetics which are commonly set in the plot. They also do not affect the x and y scales.

dboutwell3 avatar Dec 08 '25 19:12 dboutwell3

Gotcha, thank you!

teunbrand avatar Dec 08 '25 20:12 teunbrand