Reputation: 2336
I have a time series plot of two variables (tidal elevation and data logger temperature) and have been looking at how they interact in the hopes of factoring out (in a exploratory/qualitative way) the influence of first upon the second. However, in some cases the influence has been the opposite of what I expected and I'm thinking that a third variable (weather) is probably having an influence and I'd like to check it out.
It would be really helpful to just overlay some colors or grayscale over the plot that corresponds to different levels of the weather variable. For instance, clear days could be light blue, overcast days could be dark blue, etc. Does anyone have an idea of how to do this? I have included a bit of my data as well as a graph. (For graph: blue line is tide, red line is temp, black line at tide=0 is when the logger is exposed).
head(Tide.Temp, 4)
Date.Dab Temp.Dab Tide.Dab Weath.Dab
1 2013-07-28 21:30:00 68.9 9.4 Clear
2 2013-07-28 22:00:00 68.9 9.5 Clear
3 2013-07-28 22:30:00 68.9 9.5 Clear
4 2013-07-28 23:00:00 68.9 9.2 Clear
Upvotes: 0
Views: 536
Reputation: 8267
I would recommend plotting a horizontal line somewhere where it does not obscure your time series (e.g., at the overall minimum y value), color-coded for weather.
In the code below, I code weather as a factor
and use the fact that R stores factor
s internally as integers, so we can simply say col=weather
, and the factor
will be silently converted to integers, and then the i-th color from the palette()
will be used.
library(zoo)
set.seed(1)
temp <- cumsum(rnorm(1000))
timestamps <- as.POSIXct("2014-01-01 00:00")+seq_along(temp)*30*60
weather <- as.factor(rep(sample(c("Clear","Cloudy","Rainy"),200,replace=TRUE),
times=rpois(200,10))[seq_along(temp)])
temp.zoo <- zoo(x=temp,order.by=timestamps)
zero.zoo <- zoo(x=min(temp),order.by=timestamps)
plot(temp.zoo,xlab="",ylab="")
points(zero.zoo,col=weather,pch=22)
legend(x="topright",pch=22,
col=seq_along(levels(weather)),pt.bg=seq_along(levels(weather)),
legend=levels(weather))
Upvotes: 1