# ggplot2 Error in R: Invalid input: date_trans works with objects of class Date only (2 Examples)

On this page, I’ll show how to **deal with the “Error: Invalid input: date_trans works with objects of class Date only”** in the R programming language.

Table of contents:

Let’s dig in.

## Example Data, Add-On Packages & Basic Graph

The following data is used as basement for this R tutorial:

data <- data.frame(x = as.Date(c("10-08-2024", # Create example data "11-12-2022", "03-05-2025"), format = "%d-%m-%Y"), y = 1:3) data # Print example data |

data <- data.frame(x = as.Date(c("10-08-2024", # Create example data "11-12-2022", "03-05-2025"), format = "%d-%m-%Y"), y = 1:3) data # Print example data

Table 1 shows that our example data is constructed of three rows and two columns.

For the following R code, we’ll also need to install and load the ggplot2 package:

install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package |

install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package

Next, we can draw our data:

ggp <- ggplot(data, aes(x, y)) + # Create ggplot2 scatterplot with dates geom_point() ggp # Draw ggplot2 scatterplot |

ggp <- ggplot(data, aes(x, y)) + # Create ggplot2 scatterplot with dates geom_point() ggp # Draw ggplot2 scatterplot

After running the previous R programming syntax the ggplot2 time series scatterplot with dates on the x-axis shown in Figure 1 has been created.

## Example 1: Reproduce the Error: Invalid input: date_trans works with objects of class Date only

The following R programming code shows how to replicate the ggplot2 “Error: Invalid input: date_trans works with objects of class Date only”.

Let’s assume that we want to add a rectangle using the geom_rect function to our plot. Then, we might try to use the following R code:

ggp + # Replicating the error geom_rect(data = data, aes(xmin = "01-01-2024", xmax = "01-01-2025", ymin = -Inf, ymax = Inf)) # Error: Invalid input: date_trans works with objects of class Date only |

ggp + # Replicating the error geom_rect(data = data, aes(xmin = "01-01-2024", xmax = "01-01-2025", ymin = -Inf, ymax = Inf)) # Error: Invalid input: date_trans works with objects of class Date only

Unfortunately, the RStudio console returns the “Error: Invalid input: date_trans works with objects of class Date only” after executing the previous syntax.

The reason for this is that we have not specified the **dates within geom_rect** properly.

Let’s solve this problem!

## Example 2: Debug the Error: Invalid input: date_trans works with objects of class Date only

The following R syntax explains how to handle the “Error: Invalid input: date_trans works with objects of class Date only”.

For this, we have to apply the as.Date function to the dates within geom_rect. Depending on the format of your dates, you also have to specify the format argument accordingly:

ggp + # Fixing the error geom_rect(data = data, aes(xmin = as.Date("01-01-2024", format = "%d-%m-%Y"), xmax = as.Date("01-01-2025", format = "%d-%m-%Y"), ymin = -Inf, ymax = Inf)) |

ggp + # Fixing the error geom_rect(data = data, aes(xmin = as.Date("01-01-2024", format = "%d-%m-%Y"), xmax = as.Date("01-01-2025", format = "%d-%m-%Y"), ymin = -Inf, ymax = Inf))

As you can see in Figure 2, we have created a ggplot2 time series plot with a rectangle element. No errors anymore!

## Video & Further Resources

If you need more explanations on the R programming code of this tutorial, I recommend watching the following video on my YouTube channel. I’m showing the R programming code of this tutorial in the video.

*The YouTube video will be added soon.*

Also, you may want to have a look at the related tutorials on https://www.statisticsglobe.com/.

- Error in file(file, “rt”) : invalid ‘description’ argument (read.table & csv)
- How to Fix the R Error in stripchart.default(x1, …) : invalid plotting method
- Error in .subset(x, j) : invalid subscript type ‘list’
- Draw Vertical Line to X-Axis of Class Date in ggplot2 Plot
- Fixing Error & Warning Messages in R
- R Graphics Gallery
- The R Programming Language

In summary: In this tutorial you have learned how to **avoid the “Error: Invalid input: date_trans works with objects of class Date only”** in R. Let me know in the comments below, if you have additional questions and/or comments.

### Statistics Globe Newsletter

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R