En este cuaderno se va a trabajar la construcción de un mapa del departamento del Bolívar con sus respectivas ciudades
## a) Cargar las liberias que se van a usar durante el proyecto
library(sf)
library(dplyr)
library(ggplot2)
library(ggspatial)
## b)Observarlos archivos que hay en la carpeta que se va a trabajar
list.files("~/GB_CristianEljadue/GB_CristianEljadue/P3R/MGN2023_MPIO_POLITICO")
## [1] "bolivar_munic.gpkg" "MGN_ADM_MPIO_GRAFICO.cpg"
## [3] "MGN_ADM_MPIO_GRAFICO.dbf" "MGN_ADM_MPIO_GRAFICO.prj"
## [5] "MGN_ADM_MPIO_GRAFICO.sbn" "MGN_ADM_MPIO_GRAFICO.sbx"
## [7] "MGN_ADM_MPIO_GRAFICO.shp" "MGN_ADM_MPIO_GRAFICO.shp.xml"
## [9] "MGN_ADM_MPIO_GRAFICO.shx" "MUNICIPIOS.gpkg"
## c) Nombrar al archivo shp con el que se va a realizar el mapa
Colombia<- st_read("~/GB_CristianEljadue/GB_CristianEljadue/P3R/MGN2023_MPIO_POLITICO/MGN_ADM_MPIO_GRAFICO.shp")#nombrar el shp file de los municipios
## Reading layer `MGN_ADM_MPIO_GRAFICO' from data source
## `C:\Users\juanc\OneDrive\Documentos\GB_CristianEljadue\GB_CristianEljadue\P3R\MGN2023_MPIO_POLITICO\MGN_ADM_MPIO_GRAFICO.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 1121 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -81.73562 ymin: -4.229406 xmax: -66.84722 ymax: 13.39473
## Geodetic CRS: MAGNA-SIRGAS
head(Colombia)
## D) Crear un nuevo archivo geopackage con el archivo nombrado y nombrarlo
st_write(Colombia,"MUNICIPIOS.gpkg",driver="GPKG",append=F)#crear un nuevo gpkg
## Deleting layer `MUNICIPIOS' using driver `GPKG'
## Writing layer `MUNICIPIOS' to data source `MUNICIPIOS.gpkg' using driver `GPKG'
## Writing 1121 features with 11 fields and geometry type Multi Polygon.
list.files(pattern ="gpkg")
## [1] "bolivar_munic.gpkg" "MUNICIPIOS.gpkg"
colombia2<-st_read("MUNICIPIOS.gpkg")#Se lee el nuevo gpkg creado
## Reading layer `MUNICIPIOS' from data source
## `C:\Users\juanc\OneDrive\Documentos\MUNICIPIOS.gpkg' using driver `GPKG'
## Simple feature collection with 1121 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -81.73562 ymin: -4.229406 xmax: -66.84722 ymax: 13.39473
## Geodetic CRS: MAGNA-SIRGAS
## E)Filtrar el elemento que va a ser usado para el mapa en este caso el departamento del Bolívar
bolivar <- dplyr::filter(colombia2, dpto_cnmbr=="BOLÍVAR")#Filtral el departamento
## F)Realizar un grafico basico para observar si se han cargado correctamente las variables nombradas
plot(st_geometry(bolivar), col = sf.colors(12, categorical = TRUE), border = 'grey',
axes = TRUE)
plot(st_geometry(st_centroid(bolivar)), pch = 3, col = 'red', add = TRUE)
st_write(bolivar, "bolivar_munic.gpkg", driver = "GPKG", append = F)
## Deleting layer `bolivar_munic' using driver `GPKG'
## Writing layer `bolivar_munic' to data source `bolivar_munic.gpkg' using driver `GPKG'
## Writing 46 features with 11 fields and geometry type Multi Polygon.
G) Cargar otro elemnto para el mapa en este caso las ciudades del del Departamento
cities = read.csv(("~/GB_CristianEljadue/GB_CristianEljadue/P3R/CIUDADES/simplemaps_worldcities_basicv1.77/worldcities.csv")) %>%
st_as_sf(coords=c("lng","lat"), crs=4326)#ortoga las coordenadas
H) Revisar que los archivos esten en las mismas coordenas y transformarlas en caso de que no coincidad, en este proyecto que las coordenadas coordinen con el departamento.
st_crs(cities)$epsg# verificar las coordenadas
## [1] 4326
st_crs(bolivar)$epsg# verificar las coordenadas
## [1] 4686
ncities <- st_transform(cities, crs= st_crs(bolivar))#transforma las coordenas para que este con el departamento
st_crs(ncities)$epsg
## [1] 4686
I) Seleccionar las ciudades del departamento y verificarlo con un grafico rudimentario
bolivar_cities<-ncities[bolivar, , op = st_within]#Seleciona unicamente las ciudades del departamento
plot(st_geometry(bolivar), col = sf.colors(13, categorical = TRUE), border = 'grey', axes = TRUE)
plot(st_geometry(bolivar_cities), pch = 20, col = 'red', add = TRUE)#grafico rudimentario
J) Graficar de manera mas completa con datos geoespaciales añadiendo la flecha del norte y la escala
#Grafico con datos geoespaciales
ggplot() +
geom_sf(data = bolivar) +
#Add cities layer
geom_sf(data = bolivar_cities, aes(color = city, label = city), size = 3) +
#Add scale bar to bottom left from ggspatial
annotation_scale(location = "tr", height = unit(.25, "cm"),
width = unit(1, "cm"), pad_x = unit(1.7, "in"),
pad_y = unit(4, "in")) +
#Add north arrow to bottom left from ggspatial
annotation_north_arrow(height = unit(1, "cm"), width = unit(1, "cm"),
which_north = "true", location = "tr",
pad_x = unit(0.1, "in"), pad_y = unit(0.05, "in")) +
#Add titles
labs(x = "Longitud", y = "Latitud", title = "Ciudades de Santander") +
#Add theme
theme_bw()