【问题标题】:Using geom_rect with a time series in POSIXlt在 POSIXlt 中使用 geom_rect 和时间序列
【发布时间】:2017-04-10 07:01:08
【问题描述】:

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

我看到的最接近问题的解决方案发布在这里:link

我想以这样的方式结束,但我无法利用所提供的解决方案。我认为我的问题是“日期”是 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"

【问题讨论】:

    标签: r ggplot2 time-series posixlt


    【解决方案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 引用与此特定几何图形原始数据不同的data.frame。这里的诀窍是日期需要采用相同的格式。如果没有可重现的示例,就无法测试这是否有效。如果您对使用 R 很认真,我建议您仔细阅读此主题以了解如何提出好的问题:How to make a great R reproducible example?

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 2015-11-08
      • 2022-01-21
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 2016-07-25
      • 2014-02-24
      相关资源
      最近更新 更多