This tutorial codethrough will lay out functions of the tmap package of the R programming language to create choropleth maps. The tmap package plots thematic maps in R with an API based on A Layered Grammar of Graphics that resembles the syntax of ggplot2. Basically creates a shapefile by declaration a of a plot with the data to be plotted followed by an addition of first in order commands for visualization refinement. Customizing appearance and adding new data onto the shapefile will change the map details.
The tMap is where you can do what many Data Integration specialists refer to as “lookups” on data. This is basically joining data from one source to another source. The tMap has a lot of functionality on lookups, like inner join or outer join, reject if join is not found, cache the lookup data and much more. Lookups are critical to data transformation process as you often need to pull in reference data or get expanded views of records.
tmap is available on CRAN.3.3-3. One can easily install the package as the first step to usage.
install.packages("tmap")If already installed it would probably update to latest version or simply run the function from the library.
library(tmap)Will demonstrate some of the tmap function features using World dataset which includes variables about countries in the world. The dataset is used to demonstrate application of R to political/social and economic analysis.
data("World")The “World”-DataFrame basically includes information on the population variables in the world.
data.frame(World)We use tm_polygons() to fill the polygons and draws the polygon borders. The tm_shape() directs to the data in use.
“Pop density” = “pop_est_dens”
tm_shape(World) +
tm_polygons("pop_est_dens")Notably has created a choropleth map. Choropleth maps use color to show how data changes from place to place. These maps allow us to visualize information tied to geography, and to compare and analyze data from across locations. What is Choropleth maps.
We could possibly have better visualization if we modify the map by adding breaks to control the polygons using a list of values.
tm_shape(World) +
tm_fill("pop_est_dens",
breaks = c(0, 30, 60, 90, 120, 500, 1000, Inf))
Can also add the borders and title legend to the map.
tm_shape(World) +
tm_fill("pop_est_dens",
title = "World density map",
breaks = c(0, 30, 60, 90, 120, 500, 1000, Inf)) +
tm_borders()
The world dataframe is a a SpatialPolygonsDataFrame that includes
information on all populations of the world, so we can simply draw a map
that contains “The countries of the world by happiness index” Spatial
Polygons
tm_shape(World)+tm_polygons("HPI",palette="-Blues",
contrast=.7, id="name",
title="Happiness Index")You could set this map into a dynamic mode by running the tmap_mode(“view”) function. (Run chunk for a dynamic interactive experience)
tmap_mode("view")The map goes into an interactive and dynamic mode with the labels and pop ups allowing us to make some custom pop ups. Map Viewer
tm_shape(World) +
tm_fill("pop_est_dens",
title = "World density map",
breaks = c(0, 30, 60, 90, 120, 500, 1000, Inf)) +
tm_borders()Using a Netherlands municipal area data - data(NLD_muni) We could also transform the outlook from polygons to cartogram with the the tm_bubbles() function and use the tmap_arrange() funtion to display both images in the same visual diagram as below.
tmap_mode("plot")
## tmap mode set to plotting
data(NLD_muni)
tm1 <- tm_shape(NLD_muni) + tm_polygons("population", convert2density = TRUE)
tm2 <- tm_shape(NLD_muni) + tm_bubbles(size = "population")
tmap_arrange(tm1, tm2)We can also plot multiple shapes that is a spatial objects (with a class from sf, sp, stars, or raster) and multiple layers.
data(World, metro, rivers, land)
tmap_mode("plot") ## tmap mode set to plotting
tm_shape(land) + tm_raster("elevation", palette = terrain.colors(8)) +
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 = TRUE)
# Conclusion
The tmap function has many simple and easy features to make data visualizations clear and easy to comprehend and analyse. The tMap component is multipurpose and very flexible and because of this, there is often the temptation to do as much as possible in a single tMap component. This isn’t recommended since this can raise the complexity to a level where the code becomes difficult to understand and to maintain. It is recommended that multiple tMap components be used to manage complex transformations so that the code is more easily understood.
The tmap_tip() function generates a tip with an example. The tip and example code are printed, and the example itself is executed.
Learn more about [package, technique, dataset] with the following:
Resource I tmap:get started
Resource II tmap in a nutshell
Resource III tmap: thematic maps in R
This code through references and cites the following sources:
Hadley Wickham.A layered grammar of graphics.
Eoin Horgan (2020). Source III. Plotting choropleth maps in R with tmap
Population EducationWhat is Choropleth maps
Michael T. Hallworth, Ph.D.Spatial Polygons
Geocomputation with R Making maps with R
ArcGIS Online Map Viewer