0

I have a stuckplot of several plots made using cowplot function in ggplot. I want to add an "header", a boxed text at top of the cowplot. They should be five box, with each box length that follow a precise range of time, which is showed only in the bottom plot x axis.

I tried with annotate on the the cowplot, but I can't see nothing. I have tried with random coordinates for each annotate, just to try. Than, I don't know how to box the text.

I share two pics: 1) One of my cowplot without the "header" enter image description here 2) A pic of an example plot, similar to the plot that i wish enter image description here

Antonio Manco
  • 217
  • 2
  • 14

1 Answers1

1

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:

enter image description here

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.

teofil
  • 2,344
  • 1
  • 8
  • 17
  • So, you say that I could add an annotation at the top of the first plot of the cowplot? Ok, and how can I regolate the x axis spacing? For example, in my first plot I don't have a x, just 3 points! – Antonio Manco Sep 07 '19 at 13:48
  • Not sure I understand. Your x axis appears to be continuous? – teofil Sep 07 '19 at 14:00
  • you should be able to do this with the `show.legend` argument, e.g., geom_rect(..., show.legend = FALSE). Same for `geom_text`. – teofil Sep 07 '19 at 14:30
  • The x-axis position for the label can be independent of the coordinates for the boxes. You just need another column in the `rects` data frame that has x coordinate for labels. Then use that for plotting of the labels. – teofil Sep 07 '19 at 14:35
  • Ok. I've almost done everything. The last one: How can I put this text and rect on te top, outside of the plot grid? I want to avoid to have this text under the legend. – Antonio Manco Sep 07 '19 at 14:58
  • if the legend is `theme(legend.position="top")` you could instead do `theme(legend.position=c(x=0.2, y=0.8), legend.direction="horizontal")` and adjust the y coordinate for the legend so that its below the banner. – teofil Sep 07 '19 at 15:19
  • Yes, of course. I've done in this way. Thank you so much for your help!!! – Antonio Manco Sep 07 '19 at 15:27