Skip to content

Instantly share code, notes, and snippets.

@djhurio
Created December 6, 2020 19:09
Show Gist options
  • Save djhurio/fd0827fddd81e0fbec7b4188fbab931f to your computer and use it in GitHub Desktop.
Save djhurio/fd0827fddd81e0fbec7b4188fbab931f to your computer and use it in GitHub Desktop.
Deaths by week
# Deaths by week
options("max.print" = 1e4)
library(eurostat)
library(data.table)
library(ggplot2)
library(ISOcodes)
cntry <- as.data.table(ISO_3166_1)
cntry[, .(geo = Alpha_2, cntry = Name)]
# https://ec.europa.eu/eurostat/web/products-datasets/-/demo_r_mwk_ts
dat <- get_eurostat(id = "demo_r_mwk_ts")
setDT(dat)
dat
dat[, .N, keyby = .(sex)]
dat[, .N, keyby = .(unit)]
dat[, .N, keyby = .(geo)]
dat[, .N, keyby = .(time)]
dat[grepl("W99", time)]
dat <- dat[!grepl("W99", time)]
dat[, year := as.integer(substr(time, 1, 4))]
dat[, week := as.integer(substr(time, 6, 7))]
dat[week > 53]
dat[week > 52]
dat[, .N, keyby = .(year)]
dat[, .N, keyby = .(week)]
dcast.data.table(data = dat, formula = week ~ year, fun.aggregate = length)
dat[, y2020 := year == 2020L]
dat[geo == "EL", geo := "GR"]
dat[geo == "UK", geo := "GB"]
dat2 <- merge(dat, cntry[, .(geo = Alpha_2, cntry = Name)],
by = "geo", all.x = T)
dat2[is.na(cntry), .N, keyby = .(geo, cntry)]
pl <- ggplot(data = dat2[sex == "T" & geo != "AD"],
mapping = aes(x = week, y = values, group = year,
colour = y2020, alpha = year)) +
geom_line() +
scale_colour_brewer(palette = "Paired") +
facet_wrap(~ cntry, scales = "free_y") +
ggtitle("Deaths by week (total)", "Datasource: Eurostat [DEMO_R_MWK_TS]") +
theme_bw()
ggsave(filename = "dths-by-wk.png", plot = pl, width = 16, height = 9)
ggsave(filename = "dths-by-wk.pdf", plot = pl, width = 16, height = 9)
@djhurio
Copy link
Author

djhurio commented Dec 6, 2020

Result:
dths-by-wk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment