, gridExtra, facet_wrap - Vlo geom_tile. df:
Libraries:
library(data.table)
library(reshape2)
library(ggplot2)
library(gridExtra)
Convert to data table, then add y values for time1 and time2 with .Nand group for each
dt <- as.data.table(df)
dt[, y1 := 1:.N, by = time1][, y2 := 1:.N, by = time2]
Then create a separate ggplot object for each with specific scaling and color options:
p1 <- ggplot(dt) +
geom_tile(aes(x = time1, y = y1), fill = "white", col = "black") +
coord_cartesian(xlim = c(0, 10), ylim = c(0.5, 4.5), expand = TRUE) +
scale_x_continuous(breaks = 0:10)+
theme_classic() +
theme(axis.line.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
plot.margin = unit(c(6,1,1,0.5), "cm"))
p2 <- ggplot(dt) +
geom_tile(aes(x = time2, y = y2, fill = diff), col = "black") +
scale_fill_gradientn(colours = c("#237018", "white", "red4"), values = c(0, 0.8, 1)) +
coord_cartesian(xlim = c(0, 10), ylim = c(0.5, 4.5), expand = TRUE) +
scale_x_continuous(breaks = 0:10) +
theme_classic() +
theme(axis.line.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
plot.margin = unit(c(6,1,1,0.5), "cm"),
legend.position = c(0, 1.55),
legend.direction = "horizontal")
Then use grid.arrangeto offset their adjacent objects:
grid.arrange(p1, p2, nrow = 1)
Conclusion:

Failed to get the right legend, maybe a little more work is needed.