Get the development version of ggplot2

# devtools::install_github("tidyverse/ggplot2")

Get a list of the acs variables.

library(tidycensus)
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats
library(viridis)
## Loading required package: viridisLite
library(stringr)
census_api_key("7491cf955005ef7221778ebe1228115c53a12df6")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
v15 <- load_variables(2015, "acs5", cache = TRUE)
# Plot of race/ethnicity by county in Washington for 2010
library(tidycensus)
library(tidyverse)
library(viridis)
census_api_key("7491cf955005ef7221778ebe1228115c53a12df6")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
vars10 <- c("P0050003", "P0050004", "P0050006", "P0040003")

WA <- get_decennial(geography = "county", variables = vars10, year = 2010,
                    summary_var = "P0010001", state = "WA", geometry = TRUE) %>%
  mutate(pct = 100 * (value / summary_value))

ggplot(WA, aes(fill = pct, color = pct)) +
  geom_sf() +
  facet_wrap(~variable)

str(WA)
## Classes 'tbl_df', 'tbl' and 'data.frame':    156 obs. of  7 variables:
##  $ GEOID        : chr  "53001" "53001" "53001" "53001" ...
##  $ NAME         : chr  "Adams County" "Adams County" "Adams County" "Adams County" ...
##  $ variable     : chr  "P0050003" "P0050004" "P0050006" "P0040003" ...
##  $ value        : num  7262 47 99 11099 20026 ...
##  $ summary_value: num  18728 18728 18728 18728 21623 ...
##  $ geometry     :sfc_MULTIPOLYGON of length 156; first list element: List of 1
##   ..$ :List of 1
##   .. ..$ : num [1:174, 1:2] -118 -118 -118 -118 -118 ...
##   ..- attr(*, "class")= chr  "XY" "MULTIPOLYGON" "sfg"
##  $ pct          : num  38.776 0.251 0.529 59.264 92.614 ...
m90 <- get_decennial(geography = "state", variables = "H043A001", year = 1990)
m90
## # A tibble: 51 x 4
##    GEOID                 NAME variable value
##    <chr>                <chr>    <chr> <dbl>
##  1    01              Alabama H043A001   325
##  2    02               Alaska H043A001   559
##  3    04              Arizona H043A001   438
##  4    05             Arkansas H043A001   328
##  5    06           California H043A001   620
##  6    08             Colorado H043A001   418
##  7    09          Connecticut H043A001   598
##  8    10             Delaware H043A001   495
##  9    11 District of Columbia H043A001   479
## 10    12              Florida H043A001   481
## # ... with 41 more rows
options(tigris_use_cache = TRUE)

Pierce <- get_acs(state = "WA", county = "Pierce", geography = "tract", 
                  variables = "B19013_001", geometry = TRUE)

head(Pierce)
## Simple feature collection with 6 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -122.489 ymin: 47.09672 xmax: -122.2586 ymax: 47.27054
## epsg (SRID):    4269
## proj4string:    +proj=longlat +datum=NAD83 +no_defs
## # A tibble: 6 x 6
##         GEOID                                           NAME   variable
##         <chr>                                          <chr>      <chr>
## 1 53053060700    Census Tract 607, Pierce County, Washington B19013_001
## 2 53053061500    Census Tract 615, Pierce County, Washington B19013_001
## 3 53053063100    Census Tract 631, Pierce County, Washington B19013_001
## 4 53053071208 Census Tract 712.08, Pierce County, Washington B19013_001
## 5 53053071403 Census Tract 714.03, Pierce County, Washington B19013_001
## 6 53053071504 Census Tract 715.04, Pierce County, Washington B19013_001
## # ... with 3 more variables: estimate <dbl>, moe <dbl>, geometry <S3:
## #   sfc_MULTIPOLYGON>
library(viridis)

Pierce %>%
  ggplot(aes(fill = estimate, color = estimate)) + 
  geom_sf() + 
  coord_sf(crs = 26911) + 
  scale_fill_viridis(option = "magma") + 
  scale_color_viridis(option = "magma") +
  ggsave("Pierce.pdf")
## Saving 7 x 5 in image

Let’s look at median household income in the last 12 months for the State of Washington using ACS 2015 5 year estimates.

options(tigris_use_cache = TRUE)

medhh <- get_acs(state = "WA", geography = "county", 
                  variables = "B19013_001", geometry = TRUE)

head(medhh)
## Simple feature collection with 6 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.7631 ymin: 45.83596 xmax: -117.8194 ymax: 48.55072
## epsg (SRID):    4269
## proj4string:    +proj=longlat +datum=NAD83 +no_defs
## # A tibble: 6 x 6
##   GEOID                        NAME   variable estimate   moe
##   <chr>                       <chr>      <chr>    <dbl> <dbl>
## 1 53005   Benton County, Washington B19013_001    60251  1370
## 2 53007   Chelan County, Washington B19013_001    51837  1886
## 3 53009  Clallam County, Washington B19013_001    47253  1606
## 4 53015  Cowlitz County, Washington B19013_001    47452  1861
## 5 53037 Kittitas County, Washington B19013_001    46458  3093
## 6 53043  Lincoln County, Washington B19013_001    46069  1791
## # ... with 1 more variables: geometry <S3: sfc_MULTIPOLYGON>
medhh %>%
  mutate(NAME = str_replace(NAME,'County, Washington','')) %>% 
  ggplot(aes(x = estimate, y = reorder(NAME, estimate))) + 
  geom_point()

medhh %>% 
  ggplot(aes(fill = estimate, color = estimate)) + 
  geom_sf() + 
  coord_sf(crs = 26911) + 
  scale_fill_viridis(option = "magma") + 
  scale_color_viridis(option = "magma")

# Focus on Thurston County

thursinc <- get_acs(state = "WA", county = "Thurston", geography = "tract", variables = "B19013_001", geometry = TRUE)

head(thursinc)
## Simple feature collection with 6 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -123.0239 ymin: 46.89342 xmax: -122.7017 ymax: 47.168
## epsg (SRID):    4269
## proj4string:    +proj=longlat +datum=NAD83 +no_defs
## # A tibble: 6 x 6
##         GEOID                                             NAME   variable
##         <chr>                                            <chr>      <chr>
## 1 53067010400    Census Tract 104, Thurston County, Washington B19013_001
## 2 53067010510 Census Tract 105.10, Thurston County, Washington B19013_001
## 3 53067011100    Census Tract 111, Thurston County, Washington B19013_001
## 4 53067011822 Census Tract 118.22, Thurston County, Washington B19013_001
## 5 53067012330 Census Tract 123.30, Thurston County, Washington B19013_001
## 6 53067990100   Census Tract 9901, Thurston County, Washington B19013_001
## # ... with 3 more variables: estimate <dbl>, moe <dbl>, geometry <S3:
## #   sfc_MULTIPOLYGON>
thursinc %>%
  mutate(NAME = str_replace(NAME,'Thurston County, Washington','')) %>% 
  mutate(NAME = str_replace(NAME, "Census Tract","")) %>% 
  ggplot(aes(x = estimate, y = reorder(NAME, estimate))) + 
  geom_point()
## Warning: Removed 1 rows containing missing values (geom_point).

thursinc %>% 
  ggplot(aes(fill = estimate, color = estimate)) + 
  geom_sf() + 
  coord_sf(crs = 26911) + 
  scale_fill_viridis(option = "magma") + 
  scale_color_viridis(option = "magma")