Creating Interactive Choropleth Maps with tmap in R: A Customized Approach to Visualizing Population Data.

Understanding tmap: A Framework for Creating Choropleth Maps in R

tmap is a popular framework in R for creating choropleth maps, which are geographic maps that display data as colors. This article will delve into the world of tmap and explore how to create a custom choropleth map with proportional symbols.

Introduction to tmap

tmap is built on top of the Leaflet JavaScript library and allows users to create interactive choropleth maps in R. The framework provides a range of tools for customizing the appearance and behavior of the map, including support for different data types, scales, and layouts.

Defining the Map Data

To create a choropleth map using tmap, we first need to prepare our data. In this case, we have a dataframe called TA_population that contains information about the population of each Territorial Authority (TA) in New Zealand.

# Load required libraries
library(tmap)
library(tmaptools)

# Define the map mode
tmap_mode("plot")

# Plotting function
plot_tmap <- function(df) {
  # Define breaks and labels for pop_perc  
  brks1 <- c(0,0.5,1,2,5,10,20,40)
  labs1 <- c("0\u20130.5","0.5\u20131","1\u20132","2\u20135","5\u201310","10\u201320","20\u201340")

  # Map data
  tm_shape(df) +
    tm_polygons(col = "population_perc", 
                palette = "Reds",
                breaks = brks1,
                labs = labs1,
                border.alpha = 0.2,
                n = 7,
                title = "Population Percentage by TA's (%)") + 
  
    # Add bubbles for population
    tm_shape(df) +
      tm_bubbles(size = "population",
                 col = "black", 
                 alpha = 1,
                 title.size = "Population") +
      
    # Add compass and scale bar
    tm_compass(position = c("right", "top"), 
               size = 1.5, 
               show.labels = T,
               color.light = "black") +
      tm_scale_bar(position = c("right", "bottom"), 
                   breaks = c(0,100,200)) +
      
    # Add credits and layout
    tm_credits("Data: Census 2018, (c) StatsNZ", 
              position=c("right", "bottom"), # placement
              size = 0.8) +
      tm_layout(bg.color = "lightcyan",
                frame = T,
                legend.position = c("left","top"), # legend position
                title = "Population Percentages of South Island TA's", 
                title.size = 1, 
                title.fontface = "bold", 
                legend.title.size = 0.8, 
                legend.text.size = 0.6)
}

Adjusting Labels and Breaks for tm_bubbles

The original code provided attempts to adjust the breaks and labels for tm_bubbles but does not succeed. To address this, we need to convert the legend to portrait mode and insert empty intervals between the values.

# Plotting function with adjusted bubbles
plot_tmap <- function(df) {
  # Define breaks and labels for pop_perc  
  brks1 <- c(0,0.5,1,2,5,10,20,40)
  labs1 <- c("0\u20130.5","0.5\u20131","1\u20132","2\u20135","5\u201310","10\u201320","20\u201340")

  # Map data
  tm_shape(df) +
    tm_polygons(col = "population_perc", 
                palette = "Reds",
                breaks = brks1,
                labs = labs1,
                border.alpha = 0.2,
                n = 7,
                title = "Population Percentage by TA's (%)") + 
  
    # Add bubbles for population
    tm_shape(df) +
      tm_bubbles(size = "population",
                 col = "black", 
                 alpha = 1,
                 title.size = "Population", 

                 ### this specifies the legend to be in portrait mode
                 legend.size.is.portrait=TRUE, 

                 ### this specifies the breaks - bubbles of size 0 
                 ### will be plotted but won't be noticeable due to their size
                     sizes.legend=c(25000,0,100000,0,400000), 

                 ### here you label the breaks, with zeroes labeled as empty strings
                     sizes.legend.labels=c("25k","","100k","","400k")
) +
  # ... (rest of the code remains the same)
}

Conclusion

In this article, we explored how to create a custom choropleth map using tmap in R. We defined the map data and used the tm_shape function to plot polygons with colored fill values. We then added bubbles for population using tm_bubbles and adjusted the breaks and labels to avoid overlapping. Finally, we demonstrated how to convert the legend to portrait mode and insert empty intervals between the values.

References


Last modified on 2025-02-04