【问题标题】:Error: Invalid input: date_trans works with objects of class Date only错误:输入无效:date_trans 仅适用于 Date 类的对象
【发布时间】:2017-01-23 20:48:45
【问题描述】:

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

           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

我正在尝试使用 ggplot 绘制每口井随时间变化的水位高度,这样我的 x 轴是“日期”,我的 y 轴是“Elev”,并且每口井都以不同的颜色绘制。我用下面的代码创建了这个图,这让我很满意。

我的问题是我试图用 geom_rect 覆盖灰色矩形以显示井泵打开的时间段。我想我已经很接近了,但是我必须在日期格式(?)方面做错了,因为我不断收到以下错误:

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

有什么帮助吗?提前致谢!

这是我的代码:

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

【问题讨论】:

  • 你能提供dput(Data)的输出吗?
  • 嗨,J. Con,对不起——我是新来这里发帖的,'dput(Data)' 的输出对于评论和原始帖子来说都太长了。关于如何获得输出的任何建议?
  • 阅读本文以获取一些提示。 r-bloggers.com/…
  • 尝试仅使用数据中的前三个观察值,然后运行 ​​dput(data)

标签: r ggplot2


【解决方案1】:

问题似乎出在您的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)

这会让你得到这个:

【讨论】:

  • 非常感谢 micstr!我曾尝试使用 as.Date 但没有意识到我必须指定格式。
猜你喜欢
  • 1970-01-01
  • 2018-12-04
  • 2021-07-30
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多