##1. Vamos a leer el paquete GPKG
#install.packages("sf")
library(ggplot2)
Cargar la librerÃa sf
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
#importamos geopackage desde git hub
linkWorld_gpkg <- "https://raw.githubusercontent.com/Richmaninoff/introgeodf/main/maps/worldMap.gpkg"
#usamos st_layers para lista de las capas presentes
sf::st_layers(linkWorld_gpkg)
## Driver: GPKG
## Available layers:
## layer_name geometry_type features fields crs_name
## 1 countryBorders 252 1 WGS 84
## 2 riverLines 98 2 WGS 84
## 3 cityPoints Point 610 3 WGS 84
countries=read_sf(linkWorld_gpkg,layer="countryBorders")
rivers=read_sf(linkWorld_gpkg,layer="riverLines")
cities=read_sf(linkWorld_gpkg,layer="cityPoints")
# Dibujar las capas usando ggplot2
baseLayer=ggplot(data=countries) + geom_sf(fill='grey90') + theme_light()
final=baseLayer + geom_sf(data=rivers, color='blue') + geom_sf(data=cities, color='red')
final
Ejercicio 2
Follow the same steps in this last section, but use Peru. Plot your three layers in R.
#Importando paises
Archivo = "C:/Users/Ricardo/Desktop/ASIES-Herramientas cuantitativas/S4. DATA/maps/World_Countries/World_Countries.shp"
countries=st_read(Archivo)
## Reading layer `World_Countries' from data source
## `C:\Users\Ricardo\Desktop\ASIES-Herramientas cuantitativas\S4. DATA\maps\World_Countries\World_Countries.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 252 features and 1 field
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -180 ymin: -90 xmax: 180 ymax: 83.6236
## Geodetic CRS: WGS 84
#importando rios
Archivorios = "C:/Users/Ricardo/Desktop/ASIES-Herramientas cuantitativas/S4. DATA/maps/World_Hydrography/World_Hydrography.shp"
rios=st_read(Archivorios)
## Reading layer `World_Hydrography' from data source
## `C:\Users\Ricardo\Desktop\ASIES-Herramientas cuantitativas\S4. DATA\maps\World_Hydrography\World_Hydrography.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 98 features and 2 fields
## Geometry type: MULTILINESTRING
## Dimension: XY
## Bounding box: xmin: -164.6246 ymin: -36.90435 xmax: 161.0658 ymax: 72.23418
## Geodetic CRS: WGS 84
#importando ciudades
Archivocit = "C:/Users/Ricardo/Desktop/ASIES-Herramientas cuantitativas/S4. DATA/maps/World_Cities/World_Cities.shp"
ciudades=st_read(Archivocit)
## Reading layer `World_Cities' from data source
## `C:\Users\Ricardo\Desktop\ASIES-Herramientas cuantitativas\S4. DATA\maps\World_Cities\World_Cities.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 610 features and 3 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -165.27 ymin: -53.15 xmax: 177.1302 ymax: 78.2
## Geodetic CRS: WGS 84
Creaando el objeto peru
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
peru <- countries %>% filter(COUNTRY == 'Peru')
citiesperu_clipped <- st_intersection(ciudades, peru)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
riversperu_clipped <- st_intersection(rios, peru)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
graficoperu=ggplot(data=peru) + geom_sf(fill='grey90') + theme_light()
visual1=graficoperu + geom_sf(data=riversperu_clipped, color='blue') + geom_sf(data=citiesperu_clipped, color='red')
visual1
ejercicio 3.
library(rio)
data=import("pe_airports.csv")
#Importando los aeropuertos
Archivo4 = "C:/Users/Ricardo/Desktop/ASIES-Herramientas cuantitativas/S4. DATA/maps/peruoair_4326.shp"
peruoair_4326=st_read(Archivo4)
## Reading layer `peruoair_4326' from data source
## `C:\Users\Ricardo\Desktop\ASIES-Herramientas cuantitativas\S4. DATA\maps\peruoair_4326.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 203 features and 7 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -81.27556 ymin: -18.0533 xmax: -68.99421 ymax: -0.119056
## Geodetic CRS: WGS 84
#creamos la intersección
peru_air <- st_intersection(peruoair_4326, peru)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
baseLayer=ggplot(data=peru) + geom_sf(fill='grey90') + theme_light()
final=baseLayer + geom_sf(data=riversperu_clipped, color='blue') + geom_sf(data=peru_air, color='black') + geom_sf(data=citiesperu_clipped, color='red') +
coord_sf(datum = st_crs(peru))
final
## ejercicio 4.
#install.packages("remotes")
library(remotes)
america="C:/Users/Ricardo/Desktop/ASIES-Herramientas cuantitativas/S4. DATA/maps/America_2023_prjed.gpkg"
sf::st_layers(america)
## Driver: GPKG
## Available layers:
## layer_name geometry_type features fields crs_name
## 1 countries 31 4 WGS 84 / Equal Earth Americas
## 2 centroids Point 31 4 WGS 84 / Equal Earth Americas
ame=read_sf(america,layer="countries")
ame_cen=read_sf(america,layer="centroids")
library(ggplot2)
baseLayer=ggplot(data=ame) + geom_sf(fill='grey90') + theme_light()
E4=baseLayer + geom_sf(data=ame_cen,aes(color=Total_ei5_cat),size=2+ame_cen$Total_ei5) +
guides(size=NULL) +
coord_sf(datum = st_crs(ame))
E4