错误:输入无效:date_trans仅适用于Date类的对象

时间:2017-01-23 20:48:46

标签: r ggplot2

我有一个名为" Data"的时间序列数据集。其中包含多年采样日期内几口井的水位数据。 data.frame的头部如下所示:

#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)

我正在尝试使用ggplot来绘制每个井的水位随时间的变化,这样我的x轴就是&#34;日期,&#34;我的y轴是&#34; Elev&#34;并且每个孔以不同的颜色绘制。我用下面的代码创建了这个情节,这令我满意。

我的问题是我试图用geom_rect叠加灰色矩形来显示井泵的开启时间。我认为我非常接近,但我必须对日期格式化做错事(?),因为我一直收到以下错误:

  

错误:输入无效:date_trans仅适用于类Date的对象

有任何帮助吗?提前谢谢!

这是我的代码:

trim

1 个答案:

答案 0 :(得分:16)

这个问题似乎出现在你的geom_rect区域(没有这个问题的情节)。其他“date_trans”错误on this site指向需要使用as.Date设置日期。所以是的,你是在正确的调试区域。这有效:

geom_rect部分中的xmin和xmax通话中包含您的最小值和最大值:

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 of data chart