Commit 63c7aeb6 authored by Facundo Muñoz's avatar Facundo Muñoz ®️
Browse files

Report Europe: data frame country_data with variables at country level....

Report Europe: data frame country_data with variables at country level. Substitute multiple independent variables with non-explicit names.
parent 0c59c535
Pipeline #14044 passed with stage
in 7 minutes and 29 seconds
......@@ -70,12 +70,20 @@ european_countries <- sort(c(eu28, efta, candi, noneu)) %>%
```
```{r mydata, include = F}
## Deaths by country and date of European countries
## with population and lockdown date added
Dfr <- deaths_by_ctry_dt %>%
filter(name %in% european_countries) %>%
```{r country-data}
## Variables relative to European countries:
## group, name, lockdown date, population, cumulated deaths,
## and death rate.
country_data <-
map_dfr(
list("eu28", "efta", "candi", "noneu"),
~tibble(
group = .,
name = get(.)
)
) %>%
filter(name != "Liechtenstein") %>%
left_join(
lockdown_dates,
by = "name"
......@@ -84,49 +92,63 @@ Dfr <- deaths_by_ctry_dt %>%
pop_countries_2019,
by = "name"
) %>%
mutate(
name = factor(name)
mutate_if(is.character, as.factor) %>%
left_join(
deaths_by_ctry_dt %>%
group_by(name) %>%
summarise(
cum_deaths = sum(deaths),
.groups = "drop"
),
by = "name"
) %>%
select(name, date, deaths, pop, lockdown)
## compute overall stats
mutate(
## Cumulated number of deaths per 100 K hab.
## Note that pop is expressed in thousands.
death_rate = cum_deaths / pop * 100
) %>%
## Arrange rows by descending order of death rate
arrange(desc(death_rate))
## cumulative death rate sorted from the highest to the lowest
```
cdr <- 100 * tapply(Dfr$deaths, factor(Dfr$name), sum) /
tapply(Dfr$pop, factor(Dfr$name), unique)
## order name levels according to decreasing cumulative death rate
Dfr$name <- factor(as.character(Dfr$name))
o <- order(cdr, decreasing = T)
```{r mydata, include = F}
## country names ordered by decreasing cumulative death rate
onam <- names(cdr)[o]
ocdr <- cdr[o]
## Deaths by country and date of European countries
## with population and lockdown date added
Dfr <- deaths_by_ctry_dt %>%
inner_join(
country_data,
by = "name"
) %>%
mutate(
# Order name levels in decreasing order of death rate
name = fct_reorder(name, death_rate, .desc = TRUE)
) %>%
select(name, date, deaths, pop, lockdown)
## number of deaths by country
nd <- tapply(Dfr$deaths, Dfr$name, sum, na.rm = T)
ond <- nd[o]
agg <- aggregate(deaths ~ name, data = Dfr, FUN = sum)
nam100 <- as.character(agg$name[agg$deaths < 100])
## Names of countries with less than 100 cumulated deaths
nam100 <- country_data %>%
filter(cum_deaths < 100) %>%
pull(name)
## character string with the number of deaths by country
sond <- format(ond, big.mark = ",", trim = T)
sond <- format(country_data$cum_deaths, big.mark = ",", trim = T)
## character string with the cumulative death rate by country
socdr <- sprintf(ocdr, fmt = "%##.1f", digits = 0)
socdr <- sprintf(country_data$death_rate, fmt = "%##.1f", digits = 0)
## overall number of deaths
ttnd <- sum(nd)
ttnd <- sum(country_data$cum_deaths)
## character string for overall number of deaths
sttnd <- format(ttnd, big.mark = ",")
## overall population size
cond <- pop_countries_2019$name %in% european_countries
ttpop <- 1000 * sum(pop_countries_2019$pop[cond])
## overall population size (in thousands)
ttpop <- sum(country_data$pop)
## character string for overall pop size
sttpop <- format(ttpop / 1e6, digits = 4, nsmall = 1, big.mark = ",")
......@@ -292,7 +314,7 @@ Liechtenstein was excluded from the study because of its small population size.
## Cumulative death rate
In the `r length(european_countries)` countries accounted for^[List of countries (number of deaths in braces): `r collapse(nam = onam, cnt = sond)`.],
In the `r length(european_countries)` countries accounted for^[List of countries (number of deaths in braces): `r collapse(nam = levels(Dfr$name), cnt = sond)`.],
the total number of deaths is now `r sttnd` for an overall population size of
`r sttpop` millions, representing a cumulative death rate of
`r sttcdr` deaths $10^{-5}$ inhabitants (inh.).
......@@ -315,13 +337,14 @@ red <- c("Belgium", "France", "Ireland", "Italy", "Luxembourg",
lbl <- paste0("(", sond, ")")
cex <- 0.85
dotplot( ~ rev(ocdr),
xlim = extendrange(range(ocdr), f = c(.05, .2)),
scales = list(cex = cex, tck = .5,
y = list(labels = rev(onam),
col = ifelse(rev(onam) %in% red, 2, 1))),
dotplot( ~ rev(setNames(death_rate, name)),
data = country_data,
xlim = extendrange(range(country_data$death_rate), f = c(.05, .2)),
scales = list(cex = cex, tck = .5,
y = list(labels = rev(country_data$name),
col = ifelse(rev(country_data$name) %in% red, 2, 1))),
cex = cex,
col = ifelse(rev(onam) %in% red, 2, 1),
col = ifelse(rev(country_data$name) %in% red, 2, 1),
aspect = 1.5,
xlab = list(expression(Deaths %*% 10^{-5}~inhabitants),
cex = cex)) +
......@@ -334,9 +357,10 @@ dotplot( ~ rev(ocdr),
gp = gpar(cex = cex,
col = ifelse(rev(onam) %in% red, 2, 1))
),
data = list(lbl = lbl, cex = cex, red = red, onam = onam)
data = list(lbl = lbl, cex = cex, red = red, onam = country_data$name)
)
```
### Spatial distribution{-}
......@@ -360,9 +384,9 @@ roi2 <- spTransform(roi_europe, merc)
## cumulative mortality
covmap2$cumdr <- NA
for(i in seq_along(onam)){
xname <- onam[i]
xcumdr <- ocdr[i]
for(i in seq.int(nlevels(Dfr$name))){
xname <- levels(Dfr$name)[i]
xcumdr <- country_data$death_rate[i]
covmap2@data$cumdr[which(covmap2@data$CNTRY_NAME == xname)] <- xcumdr
}
......@@ -377,28 +401,28 @@ cap <- paste0(
". The countries are listed in the legend and numbered on ",
"the map (number of deaths in braces).\n")
num <- sprintf(seq(length(onam)), fmt = "%02d")
num <- sprintf(seq.int(nlevels(Dfr$name)), fmt = "%02d")
## colors for the map
cols <- colorRampPalette(rev(brewer.pal(n=9, "RdYlGn"))[-(1:2)])
labs <- paste0(num, " ", onam, " (", sond, ")")
labs <- paste0(num, " ", levels(Dfr$name), " (", sond, ")")
nch <- nchar(labs)
slabs <- labs[which(nch == max(nch))]
ocovmap2 <- covmap2[covmap2$CNTRY_NAME %in% onam, ]
ocovmap2 <- covmap2[covmap2$CNTRY_NAME %in% levels(Dfr$name), ]
oXY <- data.frame(coordinates(ocovmap2))
names(oXY) <- c("x", "y")
oXY$name <- as.character(ocovmap2$CNTRY_NAME)
num <- unlist(sapply(oXY$name, function(x) which(onam == x)))
num <- unlist(sapply(oXY$name, function(x) which(levels(Dfr$name) == x)))
## parameters of the map
cex <- .75
e <- raster::extent(roi2)
H <- stringHeight(slabs)
L <- stringWidth(slabs)
n <- length(onam)
n <- nlevels(Dfr$name)
kex <- 1.1
spplot(ocovmap2, zcol = "cumdr", col.regions = cols(100),
......@@ -435,13 +459,13 @@ spplot(ocovmap2, zcol = "cumdr", col.regions = cols(100),
* **The most affected countries are located in south-western
Europe^[Belgium, France, Ireland, Italy, Luxembourg, Portugal, Spain, The
Netherlands, The United Kingdom.], at the exception of Sweden** (ranking
`r which(onam == "Sweden")` on fig. \@ref(fig:covmap)), where
`r which(levels(Dfr$name) == "Sweden")` on fig. \@ref(fig:covmap)), where
strict lockdown measures were not adopted. Indeed, the cumulative
death rate (fig. \@ref(fig:cumul)) is contrasted between these
south-Western European countries and the others. For instance, low
death rates were reported in Germany
(`r socdr[which(onam == "Germany")]` $10^{-5}$ inh.), or even lower
in Poland (`r socdr[which(onam == "Poland")]` $10^{-5}$ inh.).
(`r socdr[which(levels(Dfr$name) == "Germany")]` $10^{-5}$ inh.), or even lower
in Poland (`r socdr[which(levels(Dfr$name) == "Poland")]` $10^{-5}$ inh.).
* Low death rates are reported in Finland, the Baltic countries,
Central Europe, and south-eastern Europe. These countries are
......@@ -484,7 +508,7 @@ cap <- paste0("Daily death rate (count / 100,000 inh. / d) ",
## add population size
## we discard countries with < 100 deaths
onam2 <- onam[!(onam %in% c("Turkey", nam100))]
onam2 <- levels(Dfr$name)[!(levels(Dfr$name) %in% c("Turkey", nam100))]
## the condition on deaths >= 0 is needed because Spain has declared a
## negative count in early June
......
......@@ -83,6 +83,7 @@ plan <- drake_plan(
ungroup() %>%
mutate(date = as.Date(date)),
## Population counts in thousands of people
pop_countries_2019 = pop_countries(world_population, year = 2019),
## today (ANSI)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment