user8959427
user8959427

Reputation: 2067

adding custom "+" to many ggplot plots

I am constructing multiple ggplot plots and all of them have the same annotation. I have a time series plot and on certain days I annotate a verticle line. So say I have daily data such that I have the following plot:

  ggplot(aes(x = date, y = depVar)) +
  geom_line()

I want to draw verticle lines at certain dates so I add the following:

      ggplot(aes(x = date, y = depVar)) +
      geom_line()
annotate("rect", xmin = as.Date("2016-03-09"), xmax = as.Date("2016-03-11"), 
         ymin = -Inf, ymax = Inf, alpha = .5)

which fills in the days from 09 to 11th of March 2016. I have many of these annotate parts and adding them takes up a lot of code. i.e.

      ggplot(aes(x = date, y = depVar)) +
      geom_line()
annotate("rect", xmin = as.Date("2016-03-09"), xmax = as.Date("2016-03-11"), 
         ymin = -Inf, ymax = Inf, alpha = .5) +
annotate("rect", xmin = as.Date("2017-01-03"), xmax = as.Date("2017-02-01"), 
         ymin = -Inf, ymax = Inf, alpha = .5) +
annotate("rect", xmin = as.Date("2018-01-03"), xmax = as.Date("2018-02-01"), 
         ymin = -Inf, ymax = Inf, alpha = .5) +
annotate("rect", xmin = as.Date("2019-03-09"), xmax = as.Date("2019-03-11"), 
         ymin = -Inf, ymax = Inf, alpha = .5) +
annotate("rect", xmin = as.Date("2020-03-09"), xmax = as.Date("2020-03-11"), 
         ymin = -Inf, ymax = Inf, alpha = .5)

Especially when I have multiple different time series plots I want to add the same annotate additions to. So how can I define the annotates into a function and just call on them such as:

      ggplot(aes(x = date, y = depVar)) +
      geom_line() +
my_annotations()

I have tried:

my_annotations <- function(protocol_fill_color = "grey25"){
ymin = -Inf, ymax = Inf, alpha = .5) %+replace%
    annotate("rect", xmin = as.Date("2018-01-03"), xmax = as.Date("2018-02-01"), 
             ymin = -Inf, ymax = Inf, alpha = .5) %+replace%
    annotate("rect", xmin = as.Date("2019-03-09"), xmax = as.Date("2019-03-11"), 
             ymin = -Inf, ymax = Inf, alpha = .5) %+replace%
    annotate("rect", xmin = as.Date("2020-03-09"), xmax = as.Date("2020-03-11"), 
             ymin = -Inf, ymax = Inf, alpha = .5)  
}

But I cannot seem to get this to work.

EDIT: Reproducible data:

library(tidyquant)
d <- tq_get("IBM")

d %>% 
  ggplot(aes(x = date, y = open)) +
  geom_line() +
  annotate("rect", xmin = as.Date("2012-03-09"), xmax = as.Date("2016-03-11"), 
           ymin = -Inf, ymax = Inf, alpha = .5) +
  annotate("rect", xmin = as.Date("2017-01-03"), xmax = as.Date("2017-04-01"), 
           ymin = -Inf, ymax = Inf, alpha = .5)

Upvotes: 2

Views: 61

Answers (1)

ulfelder
ulfelder

Reputation: 5335

If you're trying to annotate the same periods across all your plots, you could do this with a function like...

plot_1 <- d %>% 
  ggplot(aes(x = date, y = open)) +
  geom_line()

add_blocks <- function(my_plot) {

  my_plot + 
    annotate("rect", xmin = as.Date("2012-03-09"), xmax = as.Date("2016-03-11"), 
             ymin = -Inf, ymax = Inf, alpha = .5) +
    annotate("rect", xmin = as.Date("2017-01-03"), xmax = as.Date("2017-04-01"), 
             ymin = -Inf, ymax = Inf, alpha = .5)

}

add_blocks(plot_1)

That produces this plot:

enter image description here

Upvotes: 4

Related Questions