Thematic Maps
Introduction
Data visualization is important in analyzing large amounts of information which makes it easier in delivering ideas to the audience and taking decisions. When talking about analysis, it is a must to mention analysis of geographical data as known as geospatial analysis. In this sense, we can talk about different sorts of maps and several helpful packages.
This code through explores how thematic maps are illustrated using a package named tmap.
Thematic maps
A thematic map is a statistical map that focuses on the spatial variation of a particular distribution, topic or theme. The theme refers to the phenomena that is shown, which is often demographical, social, cultural, or economic. There are several types of thematic maps such as heat maps, cartogram, choropleths, and others.
Thematic maps are created by stacking layers, where per layer, data can be mapped to one or more aesthetics. It is also possible to generate small multiples.
Types
1.Choropleth Map
It is used to represent statistical values in different geographical scale, from global to local. It mainly display densities (ratios) of quantities using color.
2.Dot Distribution Map
It uses dots (variation of marks) to display the presence or absence of a feature.
3.Heat Maps
Heat maps display the density of points on a geographic map and can effectively visualise the intensity of the variable through a colour scale.
4.Cartogram
A cartogram is a thematic map type in which the size of an area is re-scaled to be proportional to the feature it represents.
Hello World?
Did you know that you can view the whole world using tmaps?
Installation
The “World” DataFrame contains information about the population and the economy of countries all over the globe.
tmap is born
In order to create the map, tm_shape does the work. It accepts spatial data as an argument and in this case “World” is a SpatialPolygonsDataFrame.
However, in order to represent the data, we can use tm_polygons. The below countries are filtered by happiness index.
tm_shape(World)+tm_polygons("HPI", palette=c("gray95", "gold"), contrast=.7, id="name", title="Happiness Index")## [1] "sf" "data.frame"
Static vs Interactive
Every map can be either plotted as static image using plot or presented interactively using view modes. The mode can be changed using tmap_mode
Mix & Match
With tmap, we can combine different data together to the above map. What if we want to view the population of the metropolitan regions in the world, for example in Armenia?
We are presenting the information using bubbles tm_bubbles instead of polygons.
data (metro)
metro$growth <- (metro$pop2020 - metro$pop2010) / (metro$pop2010 * 10) * 100
tm_shape(metro) +
tm_bubbles("pop2010", col = "growth",
breaks=c(-Inf, seq(0, 6, by=2), Inf),
palette="-RdYlBu", contrast=1,
title.size="Metro population",
title.col="Growth rate (%)", id="name") +
tm_format("World_wide")Now, let’s join the above maps together!
data(World, metro)
metro$growth <- (metro$pop2020 - metro$pop2010) / (metro$pop2010 * 10) * 100
mapWorld <- tm_shape(World) +
tm_polygons("income_grp", palette="-Blues", contrast=.7, id="name", title="Income group") +
tm_shape(metro) +
tm_bubbles("pop2010", col = "growth",
border.col = "black", border.alpha = .5,
style="fixed", breaks=c(-Inf, seq(0, 6, by=2), Inf),
palette="-RdYlBu", contrast=1,
title.size="Metro population",
title.col="Growth rate (%)", id="name") +
tm_style("grey") + tm_format("World_wide")
mapWorldLet’s Build layers!
A tile layer is a way to visualize large data sets on the map.The function tm_basemap draws the tile layer as basemap (as bottom layer), whereas tm_tiles draws the tile layer as overlay layer. The stacking order corresponds to the order in which this layer is called.
The base map is “Stamen.Watercolor” and the overlay layer is the one having metro data. We can manipulate what layer we want to view by clicking on the icon on the map.
Multiples
We can create small multiples of thematic maps using function tm_facets. This can be done in 3 different ways:
First by assigning multiple variable names to one aesthetic.
tmap_mode("view")
tm_shape(World) +
tm_polygons(c("HPI", "economy")) +
tm_facets(sync = TRUE, ncol = 2)Second by splitting the spatial data with the by argument.
tmap_mode("plot")
## tmap mode set to plotting
data(NLD_muni)
NLD_muni$perc_men <- NLD_muni$pop_men / NLD_muni$population * 100
tm_shape(NLD_muni) +
tm_polygons(col = "MAP_COLORS") +
tm_facets(by = "province")Third by using the tmap_arrange function:
The NLD_muni is the maps of Netherlands municipality level of 2013.
tmap_mode("plot")
## tmap mode set to plotting
data(NLD_muni)
tm1 <- tm_shape(NLD_muni) + tm_polygons("population", convert2density = TRUE, palette = "deepskyblue" )
tm2 <- tm_shape(NLD_muni) + tm_bubbles(size = "population")
tmap_arrange(tm1, tm2)Quick Trick
We can create a thematic map with one function which is the quick thematic map (qtm).
The data frame used is rivers which shows 141 major rivers in North America, as compiled by the US Geological Survey.
And more.. World dataframe
Map in Style
To switch styles for thematic maps, we can use tmap_style. Options include “white”, “gray”, “natural”, “classic”, “col_blind”, “albatross”, “beaver”, “bw”, and “watercolor”.
tmap_style("cobalt")
## tmap style set to "classic"
## other available styles are: "white", "gray", "natural", "cobalt", "col_blind", "albatross", "beaver", "bw", "watercolor"
qtm(World, fill = "life_exp")Focus
What if we want to focus more or less on specific locations/features on the map? In this case, we use the function filter in which we can choose what we want to color & what we don’t. The below example focuses on coloring the continent Africa.
tmap_style("gray")
tm_shape(World, filter = World$continent=="Africa") +
tm_polygons("HPI", id = "name", palette = "-viridis")
Further Resources
Resource I Spatial Data and Mapping
Resource II Basics of Handling Spatial Data in R
Resource III tmap in a nutshell
Works Cited
This code through references and cites the following sources:
Source I. Making Maps in R
Source II. Thematic Maps
Source III. Getting Started
Source IV. Introduction to tmap Package
Source V. Functions
Source VI. The 7 Best Thematic Map Types for Geospatial Data