1. Introduction

This is the second R Markdown Notebook for the Basic Geomatics 2023 course. It illustrates thematic maps that show the municipal participation of the two most important crop groups for the department of Antioquia, which are fruit trees and tubers. We will use the archives as the main source. csv files saved in the EVA notebook, as well as a geopackage of municipalities obtained in class.

2. Setup

In this section we write the code to load the libraries

library(sf)
library(dplyr)
library(readr)

4. Subset data relevant to our department

Find points (cities) within polygons (our municipalities)

sf.cities.joined <- st_join(sf.cities, municipios_an, join = st_within)

To display the joined object

sf.cities.joined
## Simple feature collection with 1104 features and 11 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -81.7006 ymin: -4.2167 xmax: -67.4667 ymax: 13.3817
## Geodetic CRS:  WGS 84
## # A tibble: 1,104 × 12
##    city         country  iso2  admin_name   capital population population_proper
##  * <chr>        <chr>    <chr> <chr>        <chr>        <dbl>             <dbl>
##  1 Bogotá       Colombia CO    Bogotá       primary    7968095           7743955
##  2 Timbío       Colombia CO    Cauca        minor      4444444           4444444
##  3 Medellín     Colombia CO    Antioquia    admin      2529403           2529403
##  4 Cali         Colombia CO    Valle del C… admin      2471474           2471474
##  5 Barranquilla Colombia CO    Atlántico    admin      1326588           1274250
##  6 Cartagena    Colombia CO    Bolívar      admin      1036412           1036412
##  7 Bucaramanga  Colombia CO    Santander    admin       870752            581130
##  8 Palermo      Colombia CO    Huila        minor       800000            800000
##  9 Cúcuta       Colombia CO    Norte de Sa… admin       750000            750000
## 10 Soledad      Colombia CO    Atlántico    minor       698852            455796
## # ℹ 1,094 more rows
## # ℹ 5 more variables: geometry <POINT [°]>, MUNI_COD <chr>, MUN <chr>,
## #   AREA <dbl>, CODIGO <int>

Now, let’s filter the rows which correspond to our deparment (in this case Antioquia)

stder.cities = dplyr::filter(sf.cities.joined, admin_name=='Antioquia')
stder.cities
## Simple feature collection with 125 features and 11 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -76.8958 ymin: 5.5833 xmax: -73.9167 ymax: 8.8517
## Geodetic CRS:  WGS 84
## # A tibble: 125 × 12
##    city        country  iso2  admin_name capital population population_proper
##  * <chr>       <chr>    <chr> <chr>      <chr>        <dbl>             <dbl>
##  1 Medellín    Colombia CO    Antioquia  admin      2529403           2529403
##  2 Bello       Colombia CO    Antioquia  minor       495483            371591
##  3 Itagüí      Colombia CO    Antioquia  minor       276744            276744
##  4 Envigado    Colombia CO    Antioquia  minor       228848            228848
##  5 Turbo       Colombia CO    Antioquia  minor       181000            181000
##  6 Rionegro    Colombia CO    Antioquia  minor       135465            135465
##  7 Apartadó    Colombia CO    Antioquia  minor       121003            121003
##  8 Sabanalarga Colombia CO    Antioquia  minor       102334            102334
##  9 Caucasia    Colombia CO    Antioquia  minor        90213             90213
## 10 Chigorodó   Colombia CO    Antioquia  minor        84183             84183
## # ℹ 115 more rows
## # ℹ 5 more variables: geometry <POINT [°]>, MUNI_COD <chr>, MUN <chr>,
## #   AREA <dbl>, CODIGO <int>

We obtained 125 cities located in Antioquia.

5. Make a map for the most important group of crops

Now, we will make a choropleth map of these data. We will use the tmap, ggplot2 and classInt libraries

library(tmap)
library(ggplot2)
library(ggrepel)
library(classInt)

Check according to your own data

class(frutales$COD_MUN)
## [1] "numeric"

Thus, we need to fix this

