我正在使用 ggplot2 制作一个简单的两个变量图。我的数据集“下游”包含来自下游盐度探头的数据和来自手持电导率仪的数据。我正在使用此图来显示手持电导率计遗漏的大量数据。我希望突出显示使用该geom_rect
层的手持式电导仪遗漏的数据中的一些峰值。到目前为止,我庞大的代码如下所示:
library(ggplot2)
> Downstream <- read.csv("~/Desktop/Downstream.csv")
> Date= as.character(Downstream$Date)
> Date=strptime(Date,format=("%m/%d/%y %H:%M"))
> Downstream$Date=Date
> Dplot <- ggplot(data= Downstream,aes(x=Date))
> Dplot <- Dplot + geom_line(aes (y=Conductivity), color="blue")
> Dplot <- Dplot + geom_point(aes(y=Thermo.Conductivity),color="red")
> Dplot<- Dplot + ggtitle("Logger and Hand Sample \nConductivity vs. Time ") +
+ theme(plot.title = element_text(lineheight=.8, face="bold"))
> Dplot <- Dplot + ylim(0,4000)
这将返回此图:
我对此感到相当满意。我剩下要做的就是添加geom_rect
图层,但这已被证明是一个挑战。我的数据集的头部如下所示:
Date Water.Level Conductivity Thermo.Conductivity
1 2013-12-17 22:00:00 0.216 487.79 NA
2 2013-12-17 22:15:00 0.210 487.38 NA
3 2013-12-17 22:30:00 0.220 485.77 NA
4 2013-12-17 22:45:00 0.225 485.37 NA
5 2013-12-17 23:00:00 0.236 484.96 NA
6 2013-12-17 23:15:00 0.241 486.19 NA
我的数据集的结构如下所示:
'data.frame': 23472 obs. of 4 variables:
$ Date : POSIXlt, format: "2013-12-17 22:00:00" "2013-12-17 22:15:00" ...
$ Water.Level : num 0.216 0.21 0.22 0.225 0.236 0.241 0.238 0.231 0.217 0.235 ...
$ Conductivity : num 488 487 486 485 485 ...
$ Thermo.Conductivity: num NA NA NA NA NA NA NA NA NA NA ...
我看到的问题的最接近的解决方案发布在这里:链接
我想以这样的方式结束,但我无法利用所提供的解决方案。我认为我的问题是“日期”是 POSIXlt 而不是 POSIXct。怨恨(和令人尴尬)的失败包括:
Dplot<- Dplot+ annotate(geom_rect(),x=Date,y=Conductivity,xmin= 2014-01-03 04:15,xmax=2014-01-05 12:30,ymin=-Inf,ymax=Inf,)
Error: unexpected numeric constant in "Dplot<- Dplot+ annotate(geom_rect(),x=Date,y=Conductivity,xmin= 2014-01-03 04"
Dplot<- Dplot+ annotate("rect",fill="gray",alpha(=.5),xmin= 2014-01-03 04:15,xmax=2014-01-05 12:30,ymin=-Inf,ymax=Inf,)
Error: unexpected '=' in "Dplot<- Dplot+ annotate("rect",fill="gray",alpha(="
d.water<- data.frame(x1=c(2014-01-03 04:15:00,2014-02-18 11:45:00,2014-03-17 12:15:00,2014-05-14 18:15:00),x2=c(2014-01-05 12:30:00,2014-02-20 3:30:00,2014-03- 21 14:30:00,2014-05-16 05:15:00),y1=c(-Inf,-Inf,-Inf,-Inf),y2=c(Inf,Inf,Inf,Inf))
Error: unexpected numeric constant in "d.water<- data.frame(x1=c(2014-01-03 04"