1 Loading and Exploring Data

1.1 Packages

library(readr)
library(dplyr)
library(ggplot2)
library(ggrepel)
library(ggpubr)
library(tidyr)
library(lubridate)
library(gsheet)
  • readr: reading files
  • dplyr: to manipulate the tables
  • ggplot2: to visualize the data
  • ggrepel: to add labels in plots
  • ggpubr: to show arrange plots in one image
  • tidyr: to manipulate the data
  • lubridate: to work with dates in R
  • gsheet: to read google spreadsheet
source("../Scripts/R0-function.R")
source("../Scripts/custom-ggplot2-theme.R")
source("../Scripts/Moving-average-function.R")

1.2 Data

# Local
# covid <- read_csv("../Data/VariosPaises_Covid19Actualizado.csv", 
#                   na = "#VALUE!")

# Online
covid <- gsheet2tbl("https://docs.google.com/spreadsheets/d/1YnsFPlJHJ9TQB5lZnqoEY6HZKkjRbns2kBr9I4HwNG4/edit?usp=sharing")

# country_pops <- tibble(
#   COUNTRY = unique(covid$COUNTRY),
#   POP = c(11673021, 212559417, 19116201, 50882891, 17643054,
#           6486205, 17915568, 128932753, 17134872, 7132538,
#           32971854, 2860853, 46754778, 3473730)
# )
country_pops <- gsheet2tbl("https://docs.google.com/spreadsheets/d/1YnsFPlJHJ9TQB5lZnqoEY6HZKkjRbns2kBr9I4HwNG4/edit#gid=1550796837", 
                           sheetid = 2)

covid_tbl <- covid %>% 
  left_join(country_pops) %>%
  mutate(CONTAGIADOS_MILL = CONTAGIADOS/POP * 1000000,
         FALLECIDOS_MILL = FALLECIDOS/POP * 1000000,
         COUNTRY = as.factor(COUNTRY),
         FECHA = dmy(FECHA),
         IP = CONTAGIADOS + FALLECIDOS - RECUPERADOS,
         R0_14 = R0(CONTAGIADOS, 14)) %>%
  group_by(COUNTRY) %>% 
  mutate(CONTAGIADOS_MA = mov_avg(CONTAGIADOS, 7),
         CONTAGIADOS_MILL_MA = mov_avg(CONTAGIADOS_MILL, 7),
         FALLECIDOS_MA = mov_avg(FALLECIDOS, 7),
         FALLECIDOS_MILL_MA = mov_avg(FALLECIDOS_MILL, 7),
         RECUPERADOS_MA = mov_avg(RECUPERADOS, 7),
         DIA = 1:n()) %>% 
  ungroup()
covid_tbl

The dataset consists of 2565 observations of daily cases of COVID-19 of Spain, Netherlands and some latin american countries (14 in total), from March 2nd to Aug 31th of 2020.

Infected

1.2.0.1 All-together

infected_p1 <- covid_tbl %>% 
  filter(FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = CONTAGIADOS_MA, col = COUNTRY)) +
  geom_point() +
  geom_line() +
  geom_label_repel(data = . %>% filter(FECHA == date("2020-08-28"),
                                       COUNTRY %in% c("BRAZIL", "SPAIN",
                                                      "PERU", "MEXICO",
                                                      "COLOMBIA")), 
                   aes(label = COUNTRY), family = "Poppins", 
                   xlim = date("2020-09-02"), ylim = c(2000, 40000),
                   box.padding = .05, force = .1) +
  geom_label(aes(label = "*"), family = "Poppins", x = date("2020-09-16"),
             y = 1200, col = "black") +
  scale_x_date(date_labels = "%b",
               limits = date(c("2020-03-01", "2020-10-20")),
               breaks = seq.Date(from = date("2020-03-01"),
                                 to = date("2020-09-01"),
                                 by = "month")) +
  custom_theme +
  labs(y = "CONTAGIADOS", caption = "* Países con número de contagiados en la última semana menor a 5000:\nChile,Bolivia, Ecuador, Guatemala, Paraguay, Puerto Rico, El Salvador,\nPaíses Bajos yUruguay")