municipios_an$COD_MUN = as.integer(municipios_an$CODIGO)

Now, We perform the union of two data which are municipios_an an and frutales

mun_frut = left_join(municipios_an, frutales, by = c("COD_MUN" = "COD_MUN"))

Giving us the following result

mun_frut
## Simple feature collection with 125 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -77.12783 ymin: 5.418558 xmax: -73.88128 ymax: 8.873974
## Geodetic CRS:  WGS 84
## First 10 features:
##    MUNI_COD                MUN.x       AREA CODIGO COD_MUN          MUN.y
## 1       001       MEDELLÃ\u008dN  374830625   5001    5001       MEDELLIN
## 2       101 CIUDAD BOLÃ\u008dVAR  260446108   5101    5101 CIUDAD BOLIVAR
## 3       107             BRICEÑO  376346756   5107    5107       BRICEÑO
## 4       113       BURITICÃ\u0081  355210256   5113    5113       BURITICA
## 5       120        CÃ\u0081CERES 1873802834   5120    5120           <NA>
## 6       093              BETULIA  262367503   5093    5093        BETULIA
## 7       091              BETANIA  180525987   5091    5091        BETANIA
## 8       088                BELLO  147758418   5088    5088          BELLO
## 9       086              BELMIRA  296153167   5086    5086        BELMIRA
## 10      079              BARBOSA  205666168   5079    5079        BARBOSA
##    GRUPO_CULTIVO MAX_PROD                           geom
## 1       FRUTALES     1050 MULTIPOLYGON (((-75.66974 6...
## 2       FRUTALES     1640 MULTIPOLYGON (((-76.04467 5...
## 3       FRUTALES       72 MULTIPOLYGON (((-75.45818 7...
## 4       FRUTALES        0 MULTIPOLYGON (((-75.90857 6...
## 5           <NA>       NA MULTIPOLYGON (((-75.20358 7...
## 6       FRUTALES     6000 MULTIPOLYGON (((-76.00304 6...
## 7       FRUTALES      320 MULTIPOLYGON (((-75.95474 5...
## 8       FRUTALES        0 MULTIPOLYGON (((-75.58203 6...
## 9       FRUTALES      105 MULTIPOLYGON (((-75.69252 6...
## 10      FRUTALES      369 MULTIPOLYGON (((-75.32144 6...

Customize the colors of the municipalities

breaks <- classIntervals(mun_frut$MAX_PROD, n = 6, style = 'fisher')
## Warning in classIntervals(mun_frut$MAX_PROD, n = 6, style = "fisher"): var has
## missing values, omitted in finding classes
#label breaks
lab_vec <- vector(length = length(breaks$brks)-1)
rounded_breaks <- round(breaks$brks,2)
lab_vec[1] <- paste0('[', rounded_breaks[1],' - ', rounded_breaks[2],']')
for(i in 2:(length(breaks$brks) - 1)){
  lab_vec[i] <- paste0('(',rounded_breaks[i], ' - ', rounded_breaks[i+1], ']')
}

Now, the static map

mun_frut <-  mun_frut %>%
  mutate(faktor_class = factor(cut(MAX_PROD, breaks$brks, include.lowest = T), labels = lab_vec))

Change attribute name

mun_frut$Produccion = mun_frut$faktor_class

This code creates a new field (“mid”) with centroid coordinate

mun_frut$mid <- sf::st_centroid(mun_frut$geom)

Get the longitude values

LONG = st_coordinates(mun_frut$mid)[,1]

Get the latitude values

LAT = st_coordinates(mun_frut$mid)[,2]

we found attributes of the municipalities

mun.tmp$MUN -> MUN1

We create a map using the ggplot function

ggplot(data = mun_frut) +
   geom_sf(aes(fill = Produccion)) +
   geom_label_repel(aes(x = LONG, y = LAT, label = MUN1), 
                    label.padding =     unit(0.05,"lines"),  
                    label.r = unit(0.025, "lines"),
                    label.size = 0.05)
## Warning: ggrepel: 102 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

