This is my second R notebook written using R Markdown Notebook. It illustrates how to make a map with polygons (representing departments in colombia). The reference data set is a shapefile provided by DANE.
Let’s start loading the libraries
library(rgeos)
library(sf)
library(leaflet)
Let’s read the “ciudades” dataset, but this time as a table:
cities <- read.table(file = "/Users/Acer/Documents/Materias U/Geomatica Basica/cities.txt", header = F, sep = ";" )
class(cities)
## [1] "data.frame"
head(cities)
names(cities) <- c("ID", "Country", "City", "Latitude", "Longitude", "Altitude")
cities
Now, we convert the “dataframe” into a spatial object:
m <- st_as_sf(cities, coords = c(5,4,6))
m
class(m)
## [1] "sf" "data.frame"
coords <- st_coordinates(m)
class(coords)
## [1] "matrix" "array"
# Create vector of coordinates
# What are vectors in R
#https://datascienceplus.com/vectors-and-functions-in-r/#:~:text=A%20vector%20is%20the%20simplest,a%20vector%20of%20logical%20values.
lat =coords[,2]
long = coords[,1]
alt = coords[,3]
deptos <- read_sf("/Users/Acer/Documents/Materias U/Geomatica Basica/MGN_DPTO/MGN_DPTO_POLITICO.shp")
class(deptos)
## [1] "sf" "tbl_df" "tbl" "data.frame"
st_crs(deptos)
## Coordinate Reference System:
## User input: WGS 84
## wkt:
## GEOGCRS["WGS 84",
## DATUM["World Geodetic System 1984",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["latitude",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["longitude",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4326]]
head(deptos)
deptos$DPTO_CCDGO
## [1] "18" "19" "86" "76" "94" "99" "85" "91" "97" "95" "17" "63" "66" "05" "27"
## [16] "52" "23" "13" "20" "44" "47" "70" "88" "81" "15" "25" "54" "11" "50" "41"
## [31] "68" "73" "08"
# We need to project the deptos object(convert it into map coordinates)
deptos2 <- deptos %>% st_transform(3116)
deptos3 <- sf::st_simplify(deptos2, preserveTopology = T, dTolerance = 100)
object.size(deptos)
## 16290112 bytes
object.size(deptos3)
## 901920 bytes
As “Leaflet” works with CRS WGS84, we need to transform the simplified object into geographic coordinates:
deptos4 <- deptos3 %>% st_transform(4326)
mapa <- leaflet(deptos4)
mapa <- addTiles(mapa)
labels <- sprintf("<strong>%s</strong><br/>%g unkown units</sup>", deptos4$DPTO_CNMBR, deptos4$SHAPE_AREA) %>% lapply(htmltools::HTML)
mapa <- addPolygons(mapa, color = "#444444", weight = 1, smoothFactor = 0.5,
opacity = 0.5, fillOpacity = 0.5,
fillColor = ~colorQuantile("YlOrRd", SHAPE_AREA)(SHAPE_AREA),
highlightOptions = highlightOptions(color = "red", weight = 2,
bringToFront = T), label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto"))
mapa <- addMarkers(mapa, lng = long, lat = lat, popup = m$City)
mapa
sessionInfo()
## R version 4.0.4 (2021-02-15)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Spanish_Colombia.1252 LC_CTYPE=Spanish_Colombia.1252
## [3] LC_MONETARY=Spanish_Colombia.1252 LC_NUMERIC=C
## [5] LC_TIME=Spanish_Colombia.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] leaflet_2.0.4.1 sf_0.9-7 rgeos_0.5-5 sp_1.4-5
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.6 compiler_4.0.4 pillar_1.5.1 RColorBrewer_1.1-2
## [5] class_7.3-18 tools_4.0.4 digest_0.6.27 jsonlite_1.7.2
## [9] evaluate_0.14 tibble_3.1.0 lifecycle_1.0.0 lattice_0.20-41
## [13] pkgconfig_2.0.3 rlang_0.4.10 DBI_1.1.1 crosstalk_1.1.1
## [17] yaml_2.2.1 xfun_0.21 e1071_1.7-4 stringr_1.4.0
## [21] knitr_1.31 vctrs_0.3.6 htmlwidgets_1.5.3 classInt_0.4-3
## [25] grid_4.0.4 R6_2.5.0 fansi_0.4.2 rmarkdown_2.7
## [29] farver_2.1.0 magrittr_2.0.1 scales_1.1.1 htmltools_0.5.1.1
## [33] ellipsis_0.3.1 units_0.7-0 colorspace_2.0-0 KernSmooth_2.23-18
## [37] utf8_1.1.4 stringi_1.5.3 munsell_0.5.0 crayon_1.4.1