Paquetes de R
x <- c("sf","ggplot2","cowplot","rcartocolor","broom","rgdal","tidyverse")
lapply(x, library, character.only = TRUE) # carga los paquetes requeridos
Mapa de la zona del contexto
rutamc<-"D:/Documents/Claudia/Midropbox/Investigacion y escritos/mapasnacionales"
ylim=c(25.47, 25.67)
xlim=c(-103.30, -103.52)
tormun <- readOGR(rutamc,"torageb", use_iconv = TRUE, encoding = "latin1")
## OGR data source with driver: ESRI Shapefile
## Source: "D:\Documents\Claudia\Midropbox\Investigacion y escritos\mapasnacionales", layer: "torageb"
## with 332 features
## It has 5 fields
# Para poder graficar con ggplot se convierte en una tabla (dataframe) (paquete broom)
tormun$id <- rownames(tormun@data)
tormun_df <- tidy(tormun, region = "id")
# Se agrega un identificador al shapefile que corresponderá al número de renglón
# Se reunen los dos archivos a partir del id
tormun_df <- left_join(tormun_df,tormun@data,by = "id")
Procedimiento con un polÃgono como marco
rutamc<-"D:/Documents/Claudia/Midropbox/Investigacion y escritos/mapasnacionales"
tormunc <- readOGR(rutamc,"muntor", use_iconv = TRUE, encoding = "latin1")
## OGR data source with driver: ESRI Shapefile
## Source: "D:\Documents\Claudia\Midropbox\Investigacion y escritos\mapasnacionales", layer: "muntor"
## with 36105 features
## It has 11 fields
rutacam<-"D:/Documents/Claudia/Midropbox/Investigacion y escritos/mapasnacionales/"
limitesbox<-read.csv(paste0(rutacam,"limites.txt"),header=TRUE, sep=",",encoding="latin")
names(limitesbox)<-c("long","lat")
#Crear el polÃgono
p = Polygon(limitesbox)
ps = Polygons(list(p),1)
sps = SpatialPolygons(list(ps))
sps_bb<-st_as_sfc(sps)
mapgde = ggplot() +
geom_path(data = tormun_df,aes(x = long, y = lat, group = group)) +
geom_sf(data = sps_bb, fill=NA,color = "red", size = 1.2) +
theme_void()
mapgde
#proj4string(sps)
#proj4string(tormun)
sp::proj4string(sps) <- CRS("+proj=longlat +datum=WGS84 +no_defs")
muninsps <- tormunc[sps, ]
zonarec = ggplot() +
geom_path(data = muninsps,aes(x = long, y = lat, group = group, fill = "white"))+
theme_void()
zonarec
map_inset = ggdraw() +
draw_plot(zonarec, x = 0.0, y = 0, width = 1, height = 1) +
draw_plot(mapgde, x = 0.75, y = 0.7, width = 0.3, height = 0.3)
map_inset
via_paletacol <- c("Calle" = "green",
"Boulevard" ="red",
"Avenida" = "blue",
"Privada" = "grey",
"Circuito" = "orange",
"Cerrada" = "purple")
muninsps_df <- tidy(muninsps, region = "id")
# Se agrega un identificador al shapefile que corresponderá al número de renglón
muninsps$id <- rownames(muninsps@data)
# Se reunen los dos archivos a partir del id
muninsps_df <- left_join(muninsps_df,muninsps@data,by = "id")
zonarec = ggplot() + geom_path(data = muninsps_df, aes(x = long, y = lat, group = group, color=TIPOVIAL)) +scale_colour_manual(values =via_paletacol) +
theme(axis.text = element_blank(), axis.ticks = element_blank())+ coord_quickmap()+theme_void()
zonarec
map_inset = ggdraw() +
draw_plot(zonarec, x = 0.0, y = 0, width = 1, height = 1) +
draw_plot(mapgde, x = 0.75, y = 0.7, width = 0.3, height = 0.3)
map_inset
ggsave("inset_map.png", width = 1300, height = 1300, units = "mm", dpi = "retina",limitsize = FALSE)