knitr::opts_chunk$set( message = FALSE, warning = FALSE, collapse = TRUE, eval = nzchar(Sys.getenv("COMPILE_VIG")), comment = "#>" )
library(tongfen) library(dplyr) library(ggplot2) #library(mountainmathHelpers)
As an example we will explore changing household size between the 2000 and 2010 US census. First we need to build the metadata for our variables "H011001" for population and "H013001" for households.
variables=c(population="H011001",households="H013001") meta <- c(2000,2010) %>% lapply(function(year){ v <- variables %>% setNames(paste0(names(.),"_",year)) meta_for_additive_variables(paste0("dec",year),v) }) %>% bind_rows() meta
Armed with that we can call get_tongfen_us_census
to request the data on a common geography based on census tracts and compute the change in household size.
census_data <- get_tongfen_us_census(regions = list(state="CA"), meta=meta, level="tract") %>% mutate(change=population_2010/households_2010-population_2000/households_2000)
census_data %>% names()
We bin the data for better plotting and zoom in on the Bay area.
census_data %>% mutate(c=cut(change,c(-Inf,-0.5,-0.3,-0.2,-0.1,0,0.1,0.2,0.3,0.5,Inf))) %>% ggplot() + geom_sf(aes(fill=c), size=0.05) + scale_fill_brewer(palette = "RdYlGn") + labs(title="Bay area change in average household size 2000-2010", fill=NULL) + #geom_water() + geom_roads() + coord_sf(datum=NA,xlim=c(-122.6,-121.7),ylim=c(37.2,37.9))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.