Elaboración de mapa de Producción de Mango a Nivel Nacional

En el presente documento, mostraremos el procedimiento para hacer un mapa con datos de producción de mango a nivel estatal para México, utilizando r-leaflet.

Librerias

library(sf)
library(dplyr)
library(leaflet)
library(RColorBrewer)

Abrimos los datos

EDO <- readxl::read_xlsx("SUPERFICIE_SEMBRADA_MANGO.xlsx")
MPIO <- readxl::read_xlsx("MUNICIPIOS_PEM.xlsx")

Inspeccionamos las variables

lapply(EDO[,1:3], class) 
## $`Clave de Estado`
## [1] "character"
## 
## $`Nombre de Estado`
## [1] "character"
## 
## $`Superficie (ha) sembrada`
## [1] "numeric"
lapply(MPIO[,1:4], class)
## $`Clave de Estado`
## [1] "character"
## 
## $`Nombre de Estado`
## [1] "character"
## 
## $`Clave de Municipio`
## [1] "character"
## 
## $`Nombre de Municipio`
## [1] "character"

Manipulamos los datos

MPIO$CODGEO <- paste0(MPIO$`Clave de Estado`, MPIO$`Clave de Municipio`) # Creamos un ID de Municipio

Cargamos los Shapes

EDO.shp  <- st_read("Sin Islas/sin_islas.shp", quiet = T) 
MPIO.shp <- st_read("division_politica_municipal/muni_2012gw/Muni_2012gw.shp", quiet = T)
head(EDO.shp)
## Simple feature collection with 6 features and 7 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -117.1264 ymin: 17.81226 xmax: -89.1135 ymax: 32.71877
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##         AREA PERIMETER COV_ COV_ID              ENTIDAD        CAPITAL
## 1  0.4919529  3.784629  257    270       AGUASCALIENTES Aguascalientes
## 2  6.7179224 18.921158    2      1      BAJA CALIFORNIA       Mexicali
## 3  6.3738283 27.474006   54     53  BAJA CALIFORNIA SUR         La Paz
## 4  4.7775949 16.883864  299    305             CAMPECHE       Campeche
## 5 13.8102834 23.463137   16     20 COAHUILA DE ZARAGOZA       Saltillo
## 6  0.4821734  4.288569  312    329               COLIMA         Colima
##   CVE_EDO                       geometry
## 1      01 POLYGON ((-102.0004 22.2332...
## 2      02 POLYGON ((-114.7575 32.7162...
## 3      03 POLYGON ((-112.7655 28.0007...
## 4      04 POLYGON ((-90.45313 20.7065...
## 5      05 POLYGON ((-102.3296 29.8639...
## 6      06 POLYGON ((-104.617 19.15396...
head(MPIO.shp)
## Simple feature collection with 6 features and 6 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -102.6935 ymin: 21.62227 xmax: -102.0645 ymax: 22.37468
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##   CVE_ENT CVE_MUN                   NOM_MUN OID_1 cov_ cov_id
## 1      01     005               Jesús María     1    1      2
## 2      01     011 San Francisco de los Romo     2    2      3
## 3      01     001            Aguascalientes     3    3      4
## 4      01     008        San José de Gracia     4    4      5
## 5      01     007           Rincón de Romos     5    5      6
## 6      01     009                  Tepezalá     6    6      7
##                         geometry
## 1 MULTIPOLYGON (((-102.3357 2...
## 2 MULTIPOLYGON (((-102.1527 2...
## 3 MULTIPOLYGON (((-102.1064 2...
## 4 MULTIPOLYGON (((-102.4561 2...
## 5 MULTIPOLYGON (((-102.2268 2...
## 6 MULTIPOLYGON (((-102.1799 2...
MPIO.shp$CODGEO <- paste0(MPIO.shp$CVE_ENT, MPIO.shp$CVE_MUN)

Hacemos el merge de las bases

EDO <- merge(EDO.shp, EDO,  by.y = "Clave de Estado", by.x = "CVE_EDO")
MPIO <- merge(MPIO.shp, MPIO , by.x = "CODGEO", by.y = "CODGEO")

Exploramos la variable de Superficie sembrada y hacemos cortes para la escala del mapa

