Last updated: 2021-09-08

Checks: 7 0

Knit directory: myTidyTuesday/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20210907) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version d0129ae. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    catboost_info/
    Ignored:    data/2021-09-08/
    Ignored:    data/acs_poverty.rds
    Ignored:    data/hike_data.rds
    Ignored:    data/us_states.rds

Unstaged changes:
    Modified:   code/_common.R

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/USPoverty.Rmd) and HTML (docs/USPoverty.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd d0129ae opus1993 2021-09-08 suppress package messages

The data here comes from the US Census American Community Survey 2014-2018 5-year dataset.

Our goal is to recreate this beautiful Tableau graphic, but to represent the population size better.

suppressPackageStartupMessages({
library(tidyverse)
library(hrbrthemes)    # shortcuts for fonts and color palettes
extrafont::loadfonts(quiet = TRUE)

library(cartogram)
library(sf)
library(tmap)
library(tigris)

library(tidycensus)
})

source(here::here("code","_common.R"),
       verbose = FALSE,
       local = knitr::knit_global())
Registered S3 method overwritten by 'tune':
  method                   from   
  required_pkgs.model_spec parsnip
ggplot2::theme_set(theme_jim(base_size = 12))
nonx <- c("78", "69", "66", "72", "60", "15", "02")

if (file.exists("data/us_states.rds")) {
  us_states <- read_rds("data/us_states.rds")
} else {
  us_states <- states(cb = TRUE) %>%
    filter(!STATEFP %in% nonx) %>%
    st_transform(crs = 2163)

  write_rds(us_states, "data/us_states.rds")
}


if (file.exists("data/acs_poverty.rds")) {
  poverty <- read_rds("data/acs_poverty.rds")
} else {
  poverty <- tidycensus::get_acs(
    geography = "county",
    variables = c(poverty = "B17013_001"),
    geometry = TRUE,
    year = 2018
  ) %>%
    select(GEOID, NAME, estimate, moe, geometry)

  write_rds(poverty, "data/acs_poverty.rds")
}

population <-
  suppressMessages(
    tidycensus::get_acs(
      geography = "county",
      variables = c(population = "B01003_001"),
      geometry = FALSE,
      year = 2018
    ) %>%
      select(GEOID, estimate)
  )

The bubbles are generated on top of the shapefile centroids, and then adjusted to fit through iterations with the cartogram package.

counties_sf <- poverty %>%
  left_join(population, by = "GEOID", suffix = c(".pov", ".pop")) %>%
  mutate(
    rate = round(estimate.pov / estimate.pop, 2),
    moe_rate = round(moe / estimate.pop, 3)
  ) %>%
  st_transform(crs = 2163)

county_dorling <-
  cartogram_dorling(
    x = counties_sf,
    weight = "estimate.pop",
    k = 0.2,
    itermax = 100
  )

A ggplot

county_dorling %>%
  filter(!str_detect(NAME, "Alaska|Hawaii|Puerto|Guam")) %>%
  ggplot(aes(fill = rate)) +
  geom_sf(color = "grey60") +
  geom_sf(
    data = us_states,
    fill = NA,
    show.legend = F,
    color = "grey60",
    lwd = .5
  ) +
  coord_sf(crs = 2163, datum = NA) +
  scale_fill_stepsn(colors = c("#eff3ff", "#bdd7e7", "#6baed6", "#3182bd", "#08519c")) +
  labs(
    title = "2014-2018 Poverty Rate in the United States by County", subtitle = "Bubble size corresponds to County Population",
    caption = "US Census American Community Survey | jim_gruman"
  )

And the original tweet, as posted to twitter:

tweetrmd::include_tweet("https://twitter.com/jim_gruman/status/1326673815288623105")

sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

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] tidycensus_1.0   tigris_1.4.1     tmap_3.3-2       sf_1.0-2        
 [5] cartogram_0.2.2  hrbrthemes_0.8.0 forcats_0.5.1    stringr_1.4.0   
 [9] dplyr_1.0.7      purrr_0.3.4      readr_2.0.1      tidyr_1.1.3     
[13] tibble_3.1.4     ggplot2_3.3.5    tidyverse_1.3.1  workflowr_1.6.2 

