top of page

Combining Choropleth and Barplots in R

a person's hand holding laptop screen

Overview

A choropleth map is a type of thematic map where areas - like countries, states, or districts - are shaded or colored in proportion to a statistical variable. It’s one of the most intuitive ways to visualize how a value (like population density, income, or literacy rate) varies across a geographic region.

Bar plots (also known as bar charts) on the other hand are graphical representations used to display categorical data with rectangular bars. The lengths or heights of these bars are proportional to the values they represent, making it easy to compare different categories visually.

Combining choropleth maps with bar plots in data visualization is like pairing a globe with a microscope - you get the big picture and the fine details in one view. Choropleth map shows how a variable (like income) varies across geographic regions – while the bar plot provides precise side-by-side comparison of those values.

Example of a Combined Choropleth & Barplot

a snapshot of choropleth map and barplot combined
Replicate the visualization: Link to code

Step-by-Step Guide

Load the required packages

Load libraries
library(tidyverse)
library(sf)
library(paletteer)
library(patchwork)

Load dataset

Load data
survey_sample<-read.csv("https://raw.githubusercontent.com/bernardkilonzo-rigor/dataviz/refs/heads/main/data/Survey%20Sample.csv")
adm2_shapefiles<-st_read("hti_boundaries_communes_adm2_cnigs_polygon.shp")#download the files and read them from your computer
access shape file data ("https://github.com/bernardkilonzo-rigor/geoinsights/tree/main/data")

Download the shapefile data from this link

Group the sample sizes into bins.

grouping the sample size into bins
survey_sample<-survey_sample%>%mutate(bins = case_when(
  Sample.Size<=150 ~"100-150",
  Sample.Size<=200 ~"151-200",
  Sample.Size<=250 ~"201-250",
  Sample.Size<=300 ~"251-300",
  Sample.Size<=350 ~"301-350",
  Sample.Size<=400 ~"351-400",
  Sample.Size<=450 ~"401-450"
))

Join the shape files data with the survey sample data.

joining the two data sets
merged_data<-adm2_shapefiles%>%left_join(
  survey_sample, by =c("commune"="Commune"))

Create a bar chart using the code below.

creating a bar plot
bplot<-survey_sample%>%group_by(bins)%>%
  summarise(sample =n_distinct(Id.Com))%>%
  ggplot(aes(y = bins, x = sample, fill =bins))+
  geom_bar(stat = "identity", width = 0.5)+
  geom_text(aes(label = sample),hjust = -0.2, size =2.5,family ="serif",color = "gray25")+
  coord_cartesian(xlim = c(0,40))+
  scale_fill_paletteer_d("rcartocolor::BurgYl")+
  labs(title = "Number of Communes by
       Sample Size Bin")+
  theme(panel.background = element_blank(),
        axis.title = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_text(family = "serif",size = 6, color = "gray25"),
        legend.position = "none",
        plot.title = element_text(family = "serif",face = "bold",size = 8, color = "gray25"))

Executing the above code creates the view below.

a snapshot of barplot

Create a choropleth map using the code below.

creating a choropleth map
map<-merged_data%>%ggplot(aes(geometry=geometry, label = commune,
                         fill = bins))+
  geom_sf(color ="white", linewidth = 0.1)+
  scale_fill_paletteer_d("rcartocolor::BurgYl")+
  labs(title = "Survey Sample Size by Commune in Haiti",
       caption = "Viz By: Bernard Kilonzo",
       fill = "Sample Size")+
  theme(panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        legend.position = "none",
        plot.title = element_text(family = "serif", face = "bold",size = 14, color = "gray25"),
        plot.caption = element_text(family = "serif", face = "italic",color = "gray35",size = 8),
        legend.title = element_text(family = "serif", size = 8),
        legend.text = element_text(family = "serif", size = 8))

Executing the above code creates the view below.

example of a choropleth map developed in r

Join the two plots using the code below.

joining the two plots
merged_plot<-map+inset_element(bplot, left = 0.0,bottom = 0.35,right = 0.3,top = 0.78)

See the resulting view

a choropleth map combined with a barplot

Conclusion

By pairing a choropleth map with a bar plot, you bridge the gap between geographic distribution and numerical comparison. The map provides a spatial overview - highlighting regional patterns and clusters - while the bar plot delivers precise, ranked values that are easy to compare side by side. This duality allows viewers to see where something is happening and understand how much it’s happening.

If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!

Thank you for reading!

Tags:

 
 
Original.png

We Support You Deliver Business-Focused Solutions That Enable Data-Driven Decision Making.

  • Tableau profile
  • YouTube
  • White LinkedIn Icon
  • Facebook
  • X

QUICK LINKS

CONTACT US

WhatsApp: +254 738 307 495

East Gate Mall, Donholm

3rd Floor Suite No. 3i

Nairobi, Kenya

Join our mailing list

bottom of page