Last updated: 2021-09-11

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 22b0c6a. 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/grainstocks.rds
    Ignored:    data/hike_data.rds
    Ignored:    data/us_states.rds
    Ignored:    data/us_states_hexgrid.geojson
    Ignored:    data/weatherstats_toronto_daily.csv

Untracked files:
    Untracked:  code/work list batch targets.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/KidsSpending.Rmd) and HTML (docs/KidsSpending.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 22b0c6a opus1993 2021-09-11 give credit to Hill for geojson template

The data for this week was provided by the US Census Bureau from their State and Local Government finance survey

The Carto platform host connections to geospatial datasets and the geojson format featured here. The web site credits AndrewXHill as the creator.

df_kids_health <-
  df_kids %>%
  filter(variable == "pubhealth") %>%
  group_by(state) %>%
  mutate(
    diff = inf_adj_perchild[which(year == max(year))] - inf_adj_perchild[which(year == min(year))],
    diff_cat = case_when(
      diff < -.1 ~ 1, ## considerably lower
      diff >= -.1 & diff < 0 ~ 2, ## slightly lower
      diff >= 0 & diff <= .2 ~ 3, ## slightly higher
      diff > .2 & diff <= 0.8 ~ 4, ## considerably higher
      diff > 0.8 ~ 5, ## considerably higher
    ),
    diff_cat = factor(diff_cat, levels = 1:5)
  )
## HEX MAP #####################################################################
# the geojson is downloadable from https://team.cartodb.com/u/andrew/tables/andrew.us_states_hexgrid/public/map

map_hex <- geojson_read(
  here::here("data", "us_states_hexgrid.geojson"),
  what = "sp"
)
map_hex@data <- map_hex@data %>%
  mutate(google_name = gsub(" \\(United States\\)", "", google_name))

map_hex_fortified <- tidy(map_hex, region = "google_name")
## smaller polygons for frame

map_hex_buffer <-
  gBuffer(map_hex, width = -.15, byid = T)

map_hex_buffer_fortified <- tidy(map_hex_buffer, region = "google_name")
## calculate centroids
centr <- cbind.data.frame(data.frame(gCentroid(map_hex_buffer, byid = T), id = map_hex@data$google_name))
## calculate lines based on centroids
df_kids_tl <-
  df_kids_health %>%
  left_join(centr, by = c("state" = "id")) %>%
  mutate(
    year_rel = year - (min(year) + (max(year) - min(year)) / 2),
    year_x = x + year_rel / 5.5,
    inf_adj_perchild_rel = inf_adj_perchild - (min(inf_adj_perchild) + (max(inf_adj_perchild) - min(inf_adj_perchild)) / 200) - 1.5,
    inf_y = y + inf_adj_perchild_rel / 2
  ) %>%
  mutate(base = y + min(inf_adj_perchild_rel) / 2)
## centroids for labels
df_kids_labels <-
  df_kids_health %>%
  left_join(map_hex@data[, 6:7], by = c("state" = "google_name")) %>%
  left_join(centr, by = c("state" = "id")) %>%
  mutate(
    y = case_when(
      iso3166_2 == "ME" ~ y + .3,
      iso3166_2 == "WY" ~ y + .7,
      iso3166_2 == "DC" ~ y - .4,
      iso3166_2 == "HI" ~ y + .4,
      iso3166_2 == "DE" ~ y + .1,
      TRUE ~ y
    )
  )

## combine data
df_kids_hex_buffer <- map_hex_buffer_fortified %>%
  left_join(df_kids_health, by = c("id" = "state"))
ggplot(
  data = df_kids_tl,
  aes(year_x, inf_y, group = state)
) +
  geom_polygon(
    data = df_kids_hex_buffer,
    aes(long, lat,
      group = id,
      color = diff_cat
    ),
    fill = "grey9", size = 2.4
  ) +
  geom_polygon(
    data = map_hex_fortified,
    aes(long, lat,
      group = id
    ),
    color = "grey18",
    fill = "transparent", size = 1
  ) +
  geom_line(
    color = "grey72", size = .2
  ) +
  geom_point(
    color = "white",
    size = .1
  ) +
  geom_text(
    data = df_kids_tl %>% filter(year %in% c(1997, 2016)),
    aes(
      label = {
        format(round(inf_adj_perchild, 2), digits = 2)
      },
      hjust = year == 1997
    ),
    family = "IBM Plex Sans",
    color = "grey62",
    size = 1,
    nudge_y = .2
  ) +
  geom_text(
    data = df_kids_labels %>% group_by(iso3166_2) %>% slice(1),
    aes(x, y,
      label = iso3166_2, color = diff_cat,
      color = after_scale(colorspace::lighten(color, .2))
    ),
    family = "IBM Plex Sans",
    size = 2.4,
    fontface = "bold"
  ) +
  geom_richtext(
    data = tibble(
      x = -103.9388, y = 53.8,
      label =
        "<b style='color:#f5f5f5;font-size:15pt;'>US Spending Trends in Children Public Health 1997—2016</b><br><br>
      Each hexagon shows the change in public health spending on children by state and year per child in 1,000 constant dollars.<br><b style='color:#d7191c;'>Rhode Island</b>, <b style='color:#d7191c;'>Tennessee </b>, <b style='color:#d7191c;'>Kansas</b>, <b style='color:#d7191c;'>Alabama</b>, and <b style='color:#d7191c;'>Rhode Island</b> spent less in 2016 compared to 1997 while <b style='color:#1a9641;'>Vermont</b>,<br>the <b style='color:#1a9641;'>District of Columbia</b>, <b style='color:#1a9641;'>Wyoming</b>, <b style='color:#1a9641;'>North Dakota</b>, <b style='color:#1a9641;'>Arizona</b>, <b style='color:#1a9641;'>Pennsylvania</b>, <b style='color:#1a9641;'>Delaware</b>, and <b style='color:#1a9641;'>California</b> increased the amount by far."
    ),
    aes(x, y, label = label),
    inherit.aes = F,
    family = "IBM Plex Sans",
    size = 2.7,
    color = "grey72",
    fill = NA,
    label.color = NA,
    hjust = .5
  ) +
  geom_richtext(
    data = tibble(
      x = -82,
      y = 30,
      text = "<b style='font-size:7pt;'>Change in Public Health Efforts<br>from 1997 to 2016 per Child:</b><br><br>
              <span style='color:#1a9641;'>• Increase by more than K$800</span><br>
              <span style='color:#a6d96a;'>• Increase by K$200 to K$800</span><br>
              <span style='color:#ffffbf;'>• Increase by up to K$200</span><br>
              <span style='color:#fdae61;'>• Decrease by up to K$100</span><br>
              <span style='color:#d7191c;'>• Decrease by more than K$100</span>"
    ),
    aes(x, y, label = text),
    inherit.aes = F,
    family = "IBM Plex Sans",
    color = "grey72",
    size = 2,
    fontface = "bold",
    hjust = 0,
    fill = "grey12",
    lineheight = 1.3,
    label.color = NA,
    label.padding = unit(rep(.6, 4), "lines")
  ) +
  coord_map() +
  scale_color_manual(
    values = c("#d7191c", "#fdae61", "#ffffbf", "#a6d96a", "#1a9641"),
    guide = "none"
  ) +
  labs(caption = "Visualization by Jim Gruman  •  Data: Census Bureau's Annual State and Local Government Finance Survey (Expenditure Variable E32)") +
  theme_void() +
  theme(
    plot.background = element_rect(fill = "grey18", color = NA),
    plot.caption = element_text(
      family = "IBM Plex Sans", color = "grey72",
      size = 6, hjust = .5,
      margin = margin(b = 10, t = 15)
    )
  )


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] showtext_0.9-4  showtextdb_3.0  sysfonts_0.8.5  ggtext_0.1.1   
 [5] rgeos_0.5-7     sp_1.4-5        broom_0.7.9     geojsonio_0.9.4
 [9] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.7     purrr_0.3.4    
[13] readr_2.0.1     tidyr_1.1.3     tibble_3.1.4    ggplot2_3.3.5  
[17] tidyverse_1.3.1 workflowr_1.6.2

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