infected_p2 <- covid_tbl %>% 
  filter(FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = CONTAGIADOS_MILL_MA, col = COUNTRY)) +
  geom_point() +
  geom_line() +
  # geom_smooth(se = FALSE) +
  geom_label_repel(data = . %>% filter(FECHA == date("2020-08-28"),
                                       COUNTRY %in% c("BRAZIL", "SPAIN",
                                                      "PERU", "MEXICO",
                                                      "COLOMBIA")), 
                   aes(label = COUNTRY), family = "Poppins", 
                   xlim = date("2020-09-02"), ylim = c(80, 4000)) +
  geom_label(aes(label = "*"), family = "Poppins", x = date("2020-09-16"),
             y = 40, col = "black") +
  scale_x_date(date_labels = "%b",
               limits = date(c("2020-03-01", "2020-10-20")),
               breaks = seq.Date(from = date("2020-03-01"),
                                 to = date("2020-09-01"),
                                 by = "month")) +
  custom_theme +
  labs(y = "CONTAGIADOS POR MILLON", caption = "* Países con número de contagiados por millón en la última semana\nmenor a 200:Chile, Bolivia, Ecuador, Guatemala, Paraguay, España,\nPuerto Rico, El Salvador, Países Bajos y Uruguay")

ggarrange(infected_p1, infected_p2)

1.2.0.2 Group 1

covid_tbl %>% 
  filter(COUNTRY %in% c("URUGUAY"),
         FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y  = CONTAGIADOS)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-03-01"), 
                                 to = as.Date("2020-09-01"), 
                                 by = "month")) +
  facet_wrap(~COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.3 Group 2

covid_tbl %>% 
  filter(COUNTRY %in% c("EL SALVADOR", "PUERTO RICO", "PARAGUAY"),
         FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = CONTAGIADOS)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-03-01"), 
                                 to = as.Date("2020-09-01"), 
                                 by = "month")) +
  facet_wrap(~COUNTRY, nrow = 2) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.4 Group 3

covid_tbl %>% 
  filter(COUNTRY %in% c("GUATEMALA", "NETHERLAND", "BOLIVIA", "ECUADOR"),
         FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = CONTAGIADOS)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-03-01"), 
                                 to = as.Date("2020-09-01"), 
                                 by = "month")) +
  facet_wrap(~COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.5 Group 4

covid_tbl %>% 
  filter(COUNTRY %in% c("CHILE", "SPAIN", "MEXICO", "COLOMBIA", "PERU"),
         FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = CONTAGIADOS)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-03-01"), 
                                 to = as.Date("2020-09-01"), 
                                 by = "month")) +
  facet_wrap(~COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.6 Group 5

covid_tbl %>%
  filter(COUNTRY %in% c("BRAZIL"),
         FECHA <= date("2020-08-28")) %>%
  ggplot(aes(x = FECHA, y = CONTAGIADOS)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(
                 from = date("2020-03-01"),
                 to = date("2020-09-01"),
                 by = "month"
               )) +
  facet_wrap( ~ COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

Deaths

1.2.0.7 All-together

deaths_p1 <- covid_tbl %>% 
  filter(FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = FALLECIDOS_MA, col = COUNTRY)) +
  geom_point() +
  geom_line() +
  # geom_smooth(se = FALSE) +
  geom_label_repel(
    data = . %>% filter(
      FECHA == date("2020-08-28"),
      COUNTRY %in% c("BRAZIL", "PERU", "MEXICO", "COLOMBIA")
    ),
    aes(label = COUNTRY),
    family = "Poppins",
    xlim = date("2020-09-02"),
    ylim = c(100, 950)
  ) +
  geom_label(
    aes(label = "*"),
    family = "Poppins",
    x = date("2020-09-15"),
    y = 40,
    col = "black"
  ) +
  scale_x_date(date_labels = "%b",
               limits = date(c("2020-03-01", "2020-10-20")),
               breaks = seq.Date(from = date("2020-03-01"),
                                 to = date("2020-09-01"),
                                 by = "month")) +
  custom_theme +
  labs(y = "MUERTES", caption = "* Países con número de muertes menor a 100 en la ultima semana: Chile,\nBolivia, Ecuador, Guatemala, Paraguay, España, Puerto Rico, El Salvador,\nPaíses Bajos y Uruguay")


deaths_p2 <- covid_tbl %>% 
  filter(FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = FALLECIDOS_MILL_MA, col = COUNTRY)) +
  geom_point() +
  geom_line() +
  # geom_smooth(se = FALSE) +
  geom_label_repel(
    data = . %>% filter(
      FECHA == date("2020-08-28"),
      COUNTRY %in% c("BRAZIL", "PERU", "MEXICO", "COLOMBIA")
    ),
    aes(label = COUNTRY),
    family = "Poppins",
    xlim = date("2020-09-02"),
    ylim = c(10, 1000)
  ) +
  geom_label(
    aes(label = "*"),
    family = "Poppins",
    x = date("2020-09-15"),
    y = 4,
    col = "black"
  ) +
  scale_x_date(date_labels = "%b",
               limits = date(c("2020-03-01", "2020-10-20")),
               breaks = seq.Date(from = date("2020-03-01"),
                                 to = date("2020-09-01"),
                                 by = "month")) +
  custom_theme +
  labs(y = "MUERTES", caption = "* Países con número de muertes por millón menor a 10 en la ultima semana:\nChile, Bolivia, Ecuador, Guatemala, Paraguay, España, Puerto Rico,\nEl Salvador, Países Bajos y Uruguay")