loaded via a namespace (and not attached):
  [1] readxl_1.3.1       uuid_0.1-4         backports_1.2.1   
  [4] workflows_0.2.3    systemfonts_1.0.2  lwgeom_0.2-7      
  [7] plyr_1.8.6         sp_1.4-5           splines_4.1.1     
 [10] listenv_0.8.0      crosstalk_1.1.1    leaflet_2.0.4.1   
 [13] digest_0.6.27      yardstick_0.0.8    foreach_1.5.1     
 [16] htmltools_0.5.2    viridis_0.6.1      parsnip_0.1.7.900 
 [19] fansi_0.5.0        tune_0.1.6         magrittr_2.0.1    
 [22] tzdb_0.1.2         globals_0.14.0     recipes_0.1.16    
 [25] modelr_0.1.8       gower_0.2.2        extrafont_0.17    
 [28] R.utils_2.10.1     extrafontdb_1.0    hardhat_0.1.6     
 [31] rsample_0.1.0      dials_0.0.9.9000   colorspace_2.0-2  
 [34] rvest_1.0.1        rappdirs_0.3.3     textshaping_0.3.5 
 [37] haven_2.4.3        xfun_0.25          rgdal_1.5-25      
 [40] leafem_0.1.6       crayon_1.4.1       jsonlite_1.7.2    
 [43] iterators_1.0.13   survival_3.2-11    glue_1.4.2        
 [46] stars_0.5-3        gtable_0.3.0       ipred_0.9-11      
 [49] R.cache_0.15.0     tweetrmd_0.0.9     Rttf2pt1_1.3.9    
 [52] future.apply_1.8.1 abind_1.4-5        scales_1.1.1      
 [55] infer_1.0.0        DBI_1.1.1          Rcpp_1.0.7        
 [58] viridisLite_0.4.0  units_0.7-2        GPfit_1.0-8       
 [61] foreign_0.8-81     proxy_0.4-26       lava_1.6.10       
 [64] prodlim_2019.11.13 htmlwidgets_1.5.3  httr_1.4.2        
 [67] RColorBrewer_1.1-2 ellipsis_0.3.2     farver_2.1.0      
 [70] R.methodsS3_1.8.1  pkgconfig_2.0.3    XML_3.99-0.7      
 [73] nnet_7.3-16        sass_0.4.0         dbplyr_2.1.1      
 [76] utf8_1.2.2         here_1.0.1         labeling_0.4.2    
 [79] tidyselect_1.1.1   rlang_0.4.11       DiceDesign_1.9    
 [82] later_1.3.0        tmaptools_3.1-1    cachem_1.0.6      
 [85] munsell_0.5.0      cellranger_1.1.0   tools_4.1.1       
 [88] cli_3.0.1          generics_0.1.0     broom_0.7.9       
 [91] evaluate_0.14      fastmap_1.1.0      ragg_1.1.3        
 [94] yaml_2.2.1         leafsync_0.1.0     knitr_1.33        
 [97] fs_1.5.0           future_1.22.1      whisker_0.4       
[100] R.oo_1.24.0        xml2_1.3.2         compiler_4.1.1    
[103] rstudioapi_0.13    curl_4.3.2         png_0.1-7         
[106] e1071_1.7-8        reprex_2.0.1       lhs_1.1.1         
[109] bslib_0.3.0        stringi_1.7.4      highr_0.9         
[112] gdtools_0.2.3      lattice_0.20-44    Matrix_1.3-4      
[115] styler_1.5.1       classInt_0.4-3     conflicted_1.0.4  
[118] vctrs_0.3.8        tidymodels_0.1.3   furrr_0.2.3       
[121] pillar_1.6.2       lifecycle_1.0.0    jquerylib_0.1.4   
[124] maptools_1.1-2     raster_3.4-13      httpuv_1.6.2      
[127] R6_2.5.1           promises_1.2.0.1   KernSmooth_2.23-20
[130] gridExtra_2.3      parallelly_1.27.0  codetools_0.2-18  
[133] dichromat_2.0-0    MASS_7.3-54        assertthat_0.2.1  
[136] rprojroot_2.0.2    withr_2.4.2        parallel_4.1.1    
[139] hms_1.1.0          grid_4.1.1         rpart_4.1-15      
[142] timeDate_3043.102  class_7.3-19       rmarkdown_2.10    
[145] packcircles_0.3.4  git2r_0.28.0       pROC_1.18.0       
[148] lubridate_1.7.10   base64enc_0.1-3