I am using ggplot2 to make a simple two variable plot. My data set "Downstream" contains data from a downstream salinity probe and data from a hand-held conductivity meeter. I am using this plot to show the large amount of data missed by the hand-held conductivity meter. I hope to highlight a few of the peaks in the data missed by the handheld conductivity meter using the geom_rect
layer. So far my bulky code looks like this:
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)
This returns this plot:
I am reasonably happy with this. All I have left to do is add the geom_rect
layer, but this has proven a challenge. The head of my dataset looks like this:
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
The structure of my data set looks like this:
'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 ...
The closest solution to the problem that I'm having that I have seen is posted here: link
I would like to end up with something like this but I have been unable to utilize the solutions presented.I think my problem is that "Date" is a POSIXlt and not a POSIXct. Resent (and embarrassing) failures include:
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"