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.
In this section we write the code to load the libraries
library(sf)
library(dplyr)
library(readr)
sf.cities.joined <- st_join(sf.cities, municipios_an, join = st_within)
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>
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.
library(tmap)
library(ggplot2)
library(ggrepel)
library(classInt)
class(frutales$COD_MUN)
## [1] "numeric"
municipios_an$COD_MUN = as.integer(municipios_an$CODIGO)
mun_frut = left_join(municipios_an, frutales, by = c("COD_MUN" = "COD_MUN"))
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...
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], ']')
}
mun_frut <- mun_frut %>%
mutate(faktor_class = factor(cut(MAX_PROD, breaks$brks, include.lowest = T), labels = lab_vec))
mun_frut$Produccion = mun_frut$faktor_class
mun_frut$mid <- sf::st_centroid(mun_frut$geom)
LONG = st_coordinates(mun_frut$mid)[,1]
LAT = st_coordinates(mun_frut$mid)[,2]
mun.tmp$MUN -> MUN1
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
(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
tuber$COD_MUN = as.integer(tuber$COD_MUN)
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...
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")
tuber_map
## Credits not supported in view mode.
## Symbol shapes other than circles or icons are not supported in view mode.