tidyverse / tidyverse/ggplot2

scale_x_date does not allow NA as minimum limit

Open
#6,868 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation tidy-dev-day :nerd_face:
Dominant language
R
Stars
7k
Forks
2.1k
Avg merge
59m
Merged PRs (30d)
2

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the scale_x_date() and scale_x_datetime() entry points and reproduce the reported failures using the examples in the issue. Trace the calls to transform_date() and transform_time(), then add a regression test covering bare NA as the minimum limit. Done means both date and datetime scales accept NA in the first limits position without the reported class error.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.