6. Make a new map for the second largest group of crops

(tuber = read_csv("C:/GB2/CUADERNO_2/tuberyplat2018.csv",show_col_types = FALSE))
## # A tibble: 118 × 4
##    COD_MUN MUN                GRUPO_CULTIVO         MAX_PROD
##      <dbl> <chr>              <chr>                    <dbl>
##  1    5837 TURBO              TUBERCULOS Y PLATANOS    84968
##  2    5893 YONDO              TUBERCULOS Y PLATANOS    54000
##  3    5659 SAN JUAN DE URABA  TUBERCULOS Y PLATANOS    47560
##  4    5490 NECOCLI            TUBERCULOS Y PLATANOS    23090
##  5    5250 EL BAGRE           TUBERCULOS Y PLATANOS    18000
##  6    5051 ARBOLETES          TUBERCULOS Y PLATANOS    17505
##  7    5756 SONSON             TUBERCULOS Y PLATANOS    17160
##  8    5368 JERICO             TUBERCULOS Y PLATANOS    16750
##  9    5686 SANTA ROSA DE OSOS TUBERCULOS Y PLATANOS    15500
## 10    5674 SAN VICENTE        TUBERCULOS Y PLATANOS    15400
## # ℹ 108 more rows

We carry out the same steps that we used previously

Set the data type to be equal

tuber$COD_MUN = as.integer(tuber$COD_MUN)

Now, We perform the union of two data which are municipios_an an and tuberculos y platanos

mun_tuber = left_join(municipios_an, tuber, by = c("COD_MUN" = "COD_MUN"))
mun_tuber
## Simple feature collection with 125 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -77.12783 ymin: 5.418558 xmax: -73.88128 ymax: 8.873974
## Geodetic CRS:  WGS 84
## First 10 features:
##    MUNI_COD                MUN.x       AREA CODIGO COD_MUN          MUN.y
## 1       001       MEDELLÃ\u008dN  374830625   5001    5001       MEDELLIN
## 2       101 CIUDAD BOLÃ\u008dVAR  260446108   5101    5101 CIUDAD BOLIVAR
## 3       107             BRICEÑO  376346756   5107    5107        BRICEÑO
## 4       113       BURITICÃ\u0081  355210256   5113    5113       BURITICA
## 5       120        CÃ\u0081CERES 1873802834   5120    5120        CACERES
## 6       093              BETULIA  262367503   5093    5093        BETULIA
## 7       091              BETANIA  180525987   5091    5091        BETANIA
## 8       088                BELLO  147758418   5088    5088          BELLO
## 9       086              BELMIRA  296153167   5086    5086        BELMIRA
## 10      079              BARBOSA  205666168   5079    5079        BARBOSA
##            GRUPO_CULTIVO MAX_PROD                           geom
## 1  TUBERCULOS Y PLATANOS      969 MULTIPOLYGON (((-75.66974 6...
## 2  TUBERCULOS Y PLATANOS     2464 MULTIPOLYGON (((-76.04467 5...
## 3  TUBERCULOS Y PLATANOS      609 MULTIPOLYGON (((-75.45818 7...
## 4  TUBERCULOS Y PLATANOS       56 MULTIPOLYGON (((-75.90857 6...
## 5  TUBERCULOS Y PLATANOS    14700 MULTIPOLYGON (((-75.20358 7...
## 6  TUBERCULOS Y PLATANOS      960 MULTIPOLYGON (((-76.00304 6...
## 7  TUBERCULOS Y PLATANOS     1554 MULTIPOLYGON (((-75.95474 5...
## 8  TUBERCULOS Y PLATANOS     1857 MULTIPOLYGON (((-75.58203 6...
## 9  TUBERCULOS Y PLATANOS     2160 MULTIPOLYGON (((-75.69252 6...
## 10 TUBERCULOS Y PLATANOS      760 MULTIPOLYGON (((-75.32144 6...

Map of the production of tubers and plantains antioquiamap of the production of tubers and plantains antioquia

