Thematic Maps
Thematic Maps
Thematic Maps
By Ronaldo Menezes
2024
Sumário
Introduction to tmap
3
Explanation of Functions
4
Interactive Maps with Advanced
5
Controls
In this introduction to tmap, we will explore the fundamental concepts for creating
thematic maps, demonstrating how this package can be used to represent spatial
variables in different contexts. Whether for urban analysis, environmental studies, or
visualizing socioeconomic indicators, tmap provides a flexible and efficient approach to
transforming complex data into accessible visualizations. Throughout this guide, we will
see how to configure, customize, and save maps, making the most of the capabilities of
this powerful geospatial visualization package.
Introduction to tmap
The tmap package is a powerful spatial data visualization tool in R, offering an intuitive
interface for creating thematic maps. It is particularly useful for data scientists,
geographers, urban planners, and anyone who needs to visualize geospatial data clearly
and effectively. tmap allows the creation of both static and interactive maps, making it a
versatile choice for various types of analysis.
Installation and Setup Before you start using tmap, you need to install it (if you haven’t
already) and load it into your workspace:
install.packages("tmap")
library(tmap)
Additionally, tmap works well with other spatial data manipulation packages like sf and
sp, which can also be installed and loaded:
install.packages("sf")
library(sf)
Basic Structure of a Map in tmap Creating maps in tmap follows a modular structure,
where each map component (data, fill, borders, labels, etc.) is added as a separate layer.
This provides flexibility to customize every aspect of the map.
3
Explanation of Functions:
tm_shape(): Defines the spatial data layer to be used in the map. This is the starting
point for creating any map in tmap.
tm_fill(): Fills polygons with colors based on a specific variable in the spatial data. You
can choose the color palette, define a title for the legend, and adjust other
parameters.
tm_borders(): Adds borders to polygons, making it easier to visualize the divisions
between different areas on the map.
Visualization Modes: Static vs. Interactive tmap allows switching between static and
interactive visualization modes:
# Interactive mode
tmap_mode("view")
In interactive mode, the map behaves similarly to one generated with the leaflet package,
allowing zoom, pan, and clicks on map elements to get more information.
Advanced Map Customization tmap offers several options to customize maps
according to your needs:
Custom Color Palettes:
The classification style can be adjusted to different methods, such as "quantile," "jenks,"
"pretty," among others.
Adding Data Layers: You can add multiple layers to the same map. For example, to add
points representing cities on top of a choropleth map:
tm_shape(nc) +
tm_polygons("BIR74", palette = "Greens") +
tm_shape(cities) +
tm_dots(size = 0.1, col = "red", title = "Cities")
Custom Labeling: Adding custom labels can help highlight important information:
tm_shape(nc) +
tm_polygons("BIR74") +
tm_text("NAME", size = 0.5, col = "black", shadow = TRUE)
4
Interactive Maps with Advanced Controls When in interactive mode, you can add
advanced controls to the map:
tm_shape(nc) +
tm_polygons("BIR74", palette = "BuPu") +
tm_basemap("OpenStreetMap") +
tm_view(set.view = c(-80, 35, 7))
Faceting and Multivariate Maps tmap supports faceting, allowing the comparison of
several variables or groups in a single layout:
tm_shape(nc) +
tm_polygons(c("BIR74", "SID74"), palette = "Blues") +
tm_facets(sync = TRUE, nrow = 1)
Interactive HTML:
Temporal Animations and Dynamic Maps For temporal data, tmap can create
animations that show the evolution of a variable over time:
Integration with External Data tmap can integrate with external spatial data such as
shapefiles, GeoJSON, or PostGIS databases, allowing direct import and visualization:
nc <- st_read("path/to/your/shapefile.shp")
3D Maps Although tmap is primarily 2D, it supports export to other tools that can visualize
in 3D, such as QGIS.
5
Example:
Dataset: D:/BGRI11_CONT/BGRI2021/BGRI2021_1105.gpkg
Variables:
N_EDIFICIOS_CLASSICOS
N_EDIFICIOS_CLASS_CONST_1OU2 ALOJ
N_EDIFICIOS_CLASS_CONST_3OUMAIS ALOJAMENTOS
N_EDIFICIOS_EXCLUSIV_RESID
N_EDIFICIOS_1OU2_PISOS
N_EDIFICIOS_3 MAIS_PISOS
N_EDIFICIOS_CONSTR_ANTES 1945
N_EDIFICIOS_CONSTR_1946A1980
N_EDIFICIOS_CONSTR_1981A2000
N_EDIFICIOS_CONSTR_2001_2010
N_EDIFICIOS_CONSTR_2011_2021
N_EDIFICIOS_COM NECESSIDADES REPARAÇÃO
N_ALOJAMENTOS_TOTAL
N_ALOJAMENTOS_FAMILIARES
N_ALOJAMENTOS_FAM_CLASS_RHABITUAL
N_ALOJAMENTOS_FAM_CLASS_VAGOS OU RESID SECUNDARIA
N_RHABITUAL_ACESSIVEL CADEIRAS RODAS
N_RHABITUAL_COM ESTACIONAMENTO
N_RHABITUAL_PROP_OCUP
N_RHABITUAL_ARRENDADOS
N_AGREGADOS_DOMESTICOS_PRIVADOS
N_ADP_1OU2_PESSOAS
N_ADP_3OU_MAIS_PESSOAS
N_NUCLEOS_FAMILIARES
N_NUCLEOS_FAMILIARES_COM_FILHOS
N_INDIVIDUOS
N_INDIVIDUOS_H
N_INDIVIDUOS_M
N_INDIVIDUOS_0A14
N_INDIVIDUOS_15A24
N_INDIVIDUOS_25A64
N_INDIVIDUOS_65_OU_MAIS
6
Let's create an example using the tmap package to visualize the data from your
BGRI2021_1105.gpkg file, which seems to contain detailed information on buildings,
housing, and individuals in a specific region.I'll show you how to load this file and create a
basic thematic map using some of the listed variables.
First, make sure you have the necessary packages installed and loaded:
install.packages("sf")
install.packages("tmap")
library(sf)
library(tmap)
Now, let's load your gpkg file and check the available variables:
str(bgri_data)
Classes 'sf' and 'data.frame': 1979 obs. of 48 variables:
$ OBJECTID : int 29080 29081 29082 29083 29084 29085 31233
31234 31235 31236 ...
$ BGRI2021 : chr "11050800801" "11050802310" "11050602204"
"11050702408" ...
$ DT21 : chr "11" "11" "11" "11" ...
$ DTMN21 : chr "1105" "1105" "1105" "1105" ...
$ DTMNFR21 : chr "110508" "110508" "110506" "110507" ...
$ DTMNFRSEC21 : chr "110508008" "110508023" "110506022"
"110507024" ...
$ SECNUM21 : chr "008" "023" "022" "024" ...
$ SSNUM21 : chr "01" "10" "04" "08" ...
$ SECSSNUM21 : chr "00801" "02310" "02204" "02408" ...
$ SUBSECCAO : chr "11050800801" "11050802310"
"11050602204" "11050702408" ...
$ NUTS1 : chr "1" "1" "1" "1" ...
$ NUTS2 : chr "17" "17" "17" "17" ...
$ NUTS3 : chr "170" "170" "170" "170" ...
$ N_EDIFICIOS_CLASSICOS : num 13 8 28 19 23 10 30 17 11 14 ...
...
$ N_ALOJAMENTOS_TOTAL : num 13 13 33 44 23 17 96 39 40 43
...
...
$ N_INDIVIDUOS_65_OU_MAIS : num 11 8 9 29 13 10 50 18 13 17
...
$ SHAPE_Length : num 318 575 659 490 492 ...
7
$ SHAPE_Area : num 5780 13285 17459 11132 9138 ...
$ geom :sfc_MULTIPOLYGON of length 1979; first list
element:List of 1
..$ :List of 1
.. ..$ : num [1:12, 1:2] -108191 -108114 -108114 -108136 -108150 ...
..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
- attr(*, "sf_column")= chr "geom"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA
NA NA NA NA ...
..- attr(*, "names")= chr [1:47] "OBJECTID" "BGRI2021" "DT21" "DTMN21" ...
8
Interactive Mode
If you want to view the map interactively, where you can zoom in and explore the data,
just switch to the view mode:
tm_shape(bgri_data) +
tm_polygons("N_EDIFICIOS_CLASSICOS",
palette = "Blues",
title = "No. of Classic Buildings") +
tm_polygons("N_ALOJAMENTOS_TOTAL",
palette = "Reds",
9
alpha = 0.5,
title = "Total Housing Units") +
tm_borders() +
tm_layout(title = "Distribution of Buildings and Housing Units")
10
Three more complete examples using the tmap package to create thematic maps
from your gpkg file. Each example uses a different variable and distinct colors for
better visualization.
The dataset you provided contains a rich variety of variables, enabling the creation of a
wide range of thematic maps that can help visualize different demographic, housing, and
structural aspects of a region. The diversity of these variables offers a solid foundation for
detailed geospatial analyses, allowing you to extract valuable insights about the study
area.
Dataset Structure and Potential for Thematic Maps The dataset contains variables
related to different characteristics of buildings, housing, families, and individuals. This
means you can create thematic maps focused on various aspects such as:
Building Distribution:
Variables like N_EDIFICIOS_CLASSICOS, N_EDIFICIOS_CONSTR_ANTES_1945, and
N_EDIFICIOS_CONSTR_2011_2021 allow you to create maps that show the distribution
and concentration of different types of buildings over time. This can be useful for historical
analyses of urban development, heritage conservation, or infrastructure assessment.
Since the dataset contains many interrelated variables, you can create comparative maps
or even maps showing multiple variables in a single visualization. For instance, you could
overlay the distribution of buildings with the distribution of housing units to analyze the
relationship between building types and housing occupancy.
Potential Map Examples:
Historical Building Map: Using the variables related to building construction years,
you can create a series of maps showing urbanization trends over different periods.
This could reveal urban growth patterns or areas that require renovation.
Accessibility Map: With variables like
N_RHABITUAL_ACESSIVEL_CADEIRAS_RODAS, you can map the accessibility of
housing for people with reduced mobility, essential for inclusive planning.
Population Aging Map: Using N_INDIVIDUOS_65_OU_MAIS, you can create a
map that highlights areas with a high concentration of elderly people, which could
influence health and social service policies.
11
The dataset is an extremely versatile tool for creating thematic maps, offering countless
possibilities for analysis and visualization. The richness and variety of variables allow you
to explore different aspects of geospatial reality, from urban development to social and
demographic issues. With the use of tmap in R, you can transform these data into clear
and informative maps that can serve as a basis for decision-making, urban planning,
academic research, and more.
# Load packages
library(sf)
library(tmap)
12
Example 2: Thematic Map of Wheelchair Accessible Housing Units
# Load packages
library(sf)
library(tmap)
The tmap package is an extremely powerful and versatile tool for creating thematic and
interactive maps in R. It combines an easy-to-use interface with advanced customization
capabilities and integration with other spatial analysis packages. Whether for simple or
complex analyses, tmap provides the necessary tools to transform spatial data into
meaningful and impactful visualizations.
With the resources presented above, you should be well-equipped to explore and use
tmap in your spatial analyses, taking full advantage of its capabilities.
13