You don't need to add this 'header panel' with cowplot
. You could make a tableGrob
using the package grid
and then combine that with another graph through cowplot::ggdraw()
. But that is unnecessary.
Its much easier to customize such an annotation in ggplot
itself and then combine everything at the end.
First we make a data frame for the header banner:
library(ggplot2)
rects <- data.frame(x=c(0,2,4), xmax=c(2,4,5), y=c(42, 42, 42), z=c("Flowering", "Fruiting", "Ripening"))
Then we can annotate some plot. Note here that I am setting the limits for x and y axes to match the rects
data frame (or the other way around).
ggplot(iris, aes(x=Sepal.Width)) +
geom_histogram(color="white") +
scale_x_continuous(limits=c(0, 5)) +
scale_y_continuous(limits=c(0, 50)) +
# add tiles
geom_rect(data=rects, aes(xmin=x, ymin=40, ymax=45, xmax=xmax, fill=z), inherit.aes = FALSE) +
# add labels
geom_text(data=rects, aes(x=x, y=y, label=z), hjust=0, inherit.aes = FALSE)
Should get something like this:

From here its easy to change this banner, map different colors to the fill aesthetic, and so on. Finally, you combine this pre-annotated plot with the rest in cowplot
.