Closed
Description
Given this data and base plot:
library(ggplot2)
dat <- data.frame(
x = c(as.Date("2025-01-01") + 0:30, rep(as.Date("2025-01-15"), 31)),
y = c(rep(15, 31), 1:31)
)
p <- ggplot(dat, aes(x, y)) +
geom_vline(aes(xintercept = x), color = "gray75") +
geom_hline(aes(yintercept = y), color = "gray75") +
geom_point() +
theme_classic() +
theme(plot.margin = margin(20, 20, 20, 20))
p
I want to limit the axis ranges and show out-of-bounds values along the y axis, but not the x axis. So I set ylim
in coord_cartesian()
with clip = "off", and
limitsin
scale_x_date()`. As expected, out-of-bounds points and hlines are shown for the y axis, and out-of-bounds points aren't shown for the x axis. But out-of-bounds vlines are shown for the x axis.
p +
scale_x_date(limits = as.Date(c("2025-01-05", "2025-01-25"))) +
coord_cartesian(ylim = c(5, 25), clip = "off")
Additional notes:
- This occurs only if
x
is a date, not a continuous value. - The same issue affects
scale_y_date()
-- i.e., ify
is a date andx
is continuous, the issue occurs for the out-of-bounds hlines on the y axis. - The problem doesn't occur if you use
geom_segment(aes(x = x, xend = x, y = -Inf, yend = Inf))
instead ofgeom_vline(aes(xintercept = x))
. - I haven't tested other scale types or geoms beyond these.
- Setting
oob = scales::oob_censor()
oroob = scales::oob_squish()
inscale_x_date()
has no effect.
Metadata
Metadata
Assignees
Labels
No labels