作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为“数据”的时间序列数据集,其中包含多年采样日期内几口井的水位高程数据。 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
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"),
#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)
关于r - 错误 : Invalid input: date_trans works with objects of class Date only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41815365/
我有一个名为“数据”的时间序列数据集,其中包含多年采样日期内几口井的水位高程数据。 data.frame 的头部如下所示: Date Well Elev
我正在尝试使用 rpy2 包从 python 脚本调用 ggplot2 来绘制时间序列数据。当我尝试调整 x 刻度的日期限制时出现错误。 rpy2 文档提供了此指导 ( https://rpy2.re
我是一名优秀的程序员,十分优秀!