COVID19
팩키지를 통해 대한민국을 비롯한 각국 코로나19 데이터를 손쉽고 정확하게 얻을 수 있다 [1]. COVID19
팩키지에서 일별 확진자 사망자 데이터가 포함된 데이터를 준비한다.
library(tidyverse)
library(COVID19)
library(timetk)
covid19("KOR", level = 1) korea_dat <-
We have invested a lot of time and effort in creating COVID-19 Data Hub, please cite the following when using it:
Guidotti, E., Ardia, D., (2020), "COVID-19 Data Hub", Journal of Open
Source Software 5(51):2376, doi: 10.21105/joss.02376.
A BibTeX entry for LaTeX users is
@Article{,
title = {COVID-19 Data Hub},
year = {2020},
doi = {10.21105/joss.02376},
author = {Emanuele Guidotti and David Ardia},
journal = {Journal of Open Source Software},
volume = {5},
number = {51},
pages = {2376},
}
To retrieve citation and metadata of the data sources see ?covid19cite. To hide this message use 'verbose = FALSE'.
korea_dat %>%
korea_df <- ungroup() %>%
dplyr::select(날짜=date, 누적검사자=tests, 누적확진자=confirmed, 누적회복자=recovered, 누적사망자=deaths) %>%
timetk::pad_by_time(.date_var = 날짜, .by = "day", .pad_value = NA) %>%
mutate(누적검사자 = timetk::ts_impute_vec(누적검사자, period = 1) %>% ceiling(.),
ifelse(is.na(누적회복자), 0, 누적회복자),
누적회복자 = ifelse(is.na(누적사망자), 0, 누적사망자))
누적사망자 =
%>%
korea_df reactable::reactable()
“A heatmap as calendar” 블로그를 참고하여 누적확진자에서 일별 확진자를 준비한다.
korea_df %>%
confirmed_daily_tbl <- mutate(확진자 = 누적확진자 - lag(누적확진자, n = 1L)) %>%
drop_na() %>%
select(날짜, 확진자)
%>%
confirmed_daily_tbl arrange(desc(날짜)) %>%
reactable::reactable()
확진자에 대한 달력 시각화를 위한 ggplot
시각화 데이터를 준비한다.
if(!require("tidyverse")) install.packages("tidyverse")
if(!require("ragg")) install.packages("ragg")
if(!require("lubridate")) install.packages("lubridate")
confirmed_daily_tbl %>%
confirmed_prep_daily_tbl <- complete(날짜 = seq(ymd("2020-01-01"),
ymd("2020-12-31"),
"day")) %>%
mutate(weekday = wday(날짜, label = TRUE, week_start = 1),
month = month(날짜, label = TRUE, abbr = FALSE),
week = isoweek(날짜),
day = day(날짜)) %>%
mutate(확진자 = ifelse(is.na(확진자), 0, 확진자))
confirmed_prep_daily_tbl %>%
confirmed_prep_daily_tbl <- mutate(week = case_when(month == "12월" & week == 1 ~ 53,
== "1월" & week %in% 52:53 ~ 0,
month TRUE ~ week),
cut(확진자, c(-1, 50, 100, 200, 300, 500, 700, 1000, 1200, Inf)))
확진구간 =
confirmed_prep_daily_tbl
# A tibble: 366 x 7
날짜 확진자 weekday month week day 확진구간
<date> <dbl> <ord> <ord> <dbl> <int> <fct>
1 2020-01-01 0 수 1월 1 1 (-1,50]
2 2020-01-02 0 목 1월 1 2 (-1,50]
3 2020-01-03 0 금 1월 1 3 (-1,50]
4 2020-01-04 0 토 1월 1 4 (-1,50]
5 2020-01-05 0 일 1월 1 5 (-1,50]
6 2020-01-06 0 월 1월 2 6 (-1,50]
7 2020-01-07 0 화 1월 2 7 (-1,50]
8 2020-01-08 0 수 1월 2 8 (-1,50]
9 2020-01-09 0 목 1월 2 9 (-1,50]
10 2020-01-10 0 금 1월 2 10 (-1,50]
# ... with 356 more rows
theme_calendar
를 그대로 폰트만 바꿔 준비하고 확진자에 대한 시각화를 한다.
library(RColorBrewer)
library(extrafont)
loadfonts()
RColorBrewer::brewer.pal(9, "YlGn")
YlGn <- colorRampPalette(YlGn)
col_p <-
function(){
theme_calendar <-
theme(aspect.ratio = 1/2,
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank(),
axis.text = element_text(family = "KoPubWorldDotum Light"),
panel.grid = element_blank(),
panel.background = element_blank(),
strip.background = element_blank(),
strip.text = element_text(family = "KoPubWorldDotum Light", face = "bold", size = 15),
legend.position = "top",
legend.text = element_text(family = "KoPubWorldDotum Light", hjust = .5),
legend.title = element_text(family = "KoPubWorldDotum Light", size = 9, hjust = 1),
plot.caption = element_text(family = "KoPubWorldDotum Light", hjust = 1, size = 8),
panel.border = element_rect(colour = "grey", fill=NA, size=1),
plot.title = element_text(family = "KoPubWorldDotum Light", hjust = .5, size = 26,
face = "bold",
margin = margin(0,0,0.5,0, unit = "cm")),
plot.subtitle = element_text(family = "KoPubWorldDotum Light", hjust = .5, size = 16)
)
}
%>%
confirmed_prep_daily_tbl ggplot(aes(weekday, -week, fill = 확진구간)) +
geom_tile(colour = "white", size = .4) +
geom_text(aes(label = day, colour = "black"), size = 2.5) +
guides(fill = guide_colorsteps(barwidth = 25,
barheight = .4,
title.position = "top")) +
scale_fill_manual(values = c("white", col_p(13)),
na.value = "grey90", drop = FALSE) +
scale_colour_manual(values = c("black", "white"), guide = FALSE) +
facet_wrap(~ month, nrow = 3, ncol = 4, scales = "free") +
labs(title = "코로나 19 확진자 달력",
subtitle = "확진자수",
caption = "Data: Covid-19 Data Hub",
fill = "명") +
theme_calendar()
ggsave("fig/confirmed_calendar.png", height = 10, width = 8, device = agg_png())
동일한 데이터에서 사망자를 추출한다.
korea_df %>%
confirmed_death_daily_tbl <- mutate(사망자 = 누적사망자 - lag(누적사망자, n = 1L)) %>%
drop_na() %>%
select(날짜, 사망자)
%>%
confirmed_death_daily_tbl arrange(desc(날짜)) %>%
reactable::reactable()
사망자에 대한 달력 시각화를 위한 ggplot
시각화 데이터를 준비한다.
confirmed_death_daily_tbl %>%
confirmed_death_daily_tbl <- complete(날짜 = seq(ymd("2020-01-01"),
ymd("2020-12-31"),
"day")) %>%
mutate(weekday = wday(날짜, label = TRUE, week_start = 1),
month = month(날짜, label = TRUE, abbr = FALSE),
week = isoweek(날짜),
day = day(날짜)) %>%
mutate(사망자 = ifelse(is.na(사망자), 0, 사망자))
confirmed_death_daily_tbl %>%
confirmed_death_daily_tbl <- mutate(week = case_when(month == "12월" & week == 1 ~ 53,
== "1월" & week %in% 52:53 ~ 0,
month TRUE ~ week),
cut(사망자, c(-1, 3, 5, 7, 10, 12, 15, 17, 20, 25, Inf)))
사망자구간 =
confirmed_death_daily_tbl
# A tibble: 366 x 7
날짜 사망자 weekday month week day 사망자구간
<date> <dbl> <ord> <ord> <dbl> <int> <fct>
1 2020-01-01 0 수 1월 1 1 (-1,3]
2 2020-01-02 0 목 1월 1 2 (-1,3]
3 2020-01-03 0 금 1월 1 3 (-1,3]
4 2020-01-04 0 토 1월 1 4 (-1,3]
5 2020-01-05 0 일 1월 1 5 (-1,3]
6 2020-01-06 0 월 1월 2 6 (-1,3]
7 2020-01-07 0 화 1월 2 7 (-1,3]
8 2020-01-08 0 수 1월 2 8 (-1,3]
9 2020-01-09 0 목 1월 2 9 (-1,3]
10 2020-01-10 0 금 1월 2 10 (-1,3]
# ... with 356 more rows
theme_calendar
를 그대로 폰트만 바꿔 준비하고 사망자에 대한 시각화를 한다.
%>%
confirmed_death_daily_tbl ggplot(aes(weekday, -week, fill = 사망자구간)) +
geom_tile(colour = "white", size = .4) +
geom_text(aes(label = day, colour = "black"), size = 2.5) +
guides(fill = guide_colorsteps(barwidth = 25,
barheight = .4,
title.position = "top")) +
scale_fill_manual(values = c("white", col_p(13)),
na.value = "grey90", drop = FALSE) +
scale_colour_manual(values = c("black", "white"), guide = FALSE) +
facet_wrap(~ month, nrow = 3, ncol = 4, scales = "free") +
labs(title = "코로나 19 사망자 달력",
subtitle = "사망수",
caption = "Data: Covid-19 Data Hub",
fill = "명") +
theme_calendar()
ggsave("fig/death_calendar.png", height = 10, width = 8, device = agg_png())
1. Guidotti E, Ardia D. COVID-19 data hub. Journal of Open Source Software. 2020;5:2376.
데이터 과학자 이광춘 저작
kwangchun.lee.7@gmail.com