ggpubr::ggarrange(deaths_p1, deaths_p2)

1.2.0.8 Group 1

ggplot(data = covid_tbl %>% 
         filter(COUNTRY %in% c("URUGUAY"),
                FECHA <= date("2020-08-28")), 
       aes(x = FECHA, y = FALLECIDOS)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-04-01"), 
                                 to = as.Date("2020-08-01"), 
                                 by = "month")) +
  scale_y_continuous(labels = scales::comma) +
  facet_wrap(~COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.9 Group 2

ggplot(data = covid_tbl %>% 
         filter(COUNTRY %in% c("EL SALVADOR", "PUERTO RICO", "PARAGUAY"),
                FECHA <= date("2020-08-28")), 
       aes(x = FECHA, y = FALLECIDOS)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-04-01"), 
                                 to = as.Date("2020-08-01"), 
                                 by = "month")) +
  scale_y_continuous(labels = scales::comma) +
  facet_wrap(~COUNTRY, nrow = 2) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.10 Group 3

ggplot(data = covid_tbl %>% 
         filter(COUNTRY %in% c("GUATEMALA", "NETHERLAND", "BOLIVIA",
                               "ECUADOR"),
                FECHA <= date("2020-08-28")), 
       aes(x = FECHA, y = FALLECIDOS)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-04-01"), 
                                 to = as.Date("2020-08-01"), 
                                 by = "month")) +
  scale_y_continuous(labels = scales::comma) +
  facet_wrap(~COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.11 Group 4

ggplot(data = covid_tbl %>% 
         filter(COUNTRY %in% c("CHILE", "SPAIN", "MEXICO", "COLOMBIA",
                               "PERU"),
                FECHA <= date("2020-08-28")), 
       aes(x = FECHA, y = FALLECIDOS)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-04-01"), 
                                 to = as.Date("2020-08-01"), 
                                 by = "month")) +
  scale_y_continuous(labels = scales::comma) +
  facet_wrap(~COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.12 Group 5

ggplot(data = covid_tbl %>% 
         filter(COUNTRY %in% c("BRAZIL"),
                FECHA <= date("2020-08-28")), 
       aes(x = FECHA, y = FALLECIDOS)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-03-01"), 
                                 to = as.Date("2020-09-01"), 
                                 by = "month")) +
  scale_y_continuous(labels = scales::comma) +
  facet_wrap(~COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

IP

1.2.0.13 Grupo 1

ggplot(data = covid_tbl %>% 
         filter(COUNTRY %in% c("URUGUAY"),
                FECHA <= date("2020-08-28")), 
       aes(x = FECHA, y = IP)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-04-01"), 
                                 to = as.Date("2020-08-01"), 
                                 by = "month")) +
  scale_y_continuous(labels = scales::comma) +
  facet_wrap(~COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.14 Grupo 2

ggplot(data = covid_tbl %>% 
         filter(COUNTRY %in% c("EL SALVADOR", "PUERTO RICO", "PARAGUAY"),
                FECHA <= date("2020-08-28")), 
       aes(x = FECHA, y = IP)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-04-01"), 
                                 to = as.Date("2020-08-01"), 
                                 by = "month")) +
  scale_y_continuous(labels = scales::comma) +
  facet_wrap(~COUNTRY, nrow = 2) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.15 Grupo 3

