我有一个名为"数据"的时间序列数据集,其中包含多年采样日期内几口井的水位数据.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叠加灰色矩形来显示井泵的开启时间.我认为我非常接近,但我必须做日期格式化错误(?),因为我不断收到以下错误:
错误:输入无效: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)
micstr.. 21
这个问题似乎出现在你所在的geom_rect
地区(如果没有这个问题,那就是情节).此站点上的其他"date_trans"错误指向需要设置日期as.Date
.所以是的,你是在正确的调试区域.这有效:
在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)
哪个可以帮到你:
这个问题似乎出现在你所在的geom_rect
地区(如果没有这个问题,那就是情节).此站点上的其他"date_trans"错误指向需要设置日期as.Date
.所以是的,你是在正确的调试区域.这有效:
在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)
哪个可以帮到你: