This tutorial explores how different visual variables, color
palettes, classification schemes, layers, and formatting choices change
the way spatial data are displayed in tmap.
The first map uses color to represent county population. Darker counties have larger 2020 populations.
tm_shape(nc_counties) +
tm_polygons(fill = "POP2020") +
tm_title("2020 Population by North Carolina County")Population can also be represented with proportional symbols. Larger symbols represent more people.
tm_shape(nc_counties) +
tm_borders(col = "gray70") +
tm_symbols(size = "POP2020", fill = "steelblue", fill_alpha = 0.65) +
tm_title("County Population Represented by Symbol Size")This version represents the same variable with both size and color. It makes the most populous counties especially easy to identify.
tm_shape(nc_counties) +
tm_borders(col = "gray70") +
tm_symbols(size = "POP2020", fill = "POP2020", fill_alpha = 0.75) +
tm_title("County Population Represented by Size and Color")cols4all::c4a_gui() opens a palette browser in an
interactive R session. It is included below for reference but is not run
while knitting because the pop-up can interrupt the render.
The blue-purple sequential palette is a good match for population because it progresses from lower to higher values.
tm_shape(nc_counties) +
tm_polygons(
fill = "POP2020",
fill.scale = tm_scale_intervals(values = "bu_pu")
) +
tm_title("2020 County Population: Blue-Purple Palette")The histogram shows that county population is strongly right-skewed. Most counties have relatively small populations, while a few counties are much larger.
ggplot(nc_counties, aes(x = POP2020)) +
geom_histogram(bins = 20, fill = "#5B8DB8", color = "white") +
scale_x_continuous(labels = scales::label_comma()) +
labs(
title = "Distribution of 2020 County Population",
x = "Population",
y = "Number of counties"
) +
theme_minimal()Equal intervals place counties into classes with the same numeric width. Quantiles place roughly the same number of counties in each class. The quantile map reveals more variation among the many lower-population counties.
map_equal <- tm_shape(nc_counties) +
tm_polygons(
fill = "POP2020",
fill.scale = tm_scale_intervals(
values = "bu_pu",
style = "equal",
n = 5
)
) +
tm_title("Equal Intervals")
map_quantile <- tm_shape(nc_counties) +
tm_polygons(
fill = "POP2020",
fill.scale = tm_scale_intervals(
values = "bu_pu",
style = "quantile",
n = 5
)
) +
tm_title("Quantiles")
tmap_arrange(map_equal, map_quantile, ncol = 2)The map below can be panned and zoomed, and counties can be selected for more information.