I am transitioning from using a heatmap created using the lattice "levelplot" to one using ggplot2, but I'm running into an issue in how the two packages deal with uneven x-axis variables.
Here is my data with an uneven time series:
MyTimes<-as.POSIXct(c(rep("2020-10-01 10:15:00",3),rep("2020-10-01 11:25:00",3), rep("2020-10-01 11:45:00",3), rep("2020-10-01 12:23:00",3), rep("2020-10-01 14:15:00",3),
rep("2020-10-01 15:15:00",3), rep("2020-10-01 16:32:00",3), rep("2020-10-01 16:20:00",3), rep("2020-10-01 18:15:00",3), rep("2020-10-01 19:15:00",3),
rep("2020-10-02 10:15:00",3), rep("2020-10-02 11:15:00",3), rep("2020-10-02 12:15:00",3), rep("2020-10-02 13:33:00",3), rep("2020-10-02 20:15:00",3),
rep("2020-10-03 10:15:00",3), rep("2020-10-03 15:15:00",3), rep("2020-10-03 19:15:00",3), rep("2020-10-05 10:15:00",3), rep("2020-10-05 12:15:00",3)))
MyY<-rep(seq(1,3),20)
MyValue<-runif(60, min = 0, max = 25)
MyData<-data.frame(MyTimes, MyY, MyValue)
And resulting plot:
library(lattice)
levelplot(MyValue ~ MyTimes * MyY,
data = MyData,
ylim=c(3,0),
aspect=0.4)
Using the above levelplot code, the plot was "too smoothed" in that it was smoothing over large time gaps. I was able to fix this by inserting blocks of NA values that broke up the time-series plot, so that only smaller gaps were smoothed over. I realize there is some judgment on my part as to what constitutes a "large time gap", but I'm OK manually deciding this like so:
GapTimes<-as.POSIXct(c(rep("2020-10-01 19:16:00",3),rep("2020-10-02 20:16:00",3),rep("2020-10-03 19:16:00",3)))
MyGapY<-rep(seq(1,3),3)
MyGapValues<-rep(NA,9)
Gap<-data.frame(GapTimes,MyGapY,MyGapValues)
colnames(Gap)<-colnames(MyData)
MyData2<-rbind(MyData,Gap)
So now when I plot my heatmap, I have the small gaps smoothed over, and the larger ones are not,
levelplot(MyValue ~ MyTimes * MyY,
data = MyData2,
ylim=c(3,0),
aspect=0.4)
What I'd like to do is switch over to a Ggplot2 heatmap, but the below Ggplot2 code has zero smoothing, and the large amount of white space (even between the smallest gaps) makes it difficult to visualize change over time
library(ggplot2)
ggplot(data = MyData2,aes(x=MyTimes, y=MyY)) +
geom_raster(aes(fill = MyValue), interpolate=TRUE)+
scale_y_reverse(lim=c(3,0))+
scale_fill_gradientn(limits=c(0,25), colors=c("blue","red"),oob = scales::squish)+
theme_bw()+
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
Is it possible to reproduce the above levelplot smoothing using a similar Ggplot2 based method?
This is a similar question to this one, which did not seem to have a satisfying answer: How can I remove space/gaps between continuous x-values in geom_raster