gpt4 book ai didi

r - 错误 : Invalid input: date_trans works with objects of class Date only

转载 作者:行者123 更新时间:2023-12-03 14:47:48 26 4
gpt4 key购买 nike

我有一个名为“数据”的时间序列数据集,其中包含多年采样日期内几口井的水位高程数据。 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 轴是“高程”,并且每口井都以不同的颜色绘制。我用下面的代码创建了这个图,我很满意。

我的问题是我试图用 geom_rect 覆盖灰色矩形以显示井泵运行的时间段。我认为我非常接近,但我一定在日期格式(?)方面做错了什么,因为我不断收到以下错误:

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



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

这是我的代码:
#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)

最佳答案

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

关于r - 错误 : Invalid input: date_trans works with objects of class Date only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41815365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com