table(EDO$Superficie..ha..sembrada)
## 
##    33    54    77    79    88   123   129   195   348   364   399   851 
##     1     1     1     1     1     1     1     1     1     1     1     1 
##  1561  2286  4168  7829 17670 19143 25341 26162 27288 29574 38656 
##     1     1     1     1     1     1     1     1     1     1     1
EDO <- EDO %>%
  mutate(Produccion = case_when(Superficie..ha..sembrada < 879 ~ "0 a 879 Has", 
                                Superficie..ha..sembrada >= 879 & Superficie..ha..sembrada < 7878 ~ "880 a 7,878 Has", 
                                Superficie..ha..sembrada >= 7878 & Superficie..ha..sembrada ~ "7879 a 31,523 Has" ))

Hacemos el mapa

colores <- c(rgb(254/255, 252/255, 176/255), rgb(252/255, 248/255, 40/255), rgb(242/255, 154/255, 39/255))

pal_EDO <- colorFactor(colores, domain = EDO$Produccion)
pal_MUN <- colorFactor("green", domain = MPIO$CODGEO)

Incluimos la informacion del enlarvamiento.

larva <- readxl::read_xlsx("MUNICIPIOS PEM LARVADOS.xlsx")
names(larva)[4] <- "Nombre.de.Municipio"
larva.shp <- merge(x = MPIO, y = larva, by = "Nombre.de.Municipio")
larva.shp$infeccion <- case_when(larva.shp$Lotes.larvados.en.empaque > 0 & 
                                   larva.shp$Lotes.larvados.en.empaque <= 20 ~ "1 a 20 lotes", 
                                 larva.shp$Lotes.larvados.en.empaque > 20 & 
                                   larva.shp$Lotes.larvados.en.empaque <= 40 ~ "21 a 40 lotes", 
                                 larva.shp$Lotes.larvados.en.empaque > 40 ~ "Más de 40 lotes", 
                                 is.na(larva.shp$Lotes.larvados.en.empaque) ~ "No hay/No se registró")

# Obtenemos la escala de colores

rgbizar <- function(a, b, c) {
  a = a/255 
  b = b/255 
  c = c/255 
  d <- rgb(a, b, c)
  return(d)}

colores2 <- c(rgbizar(70, 165, 64), rgbizar(246, 180, 39), rgbizar(230, 46, 37), 'gray')

pal_EDO <- colorFactor(colores, domain = EDO$Produccion)
pal_MUN <- colorFactor(colores2, domain = larva.shp$infeccion)

leaflet(EDO) %>%
  #setView(-103.1856, 20.73032, zoom = 5) %>%
  addProviderTiles("Esri.WorldTerrain") %>%
  addPolygons(data = EDO.shp, weight = 0.8, smoothFactor = 0.5, opacity = 0.2, color = "#444444") %>%
  addPolygons(weight = 0.8, 
                smoothFactor = 0.5,
                opacity = 0.2,
                fillOpacity = 0.4,
                fillColor = ~pal_EDO(Produccion),
                color = "#444444",
                popup = paste0("<b>","Estado: ", EDO$ENTIDAD, " </b>", "<br>",
                          "<b>", "Superficie sembrada por estado: ","</b>", EDO$Superficie..ha..sembrada, " Has"),
                #layerId = identificador_poligono,
                highlightOptions = highlightOptions(color = "white", weight = 2,
                                                    bringToFront = FALSE),
                label = EDO$ENTIDAD,
                labelOptions = labelOptions(direction = "auto")
                # ,
                # group = capa
                ) %>%
  addPolygons(data  = larva.shp, 
              weight = 1, 
              smoothFactor = 0.5,
              opacity = 1,
              fillOpacity = 1,
              fillColor = ~pal_MUN(infeccion),
              color = "#444444",
              popup = paste0("<b>Municipio : </b>", larva.shp$NOM_MUN, "<br>", 
                             "<b>", "Lotes infectados: </b>", larva.shp$infeccion),
              # layerId = identificador_poligono,
              highlightOptions = highlightOptions(color = "red", weight = 2,
                                                  bringToFront = TRUE)
              ,
              label = ~NOM_MUN
              #labelOptions = labelOptions(direction = "auto"),
              #group = capa
              ) %>%
  #addScaleBar(position = "bottomleft") %>%
  #addLegend(position = "topright", pal = pal_EDO, values = EDO$Produccion, title = "Superficie Sembrada (Ha)") %>%
  addLegend(position = "topright", pal = pal_MUN, values = larva.shp$infeccion, title = "Lotes larvados en empaque")