Instalar librerias
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'lubridate' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 4.0.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── 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
fgj = read_csv("https://archivo.datos.cdmx.gob.mx/FGJ/victimas/victimasFGJ_2024.csv")
## Rows: 146616 Columns: 22
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (13): mes_inicio, mes_hecho, delito, categoria_delito, sexo, tipo_perso...
## dbl (5): anio_inicio, anio_hecho, edad, latitud, longitud
## date (2): fecha_inicio, fecha_hecho
## time (2): hora_inicio, hora_hecho
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Variables de la base
fgj_corto = fgj %>%
select(delito,latitud,longitud)
Instalar paqueteria
options(pkgType = "binary")
install.packages("leaflet", repos = "https://cran.rstudio.com/", type = "binary")
## Installing package into 'C:/Users/isabel.garciaala/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
##
## There is a binary version available (and will be installed) but the
## source version is later:
## binary source
## leaflet 2.2.2 2.2.3
##
## package 'leaflet' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\isabel.garciaala\AppData\Local\Temp\RtmpUzTBI5\downloaded_packages
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.3
fgj_corto %>%
count(delito,sort = TRUE)
## # A tibble: 260 × 2
## delito n
## <chr> <int>
## 1 VIOLENCIA FAMILIAR 23841
## 2 FRAUDE 13795
## 3 AMENAZAS 12711
## 4 ROBO DE ACCESORIOS DE AUTO 5694
## 5 ROBO DE OBJETOS 5228
## 6 USURPACIÓN DE IDENTIDAD 4480
## 7 ROBO DE OBJETOS DEL INTERIOR DE UN VEHICULO 3763
## 8 ROBO A TRANSEUNTE EN VIA PUBLICA CON VIOLENCIA 3667
## 9 ABUSO SEXUAL 2798
## 10 DESPOJO 2285
## # ℹ 250 more rows
Queremos hacer un mapa específico
d1 = fgj_corto %>%
filter(delito=="VIOLENCIA FAMILIAR") %>%
filter(!is.na(latitud)) %>%
filter(!is.na(longitud))
Mi primer mapa con leaflet
leaflet() %>%
addProviderTiles(providers$CartoDB) %>%
addCircles(data = d1,
lng = ~longitud,
lat = ~latitud)
leaflet() %>%
addProviderTiles(providers$CartoDB) %>%
addCircleMarkers(
data = d1,
lng = d1$longitud,
lat = d1$latitud,
clusterOptions = TRUE
)