Este es un análisis geoespacial de los casos de COVID-19 en México, utilizando datos de contagios, defunciones y población por entidad federativa. Se creará un gráfico que muestra la tasa de contagio y la letalidad en cada estado, junto con puntos que representan la letalidad.
# Cargar librerÃas
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.1
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Linking to GEOS 3.11.1, GDAL 3.6.2, PROJ 9.1.1; sf_use_s2() is TRUE
library(ggspatial)
## Warning: package 'ggspatial' was built under R version 4.3.1
library(readxl)
# Cargar base de datos
cv <- read.csv("C:/Users/MCARRILLO/Downloads/datos_abiertos_covid19/COVID19MEXICO.csv")
# Filtrar datos de personas fallecidas
cv1 <- cv %>%
filter(FECHA_DEF != "9999-99-99")
# Crear base de defunciones por entidad
def <- cv1 %>%
filter(RESULTADO_LAB == 1) %>%
group_by(ENTIDAD_RES) %>%
summarise(defunciones = n())
# Crear base de contagios por entidad
cont <- cv %>%
filter(RESULTADO_LAB == 1) %>%
group_by(ENTIDAD_RES) %>%
summarise(contagios = n())
# Cargar datos de población
dat1 <- read_excel("C:/Users/MCARRILLO/Downloads/base_mun_1.xlsx")
dat2 <- read_excel("C:/Users/MCARRILLO/Downloads/base_mun_2.xlsx")
# Filtrar datos para el año 2023
da1 <- dat1[dat1$AÑO %in% 2023,]
da2 <- dat2[dat2$AÑO %in% 2023,]
# Combinar bases de datos de población
datos <- rbind(da1, da2)
# Obtener población por entidad
pob <- datos %>%
group_by(CLAVE_ENT) %>%
summarise(poblacion = sum(POB))
# Renombrar variable
names(pob) <- c("ENTIDAD_RES", "poblacion")
# Cargar datos vectoriales de los estados
mun <- st_read(dsn = "C:/Users/MCARRILLO/Downloads/Mexico_Estados/México_Estados.shp")
## Reading layer `México_Estados' from data source
## `C:\Users\MCARRILLO\Downloads\Mexico_Estados\México_Estados.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 32 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -117.1224 ymin: 14.55055 xmax: -86.735 ymax: 32.72081
## Geodetic CRS: WGS 84
# Separar variable CÓDIGO
mun <- mun %>%
mutate(CODIGO = as.character(CODIGO)) %>%
separate(CODIGO, into = c("borrar", "ENTIDAD_RES"), sep = 2)
# Modificar observaciones para quitarles el cero
# (Puedes considerar otras formas más eficientes para esto)
# Convertir a numérico la variable
mun$ENTIDAD_RES <- as.numeric(mun$ENTIDAD_RES)
# Juntar base de población, contagios y defunciones
base1 <- pob %>%
left_join(cont) %>%
left_join(def)
## Joining with `by = join_by(ENTIDAD_RES)`
## Joining with `by = join_by(ENTIDAD_RES)`
# Calcular variables de interés
base2 <- base1 %>%
mutate(letalidad = defunciones / contagios * 100,
tasa.cont = contagios / poblacion * 100000)
# Juntar bases de datos espaciales
dts <- mun %>%
left_join(base2)
## Joining with `by = join_by(ENTIDAD_RES)`
# Cargar puntos de los estados
puntos <- st_read(dsn = "C:/Users/MCARRILLO/Downloads/Estados_puntos/estados.puntos.shp")
## Reading layer `estados.puntos' from data source
## `C:\Users\MCARRILLO\Downloads\Estados_puntos\estados.puntos.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 32 features and 3 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -115.0975 ymin: 16.48524 xmax: -88.11167 ymax: 30.55092
## Geodetic CRS: WGS 84
puntos <- st_transform(puntos, crs = 4326)
# Renombrar columna CVEGEO por ENTIDAD_RES
puntos <- rename(puntos, ENTIDAD_RES = CVEGEO)
# Transformar a numérico
puntos$ENTIDAD_RES <- as.integer(puntos$ENTIDAD_RES)
# Juntar bases de datos espaciales con puntos
pts <- puntos %>%
left_join(base2)
## Joining with `by = join_by(ENTIDAD_RES)`
ggplot() +
geom_sf(data = dts, aes(fill = tasa.cont)) +
scale_fill_distiller(palette = "Reds", direction = 1) +
geom_sf(data = pts, aes(size = letalidad), alpha = 0.3, col = "blue") +
labs(title = "Tasa de contagio y letalidad por entidad",
subtitle = "Análisis geoespacial de COVID-19 en México",
caption = "Fuente: www.gob.mx/Datos abiertos",
fill = "Tasa de contagio (por 100,000 habitantes)",
size = "Letalidad (%)",
x = NULL, y = NULL) +
theme_bw() +
theme(panel.background = element_rect(fill = "white"),
plot.background = element_rect(fill = "white"),
panel.grid = element_blank(),
plot.title = element_text(hjust = 0.5, size = 15),
legend.background = element_rect(fill = NA)) +
annotation_north_arrow(location = "tr",
style = north_arrow_nautical(),
height = unit(2.5, "cm"),
width = unit(2.5, "cm")) +
annotation_scale()
## Scale on map varies by more than 10%, scale bar may be inaccurate