ggplot(data = covid_tbl %>% 
         filter(COUNTRY %in% c("GUATEMALA", "NETHERLAND", "BOLIVIA",
                               "ECUADOR"),
                FECHA <= date("2020-08-28")), 
       aes(x = FECHA, y = IP)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-04-01"), 
                                 to = as.Date("2020-08-01"), 
                                 by = "month")) +
  scale_y_continuous(labels = scales::comma, limits = c(-800, 1800)) +
  facet_wrap(~COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.16 Grupo 4

ggplot(data = covid_tbl %>% 
         filter(COUNTRY %in% c("CHILE", "SPAIN", "MEXICO", "COLOMBIA",
                               "PERU"),
                FECHA <= date("2020-08-28")), 
       aes(x = FECHA, y = IP)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-04-01"), 
                                 to = as.Date("2020-08-01"), 
                                 by = "month")) +
  scale_y_continuous(labels = scales::comma, limits = c(-2000, 8200)) +
  facet_wrap(~COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.17 Grupo 5

ggplot(data = covid_tbl %>% 
         filter(COUNTRY %in% c("BRAZIL"),
                FECHA <= date("2020-08-28")), 
       aes(x = FECHA, y = FALLECIDOS)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  scale_x_date(date_labels = "%b",
               breaks = seq.Date(from = as.Date("2020-03-01"), 
                                 to = as.Date("2020-09-01"), 
                                 by = "month")) +
  scale_y_continuous(labels = scales::comma) +
  facet_wrap(~COUNTRY) +
  custom_theme +
  theme(strip.text = element_text(size = 15))

R0

1.2.0.18 Grupo 1

covid_tbl %>% 
  filter(COUNTRY == "URUGUAY",
         FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = R0_14)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  facet_wrap(~COUNTRY) +
  scale_x_date(limit = c(as.Date("2020-03-25"), as.Date("2020-08-23"))) +
  scale_y_continuous(limit = c(0, 25)) +
  labs(y = "R0") +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.19 Grupo 2

covid_tbl %>% 
  filter(COUNTRY %in% c("EL SALVADOR", "PUERTO RICO", "PARAGUAY"),
         FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = R0_14)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  facet_wrap(~COUNTRY, nrow = 2) +
  scale_x_date(limit = c(as.Date("2020-03-25"), as.Date("2020-08-23"))) +
  scale_y_continuous(limit = c(0, 25)) +
  labs(y = "R0") +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.20 Grupo 3

covid_tbl %>% 
  filter(COUNTRY %in% c("GUATEMALA", "NETHERLAND", "BOLIVIA",
                        "ECUADOR"),
         FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = R0_14)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  facet_wrap(~COUNTRY) +
  scale_x_date(limit = c(as.Date("2020-03-25"), as.Date("2020-08-23"))) +
  scale_y_continuous(limit = c(0, 25)) +
  labs(y = "R0") +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.21 Grupo 4

covid_tbl %>% 
  filter(COUNTRY %in% c("CHILE", "SPAIN", "MEXICO", "COLOMBIA",
                        "PERU"),
         FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = R0_14)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  facet_wrap(~COUNTRY) +
  scale_x_date(limit = c(as.Date("2020-03-25"), as.Date("2020-08-23"))) +
  scale_y_continuous(limit = c(0, 25)) +
  labs(y = "R0") +
  custom_theme +
  theme(strip.text = element_text(size = 15))

1.2.0.22 Grupo 5

covid_tbl %>% 
  filter(COUNTRY == "BRAZIL",
         FECHA <= date("2020-08-28")) %>% 
  ggplot(aes(x = FECHA, y = R0_14)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  facet_wrap(~COUNTRY) +
  scale_x_date(limit = c(as.Date("2020-03-25"), as.Date("2020-08-23"))) +
  scale_y_continuous(limit = c(0, 25)) +
  labs(y = "R0") +
  custom_theme +
  theme(strip.text = element_text(size = 15))