This is the second R Markdown Notebook for the Geomatica Basica 2022 course. It illustrates how to make thematic maps showing the municipal share of the two most important groups of crops for a given department. We will use as main source the csv files saved in the EVA notebook, as well as a shapefile of municipalities obtained in class.
In this step, we will install and load the required R libraries.
#run the following lines from the command line:
#install.packages('dplyr')
#install.packages('readxl')
#install.packages('sf)
library(sf)
library(dplyr)
library(readr)
As we are interested in just a single department we need to create an spatial join:
# find points (cities) within polygons (our municipalities)
sf.cities.joined <- st_join(sf.cities, municipios, join = st_within)
To display the joined object:
sf.cities.joined
Note that we obtained an sf data frame with each row of cities appended with the columns from our municipios. Cities located in a different department have a lot of NAs.
Now, let’s filter the rows which correspond to our deparment (in this case Santander):
stder.cities = dplyr::filter(sf.cities.joined, admin_name=='Santander')
stder.cities
We obtained 87 cities located in Santander.
Now, make a choropleth map of this data. We will use the tmap, ggplot2, and classInt libraries.
# run the following lines from the command window:
#install.packages("tmap")
#install.packages("ggplot2")
#install.packages("classInt")
library(tmap)
## Warning: package 'tmap' was built under R version 4.1.2
library(ggplot2)
library(ggrepel)
library(classInt)
Let’s remind that the municipios object do not have EVA attributes. In contrast, the frutales objects, which contains crop statistics, is a non-spatial object. Our next task is to join the stats object to the spatial objects to have the relevant data in a single object.
To be able to make the join, we need a shared key, i.e. a common attribute. In this case, we have it on both objects, that is the codigo municipal. However, note that both their names and their data types are different.
### check according to your own data
class(frutales$Cod_Mun)
## [1] "numeric"
class(municipios$MPIO_CCDGO)
## [1] "character"
Thus, we need to fix this:
frutales$Cod_Mun = as.character(frutales$Cod_Mun)
Now, we are ready to make a join. You may need to review your class notes for understanding what is going here:
munic_frutales = left_join(municipios, frutales, by = c("MPIO_CCDGO" = "Cod_Mun"))
What is the result?
munic_frutales
Note that munic_frutales includes now the max_prod attribute represenying the total amount of tons produced by fruit crops in 2020.
The following code customizes the colors for the municipalities. More information [here] (https://stackoverflow.com/questions/57177312/trying-to-plot-in-tmap-shapefile-with-attribute).
breaks <- classIntervals(munic_frutales$max_prod, n = 6, style = 'fisher')
#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:
munic_frutales <- munic_frutales %>%
mutate(faktor_class = factor(cut(max_prod, breaks$brks, include.lowest = T), labels = lab_vec))
# change attribute name
munic_frutales$Produccion = munic_frutales$faktor_class
# This code creates a new field ("mid") with centroid coordinates
munic_frutales$mid <- sf::st_centroid(munic_frutales$geometry)
# Get the longitude values
LONG = st_coordinates(munic_frutales$mid)[,1]
# Get the latitude values
LAT = st_coordinates(munic_frutales$mid)[,2]
ggplot(data = munic_frutales) +
geom_sf(aes(fill = Produccion)) +
geom_label_repel(aes(x = LONG, y = LAT, label = MPIO_CNMBR),
label.padding = unit(0.05,"lines"),
label.r = unit(0.025, "lines"),
label.size = 0.05)
Another option is to produce an interactive map:
Here, we will use EVA data for the “oleaginosas y hortalizas” group which accounts for the second largest production in 2020 in Santander.
We need to conduct similar steps to the previous ones:
(oleag = read_csv("./datos/stder_oleag_2020.csv",show_col_types = FALSE))
oleag$Cod_Mun = as.character(oleag$Cod_Mun)
#municipios$MPIO_CCDGO = as.character(municipios$MPIO_CCDGO)
munic_oleag = left_join(municipios, oleag, by = c("MPIO_CCDGO" = "Cod_Mun"))
munic_oleag
# see example at https://r-tmap.github.io/tmap-book/layout.html
facet = "max_prod"
oleag_map =
tm_shape(munic_oleag) + tm_polygons(facet) + tm_text(text = "MPIO_CNMBR", size = 0.7, fontfamily = "sans") +
tm_shape(stder.cities) + tm_symbols(shape = 2, col = "red", size = 0.20) +
tm_credits("Data source: UPRA (2020)", fontface = "bold") +
tm_layout(main.title = "Produccion de oleaginosas en 2020",
main.title.fontface = "bold.italic",
legend.title.fontfamily = "monospace") +
tm_scale_bar(position = c("left", "bottom"))
tmap_mode("view")
## tmap mode set to interactive viewing
oleag_map
## Credits not supported in view mode.
## Symbol shapes other than circles or icons are not supported in view mode.