# Librerías
library(tidyverse)
library(sf)
# Leer archivo excel
sec <- read.csv("C:\\Users\\RACelis\\Downloads\\prueba.csv", encoding = "UTF-8", stringsAsFactors =  TRUE)
estados <- st_read("D:\\SIG\\SIG\\MGN_2020\\MGN_2020\\conjunto_de_datos\\00ent.shp")
## Reading layer `00ent' from data source `D:\SIG\SIG\MGN_2020\MGN_2020\conjunto_de_datos\00ent.shp' using driver `ESRI Shapefile'
## Simple feature collection with 32 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 911292 ymin: 319149.1 xmax: 4082997 ymax: 2349615
## Projected CRS: MEXICO_ITRF_2008_LCC
ggplot(estados)+
  geom_sf()

sec$CVEGEO <- as.character(sec$clave)
entidades <- left_join(estados, sec, by = "CVEGEO" )
ggplot(entidades) +
  geom_sf(aes(fill= Delitos))

entidades$cat1 <- case_when(
  entidades$Delitos <= 2 ~ "1",
  entidades$Delitos >= 3 & entidades$Delitos <= 4 ~ "2",
  entidades$Delitos >= 5 & entidades$Delitos <= 6 ~ "3",
  entidades$Delitos >= 7 ~ "4")
entidades$cat2 <- case_when(
  entidades$Victimas <= 2 ~ "1",
  entidades$Victimas >= 3 & entidades$Victimas <= 4 ~ "2",
  entidades$Victimas >= 5 & entidades$Victimas <= 6 ~ "3",
  entidades$Victimas >= 7 ~ "4")
delitos_palette <- c("1" = "green",
                     "2" = "yellow",
                     "3" = "orange",
                     "4" = "red",
                     "Unknown" = "grey60")
ggplot() +
  geom_sf(data = entidades, aes(fill= factor(cat1))) +
  scale_fill_manual(values = delitos_palette) #+

  #theme_void()