Search code examples
rplotlatticelevelplot

Overlay points on a timeseries level plot


I have a level plot using timeseries data of temperature. The level plot looks like this:

Date<-as.Date(c("2017-01-01","2017-01-01","2017-01-01","2017-01-02","2017-01-02","2017-01-02","2017-01-03","2017-01-03","2017-01-03","2017-01-4","2017-01-4","2017-01-4"))
Date<-as.POSIXct(Date)
Y<-c(1,2,3,1,2,3,1,2,3,1,2,3)
Temp<-c(20,23,25,19,20,21,18,19,20,13,17,19)
DF<-data.frame(Date,Y,Temp)


dev.new(width=15, height=6)
levelplot(Temp ~ Date * Y, data = DF,ylim=c(3,1),
  xlab = "TimeStamp", ylab = "Temp",
  main = "Test", aspect=0.4,
  col.regions =colorRampPalette(c('blue','red')),at=seq(13, 25, length.out=120))

I would like to overlay 3 points on this plot. Ideally I would like a solid point on Jan 1 at Y=2, Jan 2 at Y= 2.3, and Jan 3 at Y=1.2


Solution

  • You can use layer from latticeExtra like this:

    library(latticeExtra)
    p <- levelplot(Temp ~ Date * Y, data = DF,ylim=c(3,1),
                   xlab = "TimeStamp", ylab = "Temp",
                   main = "Test", aspect=0.4,
                   col.regions = colorRampPalette(c('blue','red')),
                                                 at=seq(13, 25, length.out=120))
    
    p + layer(panel.points(c(DF$Date[1], DF$Date[4], DF$Date[7]), c(2, 2.3, 1.2), 
                           pch = 1, col = "black")) 
    

    Which outputs this:

    enter image description here

    You can try pch = 19 if you want the points solid.