## Reading layer `00ent' from data source 
##   `C:\CONGRESO\01_input\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 leaflet providers geometria

#heatmap

library(leaflet.extras)

leer csv

fgj=read_csv("01_input/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
## 
## ℹ 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.
fgj
## # A tibble: 20,166 × 22
##    id_carpeta ano_inicio mes_inicio fecha_inicio delito    categoria sexo   edad
##         <dbl>      <dbl> <chr>      <date>       <chr>     <chr>     <chr> <dbl>
##  1    9361878       2023 Enero      2023-01-01   LESIONES… DELITO D… <NA>     NA
##  2    9361879       2023 Enero      2023-01-01   ROBO A T… ROBO A T… Masc…    50
##  3    9361880       2023 Enero      2023-01-01   ROBO DE … ROBO DE … Masc…    34
##  4    9361881       2023 Enero      2023-01-01   LESIONES… DELITO D… Masc…    NA
##  5    9361882       2023 Enero      2023-01-01   PERDIDA … HECHO NO… Masc…    90
##  6    9361883       2023 Enero      2023-01-01   ROBO DE … DELITO D… Masc…    NA
##  7    9361885       2023 Enero      2023-01-01   DAÑO EN … DELITO D… Masc…    58
##  8    9361888       2023 Enero      2023-01-01   VIOLENCI… DELITO D… Feme…    32
##  9    9361889       2023 Enero      2023-01-01   PERSONAS… DELITO D… Masc…    14
## 10    9361892       2023 Enero      2023-01-01   DAÑO EN … DELITO D… Masc…    31
## # ℹ 20,156 more rows
## # ℹ 14 more variables: tipo_persona <chr>, calidad_juridica <chr>,
## #   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>

#quiero ver los delitos de la base

table(fgj$categoria)
## 
##                                  DELITO DE BAJO IMPACTO 
##                                                   17488 
##                                      HECHO NO DELICTIVO 
##                                                     343 
##                                        HOMICIDIO DOLOSO 
##                                                      90 
##           LESIONES DOLOSAS POR DISPARO DE ARMA DE FUEGO 
##                                                      67 
##                    ROBO A CASA HABITACIÓN CON VIOLENCIA 
##                                                      15 
## ROBO A CUENTAHABIENTE SALIENDO DEL CAJERO CON VIOLENCIA 
##                                                      19 
##                            ROBO A NEGOCIO CON VIOLENCIA 
##                                                     171 
## ROBO A PASAJERO A BORDO DE MICROBUS CON Y SIN VIOLENCIA 
##                                                      87 
##           ROBO A PASAJERO A BORDO DE TAXI CON VIOLENCIA 
##                                                      13 
##   ROBO A PASAJERO A BORDO DEL METRO CON Y SIN VIOLENCIA 
##                                                     123 
##                   ROBO A REPARTIDOR CON Y SIN VIOLENCIA 
##                                                      86 
##    ROBO A TRANSEUNTE EN VÍA PÚBLICA CON Y SIN VIOLENCIA 
##                                                     909 
##                ROBO A TRANSPORTISTA CON Y SIN VIOLENCIA 
##                                                       6 
##                    ROBO DE VEHÍCULO CON Y SIN VIOLENCIA 
##                                                     519 
##                                               SECUESTRO 
##                                                       1 
##                                               VIOLACIÓN 
##                                                     229

Voy a hacer un heatmap

library(leaflet.extras)

leaflet(width = '100%') %>%
  setView(lng = -99.153912, 19.407897, zoom = 10) %>%
  addProviderTiles(providers$CartoDB,group = "capa 1") %>%
  addProviderTiles(providers$OpenStreetMap,group = "OSM") %>%
  addProviderTiles(providers$Esri.WorldImagery,group = "imagen") %>%
  addHeatmap(lng = fgj$longitud, lat = fgj$latitud,radius = 10, group = "heatmap") %>% 
  addCircles(lng = fgj$longitud, lat = fgj$latitud, group = "puntos",) %>% 
  addLayersControl(baseGroups = c("capa 1", "OSM", "imagen"), 
                   overlayGroups = c("heatmap", "puntos"),
                   options=layersControlOptions(collasped=F))

#mapa violacion

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

leaflet(width = '100%') %>%
  setView(lng = -99.153912, 19.407897, zoom = 10) %>%
  addProviderTiles(providers$CartoDB,group = "capa 1") %>%
  addProviderTiles(providers$OpenStreetMap,group = "OSM") %>%
  addProviderTiles(providers$Esri.WorldImagery,group = "imagen") %>%
  addHeatmap(lng =base_v$longitud, lat = base_v$latitud,radius = 10, group = "heatmap") %>% 
  addCircles(lng = base_v$longitud, lat = base_v$latitud, group = "puntos",) %>% 
  addLayersControl(baseGroups = c("capa 1", "OSM", "imagen"), 
                   overlayGroups = c("heatmap", "puntos"),
                   options=layersControlOptions(collasped=F))

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.