Packages

Note you need to set up an API key for tidycensus. Please check the package notes first.

library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------------------------------------------------ tidyverse 1.2.1 --
## v ggplot2 3.2.0     v purrr   0.3.2
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   1.0.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts --------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(tidycensus)

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

yolo_age_hisp <- 
  get_estimates(geography = "county", 
                product = "characteristics", 
                breakdown = c("SEX", "AGEGROUP", "HISP"),
                breakdown_labels = TRUE, 
                state = "CA", 
                county = "Yolo")

yolo_age_hisp
## # A tibble: 210 x 6
##    GEOID NAME                value SEX       AGEGROUP      HISP            
##    <chr> <chr>               <dbl> <chr>     <fct>         <chr>           
##  1 06113 Yolo County, Cali~ 215959 Both sex~ All ages      Both Hispanic O~
##  2 06113 Yolo County, Cali~ 105170 Male      All ages      Both Hispanic O~
##  3 06113 Yolo County, Cali~ 147821 Both sex~ All ages      Non-Hispanic    
##  4 06113 Yolo County, Cali~  12437 Both sex~ Age 0 to 4 y~ Both Hispanic O~
##  5 06113 Yolo County, Cali~   6357 Male      Age 0 to 4 y~ Both Hispanic O~
##  6 06113 Yolo County, Cali~   6892 Both sex~ Age 0 to 4 y~ Non-Hispanic    
##  7 06113 Yolo County, Cali~   2719 Male      Age 0 to 4 y~ Hispanic        
##  8 06113 Yolo County, Cali~ 110789 Female    All ages      Both Hispanic O~
##  9 06113 Yolo County, Cali~   6080 Female    Age 0 to 4 y~ Both Hispanic O~
## 10 06113 Yolo County, Cali~  71277 Male      All ages      Non-Hispanic    
## # ... with 200 more rows
yolo_compare <- yolo_age_hisp %>% 
  filter(str_detect(AGEGROUP, "^Age"), 
         HISP != "Both Hispanic Origins", 
         SEX != "Both sexes") %>% 
  mutate(value = if_else(SEX == "Male", -value, value))

library(ggplot2)
gg_yolo_compare <- ggplot(yolo_compare, aes(x = AGEGROUP, y = value, fill = SEX)) +
  geom_bar(stat = "identity", width = 1) +
  theme_minimal(
    # base_family = "Roboto"
    ) +
  scale_y_continuous(labels = function(y) paste0(abs(y / 1000), "k")) +
  scale_x_discrete(labels = function(x) gsub("Age | years", "", x)) +
  scale_fill_manual(values = c("darkred", "navy")) +
  coord_flip() +
  facet_wrap(~ HISP) +
  labs(x = "", 
       y = "2017 Census Bureau population estimate", 
       title = "Yolo County, California", 
       fill = "", 
       caption = "Data source: US Census Bureau population estimates & tidycensus R package")

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plotly::ggplotly(gg_yolo_compare)

sessionInfo

sessionInfo()
## R version 3.6.1 (2019-07-05)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18362)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] plotly_4.9.0     tidycensus_0.9.2 forcats_0.4.0    stringr_1.4.0   
##  [5] dplyr_0.8.3      purrr_0.3.2      readr_1.3.1      tidyr_1.0.0     
##  [9] tibble_2.1.3     ggplot2_3.2.0    tidyverse_1.2.1 
## 
## loaded via a namespace (and not attached):
##  [1] httr_1.4.1         jsonlite_1.6       viridisLite_0.3.0 
##  [4] modelr_0.1.4       shiny_1.3.2        assertthat_0.2.1  
##  [7] sp_1.3-1           cellranger_1.1.0   yaml_2.2.0        
## [10] pillar_1.4.2       backports_1.1.4    lattice_0.20-38   
## [13] glue_1.3.1         uuid_0.1-2         digest_0.6.21     
## [16] promises_1.0.1     rvest_0.3.4        colorspace_1.4-1  
## [19] htmltools_0.3.6    httpuv_1.5.1       pkgconfig_2.0.2   
## [22] broom_0.5.2        haven_2.1.1        xtable_1.8-4      
## [25] scales_1.0.0       later_0.8.0        tigris_0.8.2      
## [28] generics_0.0.2     withr_2.1.2        lazyeval_0.2.2    
## [31] cli_1.1.0          magrittr_1.5       crayon_1.3.4      
## [34] readxl_1.3.1       mime_0.7           maptools_0.9-5    
## [37] evaluate_0.14      fansi_0.4.0        nlme_3.1-140      
## [40] xml2_1.2.2         foreign_0.8-71     class_7.3-15      
## [43] tools_3.6.1        data.table_1.12.2  hms_0.5.1         
## [46] lifecycle_0.1.0    munsell_0.5.0      compiler_3.6.1    
## [49] e1071_1.7-2        rlang_0.4.0        classInt_0.3-3    
## [52] units_0.6-3        grid_3.6.1         rstudioapi_0.10   
## [55] rappdirs_0.3.1     htmlwidgets_1.3    crosstalk_1.0.0   
## [58] labeling_0.3       rmarkdown_1.15.1   gtable_0.3.0      
## [61] DBI_1.0.0          curl_4.1           R6_2.4.0          
## [64] lubridate_1.7.4    knitr_1.24         rgdal_1.4-4       
## [67] utf8_1.1.4         zeallot_0.1.0      KernSmooth_2.23-15
## [70] stringi_1.4.3      Rcpp_1.0.2         vctrs_0.2.0       
## [73] sf_0.7-7           tidyselect_0.2.5   xfun_0.9