Setup
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.0.6 v dplyr 1.0.4
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(socviz)
library(tidycensus)
library(devtools)
## Loading required package: usethis
library(urbnmapr)
library(maps)
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
library(ggthemes)
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(tmap)
library(cartogram)
Select a state other than WA and an ACS variable of interest.
OR_wide <- get_acs(geography = "county",
state = "OR",
variables = "B01001_001",
output = "wide",
geometry = TRUE)
## Getting data from the 2015-2019 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
OR_wide$NAME = gsub(" County, Oregon","",OR_wide$NAME)
OR_wide <- rename(OR_wide, 'population' = 'B01001_001E')
head(OR_wide)
Create a choropleth of the state’s counties showing the geographic distribution of the variable.
qtm(OR_wide, fill="population") +
tm_text("NAME",size = .4)

Create two types of cartograms as an alternative to the choropleth.
OR_proj = st_transform(OR_wide,crs = 3857)
OR_carto = cartogram_cont(OR_proj,"population",itermax = 5)
## Mean size error for iteration 1: 8.25121352007753
## Mean size error for iteration 2: 7.1217208370636
## Mean size error for iteration 3: 6.12943164451518
## Mean size error for iteration 4: 5.22484130036291
## Mean size error for iteration 5: 4.3995905829991
tm_shape(OR_carto) +
tm_polygons(col = "population", n = 6) +
tm_text("NAME",size = .4)

OR_carto_dorling = cartogram_dorling(OR_carto, "population",itermax = 5)
tm_shape(OR_carto_dorling) +
tm_fill(col = "population") +
tm_text("NAME",size = .4)
