Advertisement
binkleym

R code to render cartogram bug example

Jan 20th, 2020
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.27 KB | None | 0 0
  1. ### We need the following packages for this example.
  2. packages <- c("tidyverse", "lubridate", "ggplot2", "maps", "sf",
  3.               "ggthemes", "tsibble", "dplyr", "broom", "ggfortify",
  4.               "usmap", "forecast", "gridExtra", "cartogram", "RColorBrewer")
  5.  
  6. ### Install packages if needed, then load them quietly
  7. new_packages <- packages[!(packages %in% installed.packages()[, "Package"])]
  8. if (length(new_packages)) install.packages(new_packages, quiet = TRUE)
  9. invisible(lapply(packages, "library", quietly = TRUE,
  10.                  character.only = TRUE, warn.conflicts = FALSE))
  11.  
  12. # Load data for the graphs from a spreadsheet file.
  13. # The Pastebin URL for this data file is:
  14. # https://pastebin.com/a5N4JAwL
  15. state_data <- read_csv("cartogram-bug-data.csv",
  16.                        col_names = TRUE,
  17.                        col_types = "ccnnni")
  18.  
  19. ### Load a map of states in the USA.   The maps have a list of lowercase
  20. ### state names, which will use to match against "pres" down below
  21. us_map <- maps::map("state", plot = FALSE, fill = TRUE)
  22.  
  23. ### Change the latitude/longitude data to a simple feature object
  24. us_map <- sf::st_as_sf(us_map)
  25.  
  26. ### Change the name of the "ID" column to "state_name"
  27. names(us_map) <- c("geometry", "state_name")
  28.  
  29. ### Remove the District of Colombia from our map
  30. us_map <- us_map %>% filter(state_name != "district of columbia")
  31.  
  32. ### Join the state data to the map
  33. us_map <- us_map %>% left_join(state_data, by = "state_name")
  34.  
  35. ### Population-weighted cartogram using 6 iterations...
  36. us_map_cartogram_6 <- us_map %>%
  37.                       st_transform(crs = 2163) %>%
  38.                       cartogram_cont("pop2020", itermax = 6) #%>%
  39.  
  40. ### Population-weighted cartogram using 7 iterations...
  41. us_map_cartogram_7 <- us_map %>%
  42.                       st_transform(crs = 2163) %>%
  43.                       cartogram_cont("pop2020", itermax = 7) #%>%
  44.  
  45. p_unemployment <- ggplot(us_map, aes(fill = unemployment), col = "black") +
  46.      theme_void() +
  47.      labs(title = "Unemployment rate by state (SA)") +
  48.      geom_sf() +
  49.      coord_sf(crs = "+proj=aea +lat_1=25 +lat_2=50 +lon_0=-100", ndiscr = 0) +
  50.      scale_fill_gradientn(name = "Unemployment (%)",
  51.                            colours = rev(brewer.pal(9, "Blues")),
  52.                            trans = "log10")
  53.  
  54. p_unemployment_cartogram_6 <- ggplot(us_map_cartogram_6, aes(fill = unemployment), col = "black") +
  55.      theme_void() +
  56.      labs(title = "Cartogram with 6 iterations") +
  57.      geom_sf() +
  58.      coord_sf(crs = "+proj=aea +lat_1=25 +lat_2=50 +lon_0=-100", ndiscr = 0) +
  59.      scale_fill_gradientn(name = "Unemployment (%)",
  60.                           colours = rev(brewer.pal(9, "Blues")),
  61.                           trans = "log10")
  62.  
  63. p_unemployment_cartogram_7 <- ggplot(us_map_cartogram_7, aes(fill = unemployment), col = "black") +
  64.      theme_void() +
  65.      labs(title = "Cartogram with 7 iterations") +
  66.      geom_sf() +
  67.      coord_sf(crs = "+proj=aea +lat_1=25 +lat_2=50 +lon_0=-100", ndiscr = 0) +
  68.      scale_fill_gradientn(name = "Unemployment (%)",
  69.                           colours = rev(brewer.pal(9, "Blues")),
  70.                           trans = "log10")
  71.  
  72. grid.arrange(p_unemployment,
  73.              p_unemployment_cartogram_6,
  74.              p_unemployment_cartogram_7,
  75.              nrow = 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement