Skip to content

geom_vline() doesn't respect scale_*_date() limits when coord_cartesian(clip = "off") #6405

Closed
@ccsarapas

Description

@ccsarapas

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

Image

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 limitsinscale_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")

Image

Additional notes:

  • This occurs only if x is a date, not a continuous value.
  • The same issue affects scale_y_date() -- i.e., if y is a date and x 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 of geom_vline(aes(xintercept = x)).
  • I haven't tested other scale types or geoms beyond these.
  • Setting oob = scales::oob_censor() or oob = scales::oob_squish() in scale_x_date() has no effect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions