我正在嘗試在 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"))
此代碼的輸出如下所示:
渲染圖
如您所見,我想更改此代碼中有一個非常重復的函式(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很陌生,所以我有點難過,有人有什么建議嗎?
提前致謝!
uj5u.com熱心網友回復:
你的想法是對的。但是您從函式族中選擇了錯誤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
))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/464572.html
上一篇:如何根據給定的位置顯示值串列?
