Skip to content

scale_x_date does not allow NA as minimum limit #6868

Description

@vgregoire1

I found a problem with the minimum limits of scale_x_date and scale_x_datetime. According to the documentation, NA could be use.

A numeric vector of length two providing limits of the scale. Use NA to refer to the existing minimum or maximum.
But a POSXIct seems to be required.

I expected that NA could be use, without aving to use as.POSIXct(NA). The upper limit (second element of limit) seems OK.

library(ggplot2)
library(lubridate)

# Example adapted from ?scale_x_date() 
last_month <- Sys.Date() - 0:29
set.seed(1)
       
df <- data.frame(
  date = last_month,
  datetime = last_month-hours(0:29), # testing with datetime
  price = runif(30)
)  

base_datetime <- ggplot(df, aes(datetime, price)) +
  geom_line()

# NA as maximum works
base_datetime + scale_x_date(limits = c(Sys.Date() - 7, NA))

#NA as minimum don't works: 
base_datetime + scale_x_datetime(limits = c(NA,  Sys.Date() - 7) )
#> Error in `transform$transform()`:
#> ! `transform_time()` works with objects of class <POSIXct> only

# Work-around: as.POSIXct(NA)
base_datetime + scale_x_datetime(limits = c(as.POSIXct(NA), Sys.Date() - 7)   )
#> Warning: Removed 7 rows containing missing values or values outside the scale range
#> (`geom_line()`).

# Same behaviour with date (instead of datetime)
base <- ggplot(df, aes(date, price)) +
  geom_line()
base + scale_x_date(limits = c( NA,Sys.Date() - 7 ))
#> Error in `transform$transform()`:
#> ! `transform_date()` works with objects of class <Date> only

Created on 2026-06-19 with reprex v2.1.1

I'm a new GitHub contributor, feedback is welcome.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions