BivariateTMap
A simple function to create bivariate Choropleth maps in R tmap
Install / Use
/learn @stefdesabbata/BivariateTMapREADME
Bivariate tmap
The bivariate_tmap.R script defines a simple function named bivariate_choropleth which uses the tmap library to create a bivariate choropleth map (using a color scheme fro Joshua Stevens's fantastic blog post) and plot it including a legend created using the grid library.
The bivariate_choropleth function parameters are:
- bivmap_dataset, a SpatialPoligonDataFrame;
- bivmap_vars, a vector of characters containing the name of the two variables;
- bivmap_labels, a vector of characters containing the labels for the two variables, to use in the legend (default is NA, in; which case, values from bivmap_vars are used);
- bivmap_style, classification type for the bins (default is 'quantile');
- bivmap_scale, whether to use a scale bar (default is FALSE).
The bivariate_choropleth function uses a function named get_bivariate_choropleth which simply creates the map (without the legend) and returns the related object. This function can also be used directly for further manipulation of the map.
Requirements
The function was developed using R version 3.5.1, and it requires the following R libraries:
Example
Example script.
# Import function
source("bivariate_tmap.R")
# Import example data
library(tmap)
data("World")
# Transform data to SpatialPolygonDataFrame
library(sf)
world_spdf <- as(World, "Spatial")
# Plot bivariate choroplet map
bivariate_choropleth(world_spdf, c("gdp_cap_est", "inequality"))
Console output (categories).
gdp_cap_est breaks (x-axis):
style: quantile
[300.4693,3356.704) [3356.704,13587.93) [13587.93,2e+05]
59 58 59
inequality breaks (y-axis):
style: quantile
one of 9,045 possible partitions of this variable into 3 classes
[0.0432207,0.1672642) [0.1672642,0.2736572) [0.2736572,0.5073423]
45 45 46
Output map.

Acknowledments
I would like to acknowledge the contribution by Andrea Ballatore, who collaborated on the paper for which I originally wrote this script, helped me testing it, and encouraged me to publish it.
Related Skills
node-connect
350.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
350.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
350.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
