[英]adding custom "+" to many ggplot plots
I am constructing multiple ggplot plots and all of them have the same annotation.我正在构建多个 ggplot 图,并且所有图都具有相同的注释。 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. 2016 年 3 月 9 日到 11 日这几天。我有很多这样的
annotate
部分,添加它们会占用大量代码。 ie IE
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.特别是当我有多个不同的时间序列图时,我想向其中添加相同的
annotate
。 So how can I define the annotate
s into a function and just call on them such as:那么如何将
annotate
定义为一个函数并调用它们,例如:
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)
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:这产生了这个情节:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.