Skip to content

Instantly share code, notes, and snippets.

@sbfnk
Created October 24, 2022 10:11
Show Gist options
  • Save sbfnk/58f6dfbab084566528e0898f1a85fbcb to your computer and use it in GitHub Desktop.
Save sbfnk/58f6dfbab084566528e0898f1a85fbcb to your computer and use it in GitHub Desktop.
download Spanish hospitalisation data
library("readr")
library("dplyr")
library("janitor")
library("lubridate")
library("ggplot2")
## SOURCE 1: download data
df <- read_delim("https://www.sanidad.gob.es/profesionales/saludPublica/ccayes/alertasActual/nCov/documentos/Datos_Capacidad_Asistencial_Historico_19102022.csv", delim = ";", show_col_types = FALSE)
df_national <- df %>%
clean_names() %>%
filter(grepl("^[0-9]", fecha)) %>%
mutate(fecha = dmy(fecha)) %>%
filter(grepl("Hospital.*convencional", unidad)) %>%
group_by(date = fecha) %>%
summarise(hospitalisations = sum(ingresos_covid19)) %>%
arrange(date) %>%
mutate(source = "sanidad.gob.es")
## SOURCE 2: download data
df2 <- read_csv("https://cnecovid.isciii.es/covid19/resources/hosp_uci_def_sexo_edad_provres_todas_edades.csv", show_col_types = FALSE)
df2_national <- df2 %>%
group_by(date = fecha) %>%
summarise(hospitalisations = sum(num_hosp), .groups = "drop") %>%
mutate(source = "cnecovid.isciii.es")
df_all <- df_national %>%
bind_rows(df2_national) %>%
filter(date >= "2022-01-01")
p <- ggplot(df_all, aes(x = date, y = hospitalisations, colour = source)) +
geom_line() +
scale_colour_brewer(palette = "Set1") +
theme_bw()
ggsave("hospitalisations_spain.pdf", p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment