# Librerias
library(tidyverse)
## ── Attaching packages ────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 1.0.0 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(sf)
## Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library(leaflet)
library(stringr)
library(rebus)
##
## Attaching package: 'rebus'
## The following object is masked from 'package:stringr':
##
## regex
## The following object is masked from 'package:ggplot2':
##
## alpha
niveles <- function(x) levels(as.factor(x))
# DESCARGAR LOS ARCHIVOS.
# Descargar los archivos de los enlaces que estan en la funcion siguiente:
# https://www.inegi.org.mx/contenidos/masiva/denue/denue_00_46111_csv.zip
# https://www.inegi.org.mx/contenidos/masiva/denue/denue_00_46112-46311_csv.zip
# https://www.inegi.org.mx/contenidos/masiva/denue/denue_00_46321-46531_csv.zip
# https://www.inegi.org.mx/contenidos/masiva/denue/denue_00_46591-46911_csv.zip
#Intento de descarga automatica. No descomentar!
# curl::curl_download("https://www.inegi.org.mx/contenidos/masiva/denue/denue_00_46111_csv.zip", destfile = "denue_00_46111_csv") %>% zip::unzip()
# curl::curl_download("https://www.inegi.org.mx/contenidos/masiva/denue/denue_00_46112-46311_csv.zip", destfile = "denue_00_46112-46311_csv") %>% zip::unzip()
# curl::curl_download("https://www.inegi.org.mx/contenidos/masiva/denue/denue_00_46321-46531_csv.zip", destfile = "denue_00_46321-46531_csv") %>% zip::unzip()
# curl::curl_download("https://www.inegi.org.mx/contenidos/masiva/denue/denue_00_46591-46911_csv.zip", destfile = "denue_00_46591-46911_csv") %>% zip::unzip()
En este paso, leemos los datos y de una vez los manipulamos para quedarnos con las categorias del DENUE que requerimos.
# Leer datos
a <- st_read("https://raw.githubusercontent.com/JuveCampos/MexicoSinIslas/master/Sin_islas.geojson", quiet = TRUE) %>%
filter(ENTIDAD == "CIUDAD DE MÉXICO")
mpios <- st_read("https://raw.githubusercontent.com/JuveCampos/Shapes_Resiliencia_CDMX_CIDE/master/Zona%20Metropolitana/EdosZM.geojson") %>%
filter(CVE_ENT == "09")
## Reading layer `EdosZM' from data source `https://raw.githubusercontent.com/JuveCampos/Shapes_Resiliencia_CDMX_CIDE/master/Zona%20Metropolitana/EdosZM.geojson' using driver `GeoJSON'
## Simple feature collection with 76 features and 13 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -99.64989 ymin: 18.93534 xmax: -98.59687 ymax: 20.06826
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
files <- list.files()[c(1,2,3,4)]
b <- read.csv(paste0(files[4], "/conjunto_de_datos/", "denue_inegi_46591-46911_.csv"),
encoding = "latin1"
) %>%
st_as_sf(coords = c("longitud", "latitud"), crs = 4326) %>%
filter(entidad == "CIUDAD DE MÉXICO") %>%
filter(nombre_act == "Comercio al por menor de mobiliario, equipo y accesorios de cómputo" |
nombre_act == "Comercio al por menor de electrodomésticos menores y aparatos de línea blanca"
)
c <- read.csv(paste0(files[3], "/conjunto_de_datos/", "denue_inegi_46321-46531_.csv"),
encoding = "latin1"
) %>%
st_as_sf(coords = c("longitud", "latitud"), crs = 4326) %>%
filter(entidad == "CIUDAD DE MÉXICO") %>%
filter(nombre_act == "Comercio al por menor de discos y casetes" )
d <- read.csv(paste0(files[2], "/conjunto_de_datos/", "denue_inegi_46112-46311_.csv"),
encoding = "latin1"
) %>%
st_as_sf(coords = c("longitud", "latitud"), crs = 4326) %>%
filter(entidad == "CIUDAD DE MÉXICO") %>%
filter(nombre_act == "Comercio al por menor en tiendas departamentales" )
datos <- rbind.data.frame(b,c,d)
class(datos)
## [1] "sf" "data.frame"
# Filtramos y generamos etiqueta para el tipo de tienda.
datos2 <- datos %>%
mutate(tienda = str_detect(string = nom_estab, pattern = or1(c("LIVERPOOL",
"BESTBUY",
"BEST BUY",
"BEST-BUY",
"MACSTORE",
"ISHOP"
)) ),
tienda2 = str_extract(string = nom_estab, pattern = or1(c("LIVERPOOL",
"BESTBUY",
"BEST BUY",
"BEST-BUY",
"MACSTORE",
"ISHOP"
)))) %>%
filter(tienda)
niveles(datos2$tienda2)
## [1] "BEST BUY" "ISHOP" "LIVERPOOL" "MACSTORE"
# Aniadimos las apple store de Antara y de Santa Fe
apple <- data.frame(tiendas = c("Antara", "Via Santa Fé"),
longitud = c(-99.203397, -99.276048),
latitud = c(19.439928, 19.361658)
) %>%
st_as_sf(coords = c("longitud", "latitud"), crs = 4326)
A continuación, realizamos el mapa, utilizando la librería leaflet.
# Paleta de colores
pal <- colorFactor(c("blue", "black", "pink", "orange"),
domain = datos2$tienda2
)
# Mapa
leaflet(datos2, options = leafletOptions(zoomControl = FALSE, minZoom = 10)) %>%
setMaxBounds(lng1 = -99.507504, lat1 = 18.980224, lng2 = -98.722426, lat2 = 19.60) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons(data = mpios,
fill = F,
color = "black",
weight = 0.7
) %>%
addPolygons(data = a,
fill = F,
color = "black",
weight = 3
) %>%
addCircleMarkers(color = "black",
radius = 2
) %>%
addCircleMarkers(color = pal(datos2$tienda2),
label = datos2$nom_estab,
radius = 1,
opacity = 1
) %>%
addLegend(colors = "red", labels = "Apple Stores", position = "bottomleft") %>%
addLegend(title = "<b style = 'color: blue;'>Lugares donde comprar <br>computadoras Mac </b>",
pal = pal,
values = datos2$tienda2,
position = "bottomleft",
opacity = 1
) %>%
addCircleMarkers(data = apple,
radius = 3,
opacity = 1,
color = "black") %>%
addCircleMarkers(data = apple,
radius = 2,
opacity = 1,
color = "red",
label = apple$tiendas
) %>%
leaflet.extras::suspendScroll(wakeMessage = "Haga click o mantenga el cursor sobre el mapa", wakeTime = 1250)