A cartogram is a map in which the geometry of regions is distorted in order to convey the information of an alternate variable. In this example, we are going to draw a map of Africa where the size of each country is distorted proportionally to its population.
First of all, you need to understand what a geospatial object is, and how to plot it with R. See the background map section of the gallery!
We can plot the boundaries using the plot() function:
# Get the shape file of Africa
# We can visualize the region's boundaries with the plot function
The geospatial object has a ‘data slot‘: an attached data frame that provides several information for each region. It notably gives the population of each country in 2005. We can thus use the cartogram library to distort the size of each country, proportionally to this column. The new geospatial object we get can be drawn with the same plot function!
See how Nigeria is now bigger?
# We work with the cartogram library
# construct a cartogram using the population in 2005
afr_cartogram <- cartogram(afr, "POP2005", itermax=5)
# This is a new geospatial object, we can visualise it!
Of course, it is possible to custom the representation. Since we have a spatial object, all the techniques described in the map, chloropleth and hexbin sections will work here to. Let’s use ggplot2 to add some color, title, legend, background … We now have a nice cartogram chloropleth map of Africa!
# It is a new geospatial object: we can use all the usual techniques on it! Let's start with a basic ggplot2 chloropleth map:
spdf_fortified <- tidy(afr_cartogram)
spdf_fortified = spdf_fortified %>% left_join(. , afr_cartogram@data, by=c("id"="ISO3"))
geom_polygon(data = spdf_fortified, aes(fill = POP2005, x = long, y = lat, group = group) , size=0, alpha=0.9) +
# As seen before, we can do better with a bit of customization
geom_polygon(data = spdf_fortified, aes(fill = POP2005/1000000, x = long, y = lat, group = group) , size=0, alpha=0.9) +
scale_fill_viridis(name="Population (M)", breaks=c(1,50,100, 140), guide = guide_legend( keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"), label.position = "bottom", title.position = 'top', nrow=1)) +
labs( title = "Africa 2005 Population" ) +
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#f5f5f4", color = NA),
panel.background = element_rect(fill = "#f5f5f4", color = NA),
legend.background = element_rect(fill = "#f5f5f4", color = NA),
plot.title = element_text(size= 22, hjust=0.5, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
legend.position = c(0.2, 0.26)
Make a search