错误:输入无效:date_trans仅适用于Date类的对象 [英] Error: Invalid input: date_trans works with objects of class Date only
问题描述
我有一个称为数据"的时间序列数据集,其中包含多年采样日期中几口井的水位升高数据. data.frame的头看起来像这样:
I have a time series dataset called "Data" which contains water elevation data for several wells over many years of sampling dates. The head of the data.frame 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
我正在尝试使用ggplot绘制每个井的水位随时间变化的曲线,以使我的x轴为日期",我的y轴为高度",并且每个井都以不同的颜色绘制.我已经用下面的代码创建了这个图,这让我很满意.
I am trying to use ggplot to plot water elevation over time for each well, such that my x-axis is "Date," my y-axis is "Elev" and each well is plotted in a different color. I have created this plot with the code below, and it is to my satisfaction.
我的问题是我试图用geom_rect覆盖灰色矩形,以显示抽油机开启的时间段.我想我已经很接近了,但是我在日期格式(?)方面一定做错了,因为我不断收到以下错误:
My problem is that I am trying to overlay gray rectangles with geom_rect to show the periods in which a well pump was on. I think I am very close, but I must be doing something wrong with date formatting (?), because I keep getting the following error:
错误:输入无效:date_trans仅适用于Date类的对象
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"错误 指向需要设置日期的日期使用as.Date
.是的,您在正确的调试区域.这有效:
The problem seems to be in your geom_rect
area (it plots without this). Other "date_trans" errors on this site point to needed to set dates with as.Date
. So yes, you were in the right debug area. This works:
在geom_rect
部分的xmin和xmax调用中包装最小值和最大值:
Wrap your minimum and maximum in xmin and xmax call in geom_rect
section:
aes(xmin = as.Date("2004-04-29", "%Y-%m-%d"),
xmax = as.Date("2004-12-20", "%Y-%m-%d"),
以下供其他人使用的代码
我仅按照@YourEconProf的建议创建了三个数据行.
I created three data lines only as @YourEconProf suggested.
#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)
这能给你带来什么:
这篇关于错误:输入无效:date_trans仅适用于Date类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!