library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.1 v dplyr 1.0.5
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.0.5
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'readr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## Warning: package 'forcats' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(sf)
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(readxl)
## Warning: package 'readxl' was built under R version 4.0.5
Se descarga de la Unidad de Seguridad Vial (https://aplicacionesunasev.presidencia.gub.uy/mapas/Visor) base de datos con la cantidad de siniestros de tránsito entre 01/01/2020 y 30/06/2020 en esquinas de Montevideo, incluye siniestros de todo tipo (leve, grave y fatal).
siniestros <- read_xlsx("siniestros.xlsx")
Para poder analizar esta información por barrios, necesitamos transformar esta base de datos en espacial y luego unirlo con la información del mapa barrios.
siniestros_geo <- siniestros %>%
st_as_sf(coords=c("x", "y"), crs=32721)
municipios <- st_read("sig_municipios.shp", stringsAsFactors = TRUE)
## Reading layer `sig_municipios' from data source
## `C:\Users\Usuario\Documents\Diplomatura\Ciencia_datos_ciudades\Datos_ciudades\Tareas\sig_municipios.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 8 features and 5 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: 551994.3 ymin: 6133494 xmax: 589199.4 ymax: 6159799
## Projected CRS: WGS 84 / UTM zone 21S
ggplot()+
geom_sf(data=municipios)+
geom_sf(data=siniestros_geo)
Queremos analizar la cantidad de siniestros por municpio para ello uniremos espacialmente la información de municipio y siniestros.
MuniConSin <- st_join(siniestros_geo, municipios)
siniestros_munis <- MuniConSin %>%
group_by(MUNICIPIO) %>%
summarise(siniestros=sum(cantidad))
ggplot(filter(siniestros_munis, MUNICIPIO !="NA"))+
geom_bar(aes(x=MUNICIPIO, weight=siniestros, fill=MUNICIPIO), show.legend = FALSE) +
labs (title = "Cantidad de siniestros por municipio",
subtitle = "Primer semestre 2020 - Montevideo",
x = "Municipio",
y = "Q de siniestros",
caption ="Fuente: Elaboración propia con base en UNASEV")+
coord_flip()+
theme (axis.title = element_text(colour = "gray35",size = 8),
axis.text.x = element_text(colour = "gray35",size = 6),
axis.text.y = element_text(colour = "gray35",size = 6))+
scale_fill_brewer(palette = "Set3")
Como se observa en el gráfico, los municipios C y B concentran la mayor cantidad de siniestros en el primer semestre de 2020. El municipio G, al oeste del Montevideo, es que presenta la menor cantidad.
siniestros_munis <- siniestros_munis %>%
st_set_geometry(NULL)
Mapa_Munis_Siniestros <- left_join(municipios, siniestros_munis, by="MUNICIPIO")
ggplot (Mapa_Munis_Siniestros)+
geom_sf(aes(fill=siniestros))+
geom_sf_text (aes(label=MUNICIPIO), size=2)+
labs(title="Cantidad de siniestros por municipio",
subtitle="Primer semestre 2020 - Montevideo",
caption = "Elaboración propia con base en UNASEV",
fill="Cantidad de siniestros") +
scale_fill_distiller(palette = "Spectral")+
theme_void()
En el mapa puede verse más claramente que los municipios con más alta cantidad de siniestros, incluyen al centro de la ciudad y que están contiguos. Los de menos siniestros difieren mucho en superficie pero se caracterizan por ser más bien residenciales.