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.
options(scipen =999) # high 'penalty' for scientific notation
18.3 Get Map Data From rnaturalearth
Show the code
mapdata <-ne_countries(scale ="medium", # medium scalereturnclass ="sf") # as sf object
18.4 Make A Basic Map
We make a basic map, reading it into an object called mymap. We then replaymymap.
Show the code
mymap <-ggplot(mapdata) +# the data I am mappinggeom_sf() # the geometry I am usingmymap # 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.
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 stepmapdata_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 estimatetheme_void()
18.5.6 Cartogram With Better (viridis) Colors
Show the code
ggplot(mapdata_cartogram) +geom_sf(aes(fill = pop_est)) +# fill is population estimatescale_fill_viridis_c(name ="population",option ="viridis") +# beautiful colorstheme_void()