18  cartogram

A cartogram is a map where the areas of different regions are distorted (increased in size; decreased in size) by the value of some quantitative variable.

18.1 Call Libraries

Show the code
library(rnaturalearth) # natural earth data

library(ggplot2) # beautiful maps

library(dplyr) # data wrangling

library(sf) # simple (spatial) features

library(cartogram) # cartograms!

18.2 Remove Scientific Notation

Show the code
options(scipen = 999) # high 'penalty' for scientific notation

18.3 Get Map Data From rnaturalearth

Show the code
mapdata <- ne_countries(scale = "medium", # medium scale
                        returnclass = "sf")  # as sf object

18.4 Make A Basic Map

We make a basic map, reading it into an object called mymap. We then replay mymap.

Show the code
mymap <- ggplot(mapdata) + # the data I am mapping
  geom_sf() # the geometry I am using

mymap # replay my map

18.5 Make And Plot The Cartogram

18.5.1 Project The Cartogram Data

Tip

cartogram requires projected data (Chapter 5), so we need to project the data with st_transform. A number of projections, including the Mercator and Mollweide projections are possibilities. You may need to experiment with a number of projections to see which ones work best in any particular cartogram.

Show the code
mapdata_proj <- st_transform(mapdata,
                             3857) # Mercator

# mapdata_proj <- st_transform(mapdata, 
#                              crs = "+proj=moll") # Mollweide

18.5.2 Plot The Projected Data

Show the code
ggplot(mapdata_proj) + 
  geom_sf() # plot projected data

Why Does Antarctica Look So Strange? How To Fix This?

In some projections, especially the Mercator projection, Antarctica looks strange.

The key is to run this dplyr code to remove Antarctica.

Show the code
mapdata_proj <- mapdata_proj %>% 
  dplyr::filter(! continent == "Antarctica")

ggplot(mapdata_proj) + 
  geom_sf() # plot projected data

18.5.3 Make The Cartogram Data

Tip

Each iteration takes a LONG time. Fewer iterations help the time, but each iteration contributes to the distortion, and makes a more cartogram-like cartogram. Because this is the most time intensive step, I time the creation of the cartogram with Sys.time.

Show the code
start_time <- Sys.time() # time this step

mapdata_cartogram <- cartogram_cont(mapdata_proj, 
                                  "pop_est", 
                                  itermax = 7)
Warning in cartogram_cont.sf(mapdata_proj, "pop_est", itermax = 7): NA not
allowed in weight vector. Features will be removed from Shape.
Show the code
end_time <- Sys.time()

end_time - start_time
Time difference of 1.750577 mins

18.5.4 Basic Cartogram

Show the code
ggplot(mapdata_cartogram) + 
  geom_sf()

18.5.5 Basic Cartogram With fill Color

Show the code
ggplot(mapdata_cartogram) + 
  geom_sf(aes(fill = pop_est)) + # fill is population estimate
  theme_void()

18.5.6 Cartogram With Better (viridis) Colors

Show the code
ggplot(mapdata_cartogram) + 
  geom_sf(aes(fill = pop_est)) + # fill is population estimate
  scale_fill_viridis_c(name = "population",
                       option = "viridis") + # beautiful colors
  theme_void()