在POSIXlt中使用带有时间序列的geom_rect

时间:2016-11-25 21:20:18

标签: r ggplot2 time-series posixlt

我正在使用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)

这将返回此图:

Downstream plot

我对此感到非常满意。我剩下要做的就是添加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 ...

我遇到的最接近问题的解决方案已发布在此处:link

我想最终得到这样的东西,但我一直无法利用所提出的解决方案。我认为我的问题是“Date”是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"

1 个答案:

答案 0 :(得分:1)

这是我要做的。

首先生成一个新的data.frame,指定矩形的边界:

rect_df <- 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), DataMatch=c("First","Second","Third","Fourth"))

rect_df$x1=strptime(rect_df$x1,format=("%Y-%m-%d %H:%M:%S"))
rect_df$x2=strptime(rect_df$x2,format=("%Y-%m-%d %H:%M:%S"))

rect_df

然后将其添加到您的情节中:

Dplot +
  geom_rect(data=rect_df, aes(xmin=x1, yxmax=x2, ymin=y1, ymax=y2, fill=DataMatch, alpha=0.5))

您在此处所做的是要求ggplot2引用与此特定geom相同的原始数据的不同data.frame。这里的诀窍是日期需要采用相同的格式。如果没有可重复的示例,则无法测试这是否有效。如果您认真使用R,我建议您仔细阅读此主题以了解如何提出好的问题:How to make a great R reproducible example?

相关问题