R 时间序列月发生率
我想做一个关于年或月发病率的时间序列图,现在我有了每日数据,如何将每日图更改为月图或年图 我有2013年1月1日至2017年12月31日的数据,样本数据如下:R 时间序列月发生率,r,R,我想做一个关于年或月发病率的时间序列图,现在我有了每日数据,如何将每日图更改为月图或年图 我有2013年1月1日至2017年12月31日的数据,样本数据如下: dput(head(Incidence_byday,n=20)) structure(list(DATE = structure(c(15706, 15707, 15708, 15709, 15710, 15711, 15712, 15713, 15714,
dput(head(Incidence_byday,n=20))
structure(list(DATE = structure(c(15706, 15707, 15708, 15709, 15710, 15711, 15712,
15713, 15714, 15715, 15716, 15717, 15718, 15719,
15720, 15721, 15722, 15723, 15724, 15725),
class = "Date"),
Inpatient = c(5L, 7L, 3L, 52L, 111L, 150L, 177L, 251L, 292L, 321L, 338L,
178L, 124L, 346L, 368L, 354L, 375L, 461L, 220L, 148L),
HAI = c(0, 0, 0, 4, 1, 1, 3, 10, 8, 13, 9, 0, 12, 10, 11, 11, 15, 10, 7, 8),
Incidence = c(0, 0, 0, 7.69230769230769, 0.900900900900901, 0.666666666666667,
1.69491525423729, 3.98406374501992, 2.73972602739726,
4.04984423676012, 2.66272189349112, 0, 9.67741935483871,
2.89017341040462, 2.98913043478261, 3.10734463276836,
4, 2.16919739696312, 3.18181818181818, 5.40540540540541)),
row.names = c(NA, 20L), class = "data.frame")
#install.packages("ggplot2")
#install.packages("lubridate")
library(ggplot2)
library(lubridate)
theme_set(theme_bw())
ggplot(Incidence_byday, aes(x=DATE)) +
geom_line(aes(y=Incidence)) +
labs(title="Incidence trend", y="Incidence %")
结果如下:
要获得其他时间段的发病率,您可以使用lubridate将每个日期指定给特定的周、月或年,然后分组+汇总和变异以获得该时间段的统计数据:
library(tidyverse); library(lubridate)
Incidence_byweek <- Incidence_byday %>%
group_by(DATE = floor_date(DATE, "7 days")) %>% # e.g., or "1 month" or "1 year"
summarize(Inpatient = sum(Inpatient),
HAI = sum(HAI)) %>%
mutate(Incidence = 100 * HAI / Inpatient)
不要将数据添加为图片。使用dput。阅读和
ggplot(Incidence_byweek, aes(x=DATE)) +
geom_line(aes(y=Incidence)) +
labs(title="Incidence trend", y="Incidence %")