Get the development version of ggplot2

# devtools::install_github("tidyverse/ggplot2")
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 3.4.2
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`.
options(tigris_use_cache = TRUE)

Get a list of the ACS Variables

v15 <- load_variables(2015, "acs5", cache = TRUE)

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)

hcs <- get_acs(state = "WA", geography = "county", 
                  variables = "B27001_001", geometry = TRUE)

head(hcs)
## 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 B27001_001   183388   482
## 2 53007   Chelan County, Washington B27001_001    73694   154
## 3 53009  Clallam County, Washington B27001_001    71421   240
## 4 53015  Cowlitz County, Washington B27001_001   101330   250
## 5 53037 Kittitas County, Washington B27001_001    41897   143
## 6 53043  Lincoln County, Washington B27001_001    10234    43
## # ... with 1 more variables: geometry <S3: sfc_MULTIPOLYGON>
hcs %>%
  mutate(NAME = str_replace(NAME,'County, Washington','')) %>% 
  ggplot(aes(x = estimate, y = reorder(NAME, estimate))) + 
  geom_point()

# 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")

Get the uninsured rate for males under 6.

u6m <- get_acs(state = "WA", geography = "county", 
                  variables = c("B27001_003","B27001_005"), geometry = TRUE,output = "wide")
u6m %>% 
  mutate(rate = B27001_005E/B27001_003E) %>% 
  mutate(NAME = str_replace(NAME,'County, Washington','')) -> u6m
summary(u6m$rate)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.00000 0.03197 0.04600 0.04995 0.06443 0.19097
u6m %>%  ggplot(aes(x = rate, y = reorder(NAME, rate))) + 
  geom_point()

Get Entire Table

hitab <- get_acs(state = "WA", geography = "county", 
                  table = "B27001", geometry = TRUE)
str(hitab)
## Classes 'sf', 'tbl_df', 'tbl' and 'data.frame':  2223 obs. of  6 variables:
##  $ GEOID   : chr  "53005" "53005" "53005" "53005" ...
##  $ NAME    : chr  "Benton County, Washington" "Benton County, Washington" "Benton County, Washington" "Benton County, Washington" ...
##  $ variable: chr  "B27001_001" "B27001_002" "B27001_003" "B27001_004" ...
##  $ estimate: num  183388 91386 8588 8330 258 ...
##  $ moe     : num  482 458 300 326 117 307 504 380 178 373 ...
##  $ geometry:sfc_MULTIPOLYGON of length 2223; first list element: List of 1
##   ..$ :List of 1
##   .. ..$ : num [1:272, 1:2] -120 -120 -120 -120 -120 ...
##   ..- attr(*, "class")= chr  "XY" "MULTIPOLYGON" "sfg"
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA
##   ..- attr(*, "names")= chr  "GEOID" "NAME" "variable" "estimate" ...
table(hitab$variable)
## 
## B27001_001 B27001_002 B27001_003 B27001_004 B27001_005 B27001_006 
##         39         39         39         39         39         39 
## B27001_007 B27001_008 B27001_009 B27001_010 B27001_011 B27001_012 
##         39         39         39         39         39         39 
## B27001_013 B27001_014 B27001_015 B27001_016 B27001_017 B27001_018 
##         39         39         39         39         39         39 
## B27001_019 B27001_020 B27001_021 B27001_022 B27001_023 B27001_024 
##         39         39         39         39         39         39 
## B27001_025 B27001_026 B27001_027 B27001_028 B27001_029 B27001_030 
##         39         39         39         39         39         39 
## B27001_031 B27001_032 B27001_033 B27001_034 B27001_035 B27001_036 
##         39         39         39         39         39         39 
## B27001_037 B27001_038 B27001_039 B27001_040 B27001_041 B27001_042 
##         39         39         39         39         39         39 
## B27001_043 B27001_044 B27001_045 B27001_046 B27001_047 B27001_048 
##         39         39         39         39         39         39 
## B27001_049 B27001_050 B27001_051 B27001_052 B27001_053 B27001_054 
##         39         39         39         39         39         39 
## B27001_055 B27001_056 B27001_057 
##         39         39         39

Look at Thurston County

filter(hitab,NAME=="Thurston County, Washington")
## # A tibble: 57 x 6
##    GEOID                        NAME   variable estimate   moe
##    <chr>                       <chr>      <chr>    <dbl> <dbl>
##  1 53067 Thurston County, Washington B27001_001   256560   692
##  2 53067 Thurston County, Washington B27001_002   123968   631
##  3 53067 Thurston County, Washington B27001_003    10143   339
##  4 53067 Thurston County, Washington B27001_004     9724   376
##  5 53067 Thurston County, Washington B27001_005      419   200
##  6 53067 Thurston County, Washington B27001_006    20156   378
##  7 53067 Thurston County, Washington B27001_007    19116   486
##  8 53067 Thurston County, Washington B27001_008     1040   297
##  9 53067 Thurston County, Washington B27001_009    11511   181
## 10 53067 Thurston County, Washington B27001_010     9300   411
## # ... with 47 more rows, and 1 more variables: geometry <S3:
## #   sfc_MULTIPOLYGON>