R Markdown

R markdown

1 leer bde y reproyuectar a CRS4326 2 leer marginacion 3 join 4 leaflet 4.1 addproviders 4.2 addpoligons

1 leer bde y reproyuectar a CRS4326 usare sf para leer y tidyverse para manipular

## Reading layer `00ent' from data source 
##   `C:\MARIANARTALLER\01_input\MG_2020_Integrado-20230517T045939Z-001\MG_2020_Integrado\conjunto_de_datos\00ent.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 32 features and 3 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 911292 ymin: 319149.1 xmax: 4082997 ymax: 2349615
## Projected CRS: MEXICO_ITRF_2008_LCC

#hacer el leaflet

library(leaflet.extras)
## Warning: package 'leaflet.extras' was built under R version 4.1.3

·leer csv

fgj=read_csv("da_victimas_2023.csv") %>% 
  janitor::clean_names()
## Rows: 20166 Columns: 22
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (12): Mes_inicio, Delito, Categoria, Sexo, TipoPersona, CalidadJuridica...
## dbl   (6): idCarpeta, Año_inicio, Edad, Año_hecho, latitud, longitud
## date  (2): FechaInicio, FechaHecho
## time  (2): HoraHecho, HoraInicio
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
fgj
## # A tibble: 20,166 x 22
##    id_ca~1 ano_i~2 mes_i~3 fecha_in~4 delito categ~5 sexo   edad tipo_~6 calid~7
##      <dbl>   <dbl> <chr>   <date>     <chr>  <chr>   <chr> <dbl> <chr>   <chr>  
##  1 9361878    2023 Enero   2023-01-01 LESIO~ DELITO~ <NA>     NA MORAL   OFENDI~
##  2 9361879    2023 Enero   2023-01-01 ROBO ~ ROBO A~ Masc~    50 FISICA  VICTIM~
##  3 9361880    2023 Enero   2023-01-01 ROBO ~ ROBO D~ Masc~    34 FISICA  VICTIM~
##  4 9361881    2023 Enero   2023-01-01 LESIO~ DELITO~ Masc~    NA FISICA  LESION~
##  5 9361882    2023 Enero   2023-01-01 PERDI~ HECHO ~ Masc~    90 FISICA  CADAVER
##  6 9361883    2023 Enero   2023-01-01 ROBO ~ DELITO~ Masc~    NA FISICA  VICTIM~
##  7 9361885    2023 Enero   2023-01-01 DAÑO ~ DELITO~ Masc~    58 FISICA  VICTIM~
##  8 9361888    2023 Enero   2023-01-01 VIOLE~ DELITO~ Feme~    32 FISICA  VICTIM~
##  9 9361889    2023 Enero   2023-01-01 PERSO~ DELITO~ Masc~    14 FISICA  VICTIMA
## 10 9361892    2023 Enero   2023-01-01 DAÑO ~ DELITO~ Masc~    31 FISICA  VICTIM~
## # ... with 20,156 more rows, 12 more variables: competencia <chr>,
## #   ano_hecho <dbl>, mes_hecho <chr>, fecha_hecho <date>, hora_hecho <time>,
## #   hora_inicio <time>, alcaldia_hechos <chr>, municipio_hechos <chr>,
## #   colonia_datos <chr>, fgj_colonia_registro <chr>, latitud <dbl>,
## #   longitud <dbl>, and abbreviated variable names 1: id_carpeta,
## #   2: ano_inicio, 3: mes_inicio, 4: fecha_inicio, 5: categoria,
## #   6: tipo_persona, 7: calidad_juridica

#ver los datos

table(fgj$Categoria)
## Warning: Unknown or uninitialised column: `Categoria`.
## < table of extent 0 >

#hacer el heatmap

library(leaflet.extras)
leaflet(width = "100%") %>% 
  setView(lat =  19.421745837871452, lng = -99.12989770260374, zoom =10) %>% 
  addProviderTiles(providers$OpenStreetMap,group = "Capa1") %>% 
  addProviderTiles(providers$CartoDB,group = "capa2") %>% 
  addProviderTiles(providers$Esri.WorldStreetMap,group = "capa3") %>% 
  addHeatmap(lng = fgj$longitud,lat = fgj$latitud,radius = 10,group = "heatmap") %>% 
  addCircles(lng = fgj$longitud,lat = fgj$latitud,group = "puntos",
             color = "purple",
             fillColor = "white") %>% 
  addLayersControl(baseGroups = c("capa1","capa2","capa3"),
                   overlayGroups = c("heatmap","puntos"),
                   options = layersControlOptions(collapse=F))

#MAPA DE VIOLACIONES

base_v= fgj %>% 
  filter(categoria=="VIOLACIÓN")

leaflet(width = "100%") %>% 
  setView(lat =  19.421745837871452, lng = -99.12989770260374, zoom =10) %>% 
  addProviderTiles(providers$OpenStreetMap,group = "Capa1") %>% 
  addProviderTiles(providers$CartoDB,group = "capa2") %>% 
  addProviderTiles(providers$Esri.WorldStreetMap,group = "capa3") %>% 
  addHeatmap(lng = base_v$longitud,lat = base_v$latitud,radius = 10,group = "heatmap") %>% 
  addCircles(lng = base_v$longitud,lat = base_v$latitud,group = "puntos",
             color = "purple",
             fillColor = "white") %>% 
  addLayersControl(baseGroups = c("capa1","capa2","capa3"),
                   overlayGroups = c("heatmap","puntos"),
                   options = layersControlOptions(collapse=F))