Census ACS Housing

### replicated from: https://austinwehrwein.com/data-visualization/housing/
library(albersusa) #for the projection
library(tidycensus) #data
#### library(awtools) #aesthetics
library(tidyverse) 
## -- Attaching packages ------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.0       v purrr   0.2.5  
## v tibble  2.1.3       v dplyr   0.8.0.1
## v tidyr   0.8.2       v stringr 1.3.1  
## v readr   1.1.1       v forcats 0.3.0
## -- Conflicts ---------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(viridis)
## Loading required package: viridisLite
library(ggrepel)

census_api_key("12c55b28a8b13fd30e88db9e59bee9edd0fa8ce1")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
options(tigris_use_cache = TRUE)
income<- get_acs(geography = "county", variables = "B19013_001")
## Getting data from the 2013-2017 5-year ACS
hc<- get_acs(geography = "county", variables = "B25105_001", geometry = TRUE)
## Getting data from the 2013-2017 5-year ACS
hc$estimated<-hc$estimate*12
income$percent<-hc$estimated/income$estimate*100

cty_sf <- counties_sf("aeqd")
cty_sf$NAME<-paste0(as.character(cty_sf$name),' ',as.character(cty_sf$lsad),', ',as.character(cty_sf$state))
cty_income<-left_join(cty_sf,income,by='NAME')


#find the cities 
hrdshp<- income %>%
  arrange(-percent) %>%
  slice(1:25) %>%
  inner_join(.,cty_sf) %>%
  mutate(
    label=paste0(name,', ',as.character(iso_3166_2))
  )
## Joining, by = "NAME"
cty_income %>%
  ggplot(aes(fill = percent, color = percent)) + 
  geom_sf() + 
  geom_label_repel(data=hrdshp,
                   aes(label=label, geometry=geometry),
                   size=3,
                   color='#222222',
                   stat = "sf_coordinates") +
  scale_fill_viridis(option = "magma") + 
  scale_color_viridis(option = "magma")+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        legend.position="bottom") +
  labs(title='Burden of roof',
       subtitle='The impact of housing costs on income. The percent value is calculated by dividing median household\nincome (B19013_001) by the median monthly housing costs (B25105_001) by county. The map\nshows the names of the top 25 counties by their percent.',
       caption='Data: tidycensus\nACS 2013-2017 5-year estimates')

2019-08-06