如何使用函数将 geom_rect 个对象从数据框添加到 ggplot
How to use function to add geom_rect objects to ggplot from a data frame
我正在尝试在 r 中创建一个复合图,其代码如下:
#Adding initial data
ggp <- ggplot(NULL, aes(x = date, y = covid)) +
geom_spline(data = onsdf,
aes(x = date, y = covid, colour = "ONS Modelled Estimates"), nknots = 90, size = 1.3) +
geom_spline(data = gvtdf,
aes(x = date, y = covid, colour = "Gvt Reported Positive Tests"), nknots = 90, size = 1.3)
#Creating function to add stringency bars
barfunction <- function(date1, date2, alpha){
a <- annotate(geom = "rect",
xmin = as.Date(date1), xmax = as.Date(date2), ymin = 0, ymax = Inf, alpha = alpha, fill = "red")
return(a)
}
#Adding lockdown stringency bars
ggp <- ggp +
barfunction("2020-05-03", "2020-06-01", 0.5) +
barfunction("2020-06-01", "2020-06-15", 0.4) +
barfunction("2020-06-15", "2020-09-14", 0.3) +
barfunction("2020-09-14", "2020-11-05", 0.3) +
barfunction("2020-11-05", "2020-12-02", 0.5) +
barfunction("2020-12-02", "2021-01-06", 0.4) +
barfunction("2021-01-06", "2021-03-29", 0.5) +
barfunction("2021-03-29", "2021-04-12", 0.4) +
barfunction("2021-04-12", "2021-05-17", 0.3) +
barfunction("2021-05-17", "2021-07-19", 0.2) +
barfunction("2021-07-19", "2021-12-08", 0.1) +
barfunction("2021-12-08", "2022-02-24", 0.2) +
#Adding plot labels
ggp <- ggp + labs(title = "Estimated Total Covid-19 Cases vs Reported Positive Cases",
subtitle = "From ONS and HMGvt datasets",
x = "Date (year - month)", y = "Covid Levels") +
scale_y_continuous(labels = scales::comma) +
scale_x_date(limits = as.Date(c("2020-05-03", NA ))) +
scale_colour_manual(name = "Measurement Method",
values = c("ONS Modelled Estimates"="purple",
"Gvt Reported Positive Tests" = "blue"))
此代码的输出如下所示:
Rendered graph
如您所见,我想更改此代码中的一个非常重复的函数 (barfunction)。我认为最好的方法是将 barfunction() 应用于图形的数据转换为数据框,然后尝试在所述数据框上使用函数。这是数据框的头部(称为 strindf)
date1 date2 alpha
2020-05-03 2020-06-01 0.5
2020-06-01 2020-06-15 0.4
2020-06-15 2020-09-14 0.3
2020-09-14 2020-11-05 0.3
我最初尝试使用 apply() 将 strindf 数据添加到我的绘图中,但是我收到一条错误消息(as.Date(date2) 中的错误:缺少参数“date2”,没有默认值).这是我如何将其实现到原始代码中
ggptest <- ggplot(NULL, aes(x = date, y = covid)) +
geom_spline(data = onsdf,
aes(x = date, y = covid, colour = "ONS Modelled Estimates"), nknots = 90, size = 1.3) +
geom_spline(data = gvtdf,
aes(x = date, y = covid, colour = "Gvt Reported Positive Tests"), nknots = 90, size = 1.3) +
apply(strindf, MARGIN = 1 , barfunction) +
theme_minimal() +
scale_y_continuous(labels = scales::comma) +
scale_x_date(limits = as.Date(c("2020-05-03", NA ))) +
scale_colour_manual(name = "Legend",
我对 r 很陌生,所以有点难过,有人有什么建议吗?
提前致谢!
你的想法是对的。但是您从 apply
函数族中选择了错误的函数。因为你有多个参数的函数,所以使用 mapply
或者像我在下面做的那样 purrr::pmap
:
使用一些伪造的随机示例数据:
library(ggplot2)
library(ggformula)
barfunction <- function(date1, date2, alpha) {
annotate(geom = "rect", xmin = as.Date(date1), xmax = as.Date(date2), ymin = 0, ymax = Inf, alpha = alpha, fill = "red")
}
ggplot(NULL, aes(x = date, y = covid)) +
geom_spline(data = df, aes(colour = "ONS Modelled Estimates"), nknots = 90, size = 1.3) +
purrr::pmap(strindf, barfunction) +
theme_minimal() +
scale_y_continuous(labels = scales::comma) +
scale_x_date(limits = as.Date(c("2020-05-03", NA))) +
scale_colour_manual(
name = "Measurement Method",
values = c(
"ONS Modelled Estimates" = "purple",
"Gvt Reported Positive Tests" = "blue"
)
)
#> Warning: Removed 123 rows containing non-finite values (stat_spline).
数据
set.seed(123)
df <- data.frame(
date = seq.Date(as.Date("2020-01-01"), as.Date("2020-12-31"), by = "day"),
covid = runif(366)
)
strindf <- structure(list(date1 = c(
"2020-05-03", "2020-06-01", "2020-06-15",
"2020-09-14"
), date2 = c(
"2020-06-01", "2020-06-15", "2020-09-14",
"2020-11-05"
), alpha = c(0.5, 0.4, 0.3, 0.3)), class = "data.frame", row.names = c(
NA,
-4L
))
我正在尝试在 r 中创建一个复合图,其代码如下:
#Adding initial data
ggp <- ggplot(NULL, aes(x = date, y = covid)) +
geom_spline(data = onsdf,
aes(x = date, y = covid, colour = "ONS Modelled Estimates"), nknots = 90, size = 1.3) +
geom_spline(data = gvtdf,
aes(x = date, y = covid, colour = "Gvt Reported Positive Tests"), nknots = 90, size = 1.3)
#Creating function to add stringency bars
barfunction <- function(date1, date2, alpha){
a <- annotate(geom = "rect",
xmin = as.Date(date1), xmax = as.Date(date2), ymin = 0, ymax = Inf, alpha = alpha, fill = "red")
return(a)
}
#Adding lockdown stringency bars
ggp <- ggp +
barfunction("2020-05-03", "2020-06-01", 0.5) +
barfunction("2020-06-01", "2020-06-15", 0.4) +
barfunction("2020-06-15", "2020-09-14", 0.3) +
barfunction("2020-09-14", "2020-11-05", 0.3) +
barfunction("2020-11-05", "2020-12-02", 0.5) +
barfunction("2020-12-02", "2021-01-06", 0.4) +
barfunction("2021-01-06", "2021-03-29", 0.5) +
barfunction("2021-03-29", "2021-04-12", 0.4) +
barfunction("2021-04-12", "2021-05-17", 0.3) +
barfunction("2021-05-17", "2021-07-19", 0.2) +
barfunction("2021-07-19", "2021-12-08", 0.1) +
barfunction("2021-12-08", "2022-02-24", 0.2) +
#Adding plot labels
ggp <- ggp + labs(title = "Estimated Total Covid-19 Cases vs Reported Positive Cases",
subtitle = "From ONS and HMGvt datasets",
x = "Date (year - month)", y = "Covid Levels") +
scale_y_continuous(labels = scales::comma) +
scale_x_date(limits = as.Date(c("2020-05-03", NA ))) +
scale_colour_manual(name = "Measurement Method",
values = c("ONS Modelled Estimates"="purple",
"Gvt Reported Positive Tests" = "blue"))
此代码的输出如下所示:
Rendered graph
如您所见,我想更改此代码中的一个非常重复的函数 (barfunction)。我认为最好的方法是将 barfunction() 应用于图形的数据转换为数据框,然后尝试在所述数据框上使用函数。这是数据框的头部(称为 strindf)
date1 date2 alpha
2020-05-03 2020-06-01 0.5
2020-06-01 2020-06-15 0.4
2020-06-15 2020-09-14 0.3
2020-09-14 2020-11-05 0.3
我最初尝试使用 apply() 将 strindf 数据添加到我的绘图中,但是我收到一条错误消息(as.Date(date2) 中的错误:缺少参数“date2”,没有默认值).这是我如何将其实现到原始代码中
ggptest <- ggplot(NULL, aes(x = date, y = covid)) +
geom_spline(data = onsdf,
aes(x = date, y = covid, colour = "ONS Modelled Estimates"), nknots = 90, size = 1.3) +
geom_spline(data = gvtdf,
aes(x = date, y = covid, colour = "Gvt Reported Positive Tests"), nknots = 90, size = 1.3) +
apply(strindf, MARGIN = 1 , barfunction) +
theme_minimal() +
scale_y_continuous(labels = scales::comma) +
scale_x_date(limits = as.Date(c("2020-05-03", NA ))) +
scale_colour_manual(name = "Legend",
我对 r 很陌生,所以有点难过,有人有什么建议吗?
提前致谢!
你的想法是对的。但是您从 apply
函数族中选择了错误的函数。因为你有多个参数的函数,所以使用 mapply
或者像我在下面做的那样 purrr::pmap
:
使用一些伪造的随机示例数据:
library(ggplot2)
library(ggformula)
barfunction <- function(date1, date2, alpha) {
annotate(geom = "rect", xmin = as.Date(date1), xmax = as.Date(date2), ymin = 0, ymax = Inf, alpha = alpha, fill = "red")
}
ggplot(NULL, aes(x = date, y = covid)) +
geom_spline(data = df, aes(colour = "ONS Modelled Estimates"), nknots = 90, size = 1.3) +
purrr::pmap(strindf, barfunction) +
theme_minimal() +
scale_y_continuous(labels = scales::comma) +
scale_x_date(limits = as.Date(c("2020-05-03", NA))) +
scale_colour_manual(
name = "Measurement Method",
values = c(
"ONS Modelled Estimates" = "purple",
"Gvt Reported Positive Tests" = "blue"
)
)
#> Warning: Removed 123 rows containing non-finite values (stat_spline).
数据
set.seed(123)
df <- data.frame(
date = seq.Date(as.Date("2020-01-01"), as.Date("2020-12-31"), by = "day"),
covid = runif(366)
)
strindf <- structure(list(date1 = c(
"2020-05-03", "2020-06-01", "2020-06-15",
"2020-09-14"
), date2 = c(
"2020-06-01", "2020-06-15", "2020-09-14",
"2020-11-05"
), alpha = c(0.5, 0.4, 0.3, 0.3)), class = "data.frame", row.names = c(
NA,
-4L
))