Error: invalid input: date_trans works with objects of class Date only

I have a set of time series called Data that contains water height data for several wells over the years. The data.frame chapter looks like this:

           Date            Well   Elev
1    2002-05-23            MW-3 929.04
2    2002-05-29            MW-3 929.39
3    2002-05-31            MW-3 929.37
4    2002-06-05            MW-3 929.36
5    2002-06-12            MW-3     NA
6    2002-06-13            MW-3 929.47
7    2002-06-19            MW-3 929.42
8    2002-06-26            MW-3 930.02
9    2002-07-05            MW-3 930.00

I am trying to use ggplot to determine the height of the water over time for each well, so my x-axis is β€œDate”, my y-axis is β€œElev” and each well is plotted in a different color. I created this plot with the code below, and it is to my satisfaction.

My problem is that I am trying to overlay gray rectangles with geom_rect to show the periods in which the well pump was turned on. I think I'm very close, but I have to do something wrong with date formatting (?), Because I keep getting the following error:

Error: invalid input: date_trans works with class objects Date only

Any help? Thanks in advance!

Here is my code:

#Import and fix up data
Data = read.csv("water_elevation_for_R_date.csv", stringsAsFactors=FALSE)
colnames(Data)[1] <- "Date"
Data$Date = as.Date(Data$Date, format = "%m/%d/%Y")
Data$Well <- as.factor(Data$Well)
Data$Elev <- as.numeric(Data$Elev)

#Load ggplot and scales
library(ggplot2)
library(scales)

#Create graph
ggplot(data= Data, aes(x = Date, y = Elev, group = Well, colour = Well)) +
geom_line(size = 0.75) +
xlab("") + ylab("Elevation (ft.)") +
scale_color_brewer(palette = "Spectral") +
scale_x_date(breaks = date_breaks("2 year"),
             labels = date_format("%Y")) +
theme_bw()+
theme(plot.background = element_blank(), 
      panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(), 
      panel.border = element_blank(), 
      axis.line.x = element_line(color = "black"),
      axis.line.y = element_line(color = "black")) +
geom_rect(data = Data, 
          aes(xmin = "2004-04-29", 
              xmax = "2004-12-20",
              ymin = -Inf, 
              ymax = Inf),
          fill = "gray", 
          alpha = 0.5)
+6
source share
1 answer

The problem seems to be in your area geom_rect(this is without it). Other "date_trans" errors on this site indicate the need to set dates using as.Date. So yes, you were in the right debugging area. It works:

xmin xmax geom_rect:

aes(xmin = as.Date("2004-04-29", "%Y-%m-%d"), 
    xmax = as.Date("2004-12-20",  "%Y-%m-%d"),

@YourEconProf.

#Import and fix up data
#Data = read.csv("water_elevation_for_R_date.csv", stringsAsFactors=FALSE)
#Date            Well   Elev
#1    2002-05-23            MW-3 929.04
#2    2002-05-29            MW-3 929.39
#3    2002-05-31            MW-3 929.37
# etc...
Data = data.frame(Date = c(as.Date("2002-05-23", "%Y-%m-%d"),
                           as.Date("2002-05-29", "%Y-%m-%d"),
                           as.Date("2002-05-31", "%Y-%m-%d")),
                  Well = c("MW-3","MW-3","MW-3"),
                  Elev = c(929.04, 929.39, 929.37))

colnames(Data)[1] <- "Date"
Data$Date = as.Date(Data$Date, format = "%m/%d/%Y")
Data$Well <- as.factor(Data$Well)
Data$Elev <- as.numeric(Data$Elev)

#Load ggplot and scales
library(ggplot2)
library(scales)

#Create graph
ggplot(data= Data, aes(x = Date, y = Elev, group = Well, colour = Well)) +
  geom_line(size = 0.75) +
  xlab("") + ylab("Elevation (ft.)") +
  scale_color_brewer(palette = "Spectral") +
  scale_x_date(breaks = date_breaks("2 year"),
               labels = date_format("%Y")) +
  theme_bw()+
  theme(plot.background = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.border = element_blank(), 
        axis.line.x = element_line(color = "black"),
        axis.line.y = element_line(color = "black")) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2004-04-29", "%Y-%m-%d"), 
                xmax = as.Date("2004-12-20",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "gray", 
            alpha = 0.5)

:

3lines data charts

+5

Source: https://habr.com/ru/post/1014343/


All Articles