-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpyramid-plot-week.R
173 lines (152 loc) · 4.64 KB
/
pyramid-plot-week.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# clean environment
remove(list = ls())
# required packages
library(ggplot2)
library(ggrepel)
library(zoo)
library(lme4)
library(plyr)
library(dplyr)
library(scales)
library(ggpubr)
library(grid)
library(gridExtra)
library(magrittr)
library(ggpol)
library(reshape2)
library(XML)
library(patchwork)
# import data
dat_all <- read.csv("https://epistat.sciensano.be/Data/COVID19BE_CASES_AGESEX.csv",
stringsAsFactors = TRUE
)
dat_all$DATE <- as.Date(dat_all$DATE, "%Y-%m-%d")
## Recoding dat_all$SEX into dat_all$SEX_rec
dat_all$SEX <- recode_factor(dat_all$SEX,
"F" = "Female",
"M" = "Male"
)
# subset for period
start <- as.Date("2020-03-01")
end <- as.Date("2020-05-31")
dat <- subset(dat_all, DATE >= start & DATE <= end)
nweeks <- as.numeric(end - start) / 7
# aggregate new cases by province and date
dat <- aggregate(CASES ~ AGEGROUP + SEX, dat, sum)
# cases per week
dat$CASES <- dat$CASES / nweeks
# create limits for number of cases for all periods
lower_limit <- -500
upper_limit <- 600
# Create plots in engl
p1 <- ggplot(data = dat) +
geom_bar(aes(AGEGROUP, CASES, group = SEX, fill = SEX),
stat = "identity",
subset(dat, SEX == "Female")
) +
geom_bar(aes(AGEGROUP, -CASES, group = SEX, fill = SEX),
stat = "identity",
subset(dat, SEX == "Male")
) +
scale_y_continuous(
limits = c(lower_limit, upper_limit),
breaks = seq(lower_limit, upper_limit, 200),
labels = abs(seq(lower_limit, upper_limit, 200))
) +
coord_flip() +
theme_minimal() +
labs(
title = "COVID-19 cases in Belgium",
subtitle = paste0(format(start, format = "%d/%m/%Y"), " - ", format(end, format = "%d/%m/%Y")),
# caption = "Niko Speybroeck (@NikoSpeybroeck), Antoine Soetewey (@statsandr) \n Data: https://epistat.wiv-isp.be/covid/",
x = "Age group",
y = "Number of cases per week"
) +
theme(
legend.position = c(.95, .15),
legend.justification = c("right", "top"),
legend.box.just = "right",
legend.margin = margin(6, 6, 6, 6),
legend.title = element_blank(),
plot.title = element_text(face = "bold"),
plot.subtitle = element_text(face = "bold")
)
p1
# subset for period
start <- as.Date("2020-06-01")
end <- as.Date("2020-08-31")
dat <- subset(dat_all, DATE >= start & DATE <= end)
nweeks <- as.numeric(end - start) / 7
# aggregate new cases by province and date
dat <- aggregate(CASES ~ AGEGROUP + SEX, dat, sum)
# cases per week
dat$CASES <- dat$CASES / nweeks
# Create plots in engl
p2 <- ggplot(data = dat) +
geom_bar(aes(AGEGROUP, CASES, group = SEX, fill = SEX),
stat = "identity",
subset(dat, SEX == "Female")
) +
geom_bar(aes(AGEGROUP, -CASES, group = SEX, fill = SEX),
stat = "identity",
subset(dat, SEX == "Male")
) +
scale_y_continuous(
limits = c(lower_limit, upper_limit),
breaks = seq(lower_limit, upper_limit, 200),
labels = abs(seq(lower_limit, upper_limit, 200))
) +
coord_flip() +
theme_minimal() +
labs( # title = "COVID-19 cases in Belgium",
subtitle = paste0(format(start, format = "%d/%m/%Y"), " - ", format(end, format = "%d/%m/%Y")),
# caption = "Niko Speybroeck (@NikoSpeybroeck), Antoine Soetewey (@statsandr) \n Data: https://epistat.wiv-isp.be/covid/",
x = "Age group",
y = "Number of cases per week"
) +
theme(
legend.position = "none",
plot.subtitle = element_text(face = "bold")
)
p2
# subset for period
start <- as.Date("2020-09-01")
end <- as.Date(max(dat_all$DATE, na.rm = TRUE))
dat <- subset(dat_all, DATE >= start & DATE <= end)
nweeks <- as.numeric(end - start) / 7
# aggregate new cases by province and date
dat <- aggregate(CASES ~ AGEGROUP + SEX, dat, sum)
# cases per week
dat$CASES <- dat$CASES / nweeks
# Create plots in engl
p3 <- ggplot(data = dat) +
geom_bar(aes(AGEGROUP, CASES, group = SEX, fill = SEX),
stat = "identity",
subset(dat, SEX == "Female")
) +
geom_bar(aes(AGEGROUP, -CASES, group = SEX, fill = SEX),
stat = "identity",
subset(dat, SEX == "Male")
) +
scale_y_continuous(
limits = c(lower_limit, upper_limit),
breaks = seq(lower_limit, upper_limit, 200),
labels = abs(seq(lower_limit, upper_limit, 200))
) +
coord_flip() +
theme_minimal() +
labs( # title = "COVID-19 cases in Belgium",
subtitle = paste0(format(start, format = "%d/%m/%Y"), " - ", format(end, format = "%d/%m/%Y")),
caption = "Niko Speybroeck (@NikoSpeybroeck), Antoine Soetewey (@statsandr) \n Data: https://epistat.wiv-isp.be/covid/",
x = "Age group",
y = "Number of cases per week"
) +
theme(
legend.position = "none",
plot.subtitle = element_text(face = "bold")
)
p3
p1 + p2 + p3
# save plot
ggsave("pyramid-plot-week-limit.png")
# ggsave("pyramid-plot-week.pdf")