sebas = "MAX_PROD"
tuber_map =  
  tm_shape(mun_tuber) + tm_polygons(sebas) + tm_text(text = "MUN.y", size = 0.7, fontfamily = "sans") +
  tm_shape(stder.cities) + tm_symbols(shape = 2, col = "red", size = 0.20) +
  tm_credits( "Data source: UPRA (2018)", fontface = "bold") +
  tm_layout(main.title = "Produccion de tuberculos y platanos en 2018",
            main.title.fontface = "bold.italic", 
            legend.title.fontfamily = "monospace") +
  tm_scale_bar(position = c("left", "bottom"))
tmap_mode("view")

Map result

tuber_map
## Credits not supported in view mode.
## Symbol shapes other than circles or icons are not supported in view mode.

7. Bibliography

- Lizarazo, I., 2022. Getting started with thematic maps. Available at https://rpubs.com/ials2un/thematic_maps_v2

- MGN2018 Integrado con CNPV2018, nivel de Municipio

sessionInfo()
## R version 4.3.0 (2023-04-21 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19045)
## 
## Matrix products: default
## 
## 
## locale:
## [1] LC_COLLATE=Spanish_Colombia.utf8  LC_CTYPE=Spanish_Colombia.utf8   
## [3] LC_MONETARY=Spanish_Colombia.utf8 LC_NUMERIC=C                     
## [5] LC_TIME=Spanish_Colombia.utf8    
## 
## time zone: America/Bogota
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] classInt_0.4-9 ggrepel_0.9.3  ggplot2_3.4.2  tmap_3.3-3     readr_2.1.4   
## [6] dplyr_1.1.2    sf_1.0-12     
## 
## loaded via a namespace (and not attached):
##  [1] gtable_0.3.3            xfun_0.39               bslib_0.4.2            
##  [4] raster_3.6-20           htmlwidgets_1.6.2       lattice_0.21-8         
##  [7] tzdb_0.3.0              leaflet.providers_1.9.0 vctrs_0.6.2            
## [10] tools_4.3.0             crosstalk_1.2.0         generics_0.1.3         
## [13] parallel_4.3.0          tibble_3.2.1            proxy_0.4-27           
## [16] fansi_1.0.4             highr_0.10              pkgconfig_2.0.3        
## [19] KernSmooth_2.23-20      RColorBrewer_1.1-3      leaflet_2.1.2          
## [22] lifecycle_1.0.3         farver_2.1.1            compiler_4.3.0         
## [25] munsell_0.5.0           terra_1.7-29            codetools_0.2-19       
## [28] leafsync_0.1.0          stars_0.6-1             htmltools_0.5.5        
## [31] class_7.3-21            sass_0.4.6              yaml_2.3.7             
## [34] pillar_1.9.0            crayon_1.5.2            jquerylib_0.1.4        
## [37] ellipsis_0.3.2          cachem_1.0.8            lwgeom_0.2-11          
## [40] wk_0.7.2                abind_1.4-5             mime_0.12              
## [43] tidyselect_1.2.0        digest_0.6.31           fastmap_1.1.1          
## [46] grid_4.3.0              colorspace_2.1-0        cli_3.6.1              
## [49] magrittr_2.0.3          base64enc_0.1-3         dichromat_2.0-0.1      
## [52] XML_3.99-0.14           utf8_1.2.3              leafem_0.2.0           
## [55] e1071_1.7-13            withr_2.5.0             scales_1.2.1           
## [58] sp_1.6-0                bit64_4.0.5             rmarkdown_2.21         
## [61] bit_4.0.5               png_0.1-8               hms_1.1.3              
## [64] evaluate_0.21           knitr_1.42              tmaptools_3.1-1        
## [67] viridisLite_0.4.2       markdown_1.6            s2_1.1.3               
## [70] rlang_1.1.1             Rcpp_1.0.10             glue_1.6.2             
## [73] DBI_1.1.3               rstudioapi_0.14         vroom_1.6.3            
## [76] jsonlite_1.8.4          R6_2.5.1                units_0.8-2