--- title: "Get Semáforo Epidemiológico Data from Mexico COVID-19 Response" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Get Semáforo Epidemiológico Data from Mexico COVID-19 Response} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` ```{r setup} library(semaforos) ``` ```{r} sl <- semaforos_get("long") head(sl) ``` ## Map of semaforos over time ```{r, dpi = 96, warning = FALSE, message = FALSE, out.width="30%"} ```{r, "maps-ggplot", dpi = 96, warning = FALSE, message = FALSE, out.width="30%"} library(ggplot2) library(sf) mxstates <- readRDS(system.file("extdata", "sf_mx_estados.rds", package = "semaforos")) df <- merge(mxstates, sl, by.x = "name", by.y = "estado", all = TRUE) df$yearweek <- paste(df$year, df$week, sep = "-") for (w in sort(unique(df$yearweek))) { print( ggplot(df[df$yearweek == w, ]) + geom_sf(aes(fill = color), size = 0.2) + coord_sf() + scale_fill_manual(values = c("verde" = "#02A247", "amarillo" = "#FFC60F", "naranja" = "#FF7000", "rojo" = "#FF0000"), limits = c("verde", "amarillo", "naranja", "rojo")) + theme_minimal() + theme(title = element_text(color = "grey30"), legend.text = element_text(color = "grey30")) + labs(title = paste("Semáforos for week", w)) ) } ``` ## Duration of each semáforo ```{r} library(dplyr) library(ggplot2) duration_sf <- sl %>% group_by(estado) %>% summarize(semaforo = rle(color)$values, duration = rle(color)$lengths) head(duration_sf) ggplot(duration_sf, aes(x = duration, fill = semaforo)) + geom_histogram() + scale_fill_manual(values = c("verde" = "#02A247", "amarillo" = "#FFC60F", "naranja" = "#FF7000", "rojo" = "#FF0000"), limits = c("verde", "amarillo", "naranja", "rojo")) + labs(x = "duration (weeks)", title = "Distribution of the durations of semáforos in Mexico") + theme_minimal() ggplot(duration_sf, aes(x = semaforo, y = duration, fill = semaforo, color = semaforo)) + geom_violin(alpha = 0.5) + geom_jitter() + scale_fill_manual(values = c("verde" = "#02A247", "amarillo" = "#FFC60F", "naranja" = "#FF7000", "rojo" = "#FF0000"), limits = c("verde", "amarillo", "naranja", "rojo")) + scale_colour_manual(values = c("verde" = "#02A247", "amarillo" = "#FFC60F", "naranja" = "#FF7000", "rojo" = "#FF0000"), limits = c("verde", "amarillo", "naranja", "rojo")) + labs(y = "duration (weeks)", title = "Duration of semáforos in Mexico depending on the colour") + theme_minimal() ``` ## Time spent in each colour ```{r} library(dplyr) library(forcats) sl %>% count(color, estado) %>% ggplot(aes(x = n, y = estado, fill = fct_relevel(color, "verde"))) + geom_histogram(stat = "identity") + scale_fill_manual(values = c("verde" = "#02A247", "amarillo" = "#FFC60F", "naranja" = "#FF7000", "rojo" = "#FF0000"), limits = c("verde", "amarillo", "naranja", "rojo"), name = "color") + theme_minimal() + labs(x = "total number of weeks on this colour") ```