Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### We need the following packages for this example.
- packages <- c("tidyverse", "lubridate", "ggplot2", "maps", "sf",
- "ggthemes", "tsibble", "dplyr", "broom", "ggfortify",
- "usmap", "forecast", "gridExtra", "cartogram", "RColorBrewer")
- ### Install packages if needed, then load them quietly
- new_packages <- packages[!(packages %in% installed.packages()[, "Package"])]
- if (length(new_packages)) install.packages(new_packages, quiet = TRUE)
- invisible(lapply(packages, "library", quietly = TRUE,
- character.only = TRUE, warn.conflicts = FALSE))
- # Load data for the graphs from a spreadsheet file.
- # The Pastebin URL for this data file is:
- # https://pastebin.com/a5N4JAwL
- state_data <- read_csv("cartogram-bug-data.csv",
- col_names = TRUE,
- col_types = "ccnnni")
- ### Load a map of states in the USA. The maps have a list of lowercase
- ### state names, which will use to match against "pres" down below
- us_map <- maps::map("state", plot = FALSE, fill = TRUE)
- ### Change the latitude/longitude data to a simple feature object
- us_map <- sf::st_as_sf(us_map)
- ### Change the name of the "ID" column to "state_name"
- names(us_map) <- c("geometry", "state_name")
- ### Remove the District of Colombia from our map
- us_map <- us_map %>% filter(state_name != "district of columbia")
- ### Join the state data to the map
- us_map <- us_map %>% left_join(state_data, by = "state_name")
- ### Population-weighted cartogram using 6 iterations...
- us_map_cartogram_6 <- us_map %>%
- st_transform(crs = 2163) %>%
- cartogram_cont("pop2020", itermax = 6) #%>%
- ### Population-weighted cartogram using 7 iterations...
- us_map_cartogram_7 <- us_map %>%
- st_transform(crs = 2163) %>%
- cartogram_cont("pop2020", itermax = 7) #%>%
- p_unemployment <- ggplot(us_map, aes(fill = unemployment), col = "black") +
- theme_void() +
- labs(title = "Unemployment rate by state (SA)") +
- geom_sf() +
- coord_sf(crs = "+proj=aea +lat_1=25 +lat_2=50 +lon_0=-100", ndiscr = 0) +
- scale_fill_gradientn(name = "Unemployment (%)",
- colours = rev(brewer.pal(9, "Blues")),
- trans = "log10")
- p_unemployment_cartogram_6 <- ggplot(us_map_cartogram_6, aes(fill = unemployment), col = "black") +
- theme_void() +
- labs(title = "Cartogram with 6 iterations") +
- geom_sf() +
- coord_sf(crs = "+proj=aea +lat_1=25 +lat_2=50 +lon_0=-100", ndiscr = 0) +
- scale_fill_gradientn(name = "Unemployment (%)",
- colours = rev(brewer.pal(9, "Blues")),
- trans = "log10")
- p_unemployment_cartogram_7 <- ggplot(us_map_cartogram_7, aes(fill = unemployment), col = "black") +
- theme_void() +
- labs(title = "Cartogram with 7 iterations") +
- geom_sf() +
- coord_sf(crs = "+proj=aea +lat_1=25 +lat_2=50 +lon_0=-100", ndiscr = 0) +
- scale_fill_gradientn(name = "Unemployment (%)",
- colours = rev(brewer.pal(9, "Blues")),
- trans = "log10")
- grid.arrange(p_unemployment,
- p_unemployment_cartogram_6,
- p_unemployment_cartogram_7,
- nrow = 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement