Maps of Paraguay with R

1 Using maps package

1.1 Install and load the necesaries packages

# install.packages("sf")
# install.packages("raster")
# install.packages("dplyr")
# install.packages("spData")
# install.packages("spDataLarge")
# install.packages("tmap")
# install.packages("leaflet")
# install.packages("ggplot2")
# install.packages("spDataLarge", repos = "https://nowosad.github.io/drat/", type = "source")
# install.packages("tmaptools")
# install.packages("rgdal")
# install.packages("OpenStreetMap")
#install.packages("maps")
library("maps")
library("tmaptools")
library("rgdal")
## Loading required package: sp
## Please note that rgdal will be retired during 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## See https://r-spatial.org/r/2022/04/12/evolution.html and https://github.com/r-spatial/evolution
## rgdal: version: 1.6-2, (SVN revision 1183)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.0.4, released 2020/01/28
## Path to GDAL shared files: /usr/share/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
## Path to PROJ shared files: /usr/share/proj
## Linking to sp version:1.5-1
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library("OpenStreetMap")
library("sf")
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library("raster")
library("dplyr")
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library("spData")
library("tmap")    # for static and interactive maps
library("leaflet") # for interactive maps
library("ggplot2") # tidyverse data visualization package
library("spDataLarge")

1.2 Filter the region of interest

## make a df with only the country to overlap
map_data_py <- map_data("world")[map_data('world')$region == "Paraguay",]

#View(map_data_py)
summary(map_data_py$long)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  -62.65  -58.25  -57.83  -57.58  -55.65  -54.24
summary(map_data_py$lat)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  -27.55  -26.05  -23.58  -23.74  -22.10  -19.29

1.3 Draw the map

## The map (maps + ggplot2 )
ggplot() +
    ## First layer: worldwide map
    geom_polygon(data = map_data("world"),
                 aes(x=long, y=lat, group = group),
                 color = '#9c9c9c', fill = '#f3f3f3') +
    ## Second layer: Country map
    geom_polygon(data = map_data_py,
                 aes(x=long, y=lat, group = group),
                 color = 'red', fill = 'pink') +
    coord_map() +
    coord_fixed(1.3,
                xlim = c(-65, -50),
                ylim = c(-28, -18)) +
    ggtitle("A map of Paraguay") +
    theme(panel.background =element_rect(fill = 'blue'))
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

2 Using tmap library

#https://geocompr.robinlovelace.net/adv-map.html

data(World)
#str(World)
#View(World)
#table(World$name)
# create two shapes, one with border, one with selection (life expectancy bigger then 80 years) and only text
# 
tm_shape(World) + tm_borders() +
  
  tm_shape(World) + tm_text("iso_a3")

tm_shape(World) + 
    tm_borders() + 
tm_shape(World %>% filter(life_exp > 80)) + 
  tm_text("iso_a3")

data("World")

tm_shape(World) +
    tm_polygons("HPI")

data(World, metro, rivers, land)

tmap_mode("plot")
## tmap mode set to plotting
## tmap mode set to plotting
tm_shape(land) +
    tm_raster("elevation", palette = terrain.colors(10)) +
tm_shape(World) +
    tm_borders("white", lwd = .5) +
    tm_text("iso_a3", size = "AREA") +
tm_shape(metro) +
    tm_symbols(col = "red", size = "pop2020", scale = .5) +
tm_legend(show = FALSE)
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()

url <- "https://geodata.ucdavis.edu/gadm/gadm4.0/shp/gadm40_PRY_shp.zip"

download.file(url, "paraguay.zip")
unzip("paraguay.zip", exdir = "paraguay")
py <- st_read("paraguay/gadm40_PRY_2.shp")
## Reading layer `gadm40_PRY_2' from data source 
##   `/cloud/project/paraguay/gadm40_PRY_2.shp' using driver `ESRI Shapefile'
## Simple feature collection with 218 features and 12 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -62.64652 ymin: -27.60586 xmax: -54.25863 ymax: -19.29137
## Geodetic CRS:  WGS 84
#View(py)
#str(py)
py <- st_read("paraguay/gadm40_PRY_2.shp")
## Reading layer `gadm40_PRY_2' from data source 
##   `/cloud/project/paraguay/gadm40_PRY_2.shp' using driver `ESRI Shapefile'
## Simple feature collection with 218 features and 12 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -62.64652 ymin: -27.60586 xmax: -54.25863 ymax: -19.29137
## Geodetic CRS:  WGS 84
#añadir un valor aleatorio para la escala de calor en el mapa

py$refer <- rnorm(218,0,1)

#View(py)
map <- tm_shape(py) + tm_fill("NAME_1") + tm_borders()

print(map)

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(py) +
    tm_polygons(c("refer")) +
                  tm_facets(sync = TRUE, ncol = 2 )
## Variable(s) "refer" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

2.1 An example of “Heat Map” for the whole world, with two variables simultaneously

data("World")
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(World) +
    tm_polygons(c("HPI", "economy")) +
    tm_facets(sync = TRUE, ncol = 2)