0

Using ggplot2, I have two vertically aligned geom_line() objects and I want to draw a vertical rectangle across both plots in the same x_axis range.

For example, how could this rectangle be extended down across the plot below?

paquetes <- c("ggplot2", "gcookbook", "cowplot")
lapply(paquetes, require, character.only = TRUE)

p <- ggplot(subset(climate, Source=="Berkeley"), aes(x=Year, y=Anomaly10y)) +
  geom_line() + 
  annotate("rect", xmin=1950, xmax=1980, ymin=-1, ymax=1, alpha=.1,
             fill="blue") +
  theme(axis.line.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.title.x = element_blank())

w <- ggplot(subset(climate, Source=="NASA"), aes(x=Year, y=Anomaly1y)) +
  geom_line()


both <- cowplot::plot_grid(p + theme(legend.position = "none"),
                                w + theme(legend.position = "none"),
                                align = "v",
                                ncol = 1,
                                nrow = 2)

both

enter image description here

AJMA
  • 1,134
  • 2
  • 13
  • 28
  • 2
    maybe useful https://stackoverflow.com/questions/15016995/how-to-align-multiple-ggplot2-plots-and-add-shadows-over-all-of-them – user20650 Jun 22 '17 at 13:12
  • Also, your example is not reproducible since we don't have `climate`. – Axeman Jun 22 '17 at 13:26

0 Answers0