knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo=F,
  warning=F,
  error=F,
  fig.width = 6,
  fig.height = 4,
  fig.align = "center"
)
library(retirementData)
library(magrittr)
library(kableExtra)
library(scales)
library(readr)
library(dplyr)
library(ggplot2)
data("retirementLoc")

Population 2020 Summary

retirementLoc %>% 
  select(pop_2020) %>% 
  summarize(min = min(pop_2020, na.rm = T),
                   ptile_25 = quantile(pop_2020, .25, na.rm = T), 
                   mean = mean(pop_2020, na.rm = T),
                   median = median(pop_2020, na.rm = T),
                   ptile_75 = quantile(pop_2020, .75, na.rm = T), 
                   max = max(pop_2020, na.rm = T)) %>% 
  slice(n = 1) %>% 
  kbl(format.args = list(big.mark = ",")) %>% 
  kable_styling()
library(scales)
retirementLoc %>% 
  ggplot() +
  aes(pop_2020) +
  geom_density() +
  scale_x_continuous(name = "", labels = comma) +
  labs(title = "U.S. Population by County 2020") +
  theme_minimal()

Population Change 2010 - 2020 Summary

retirementLoc %>% 
  select(pct_pop_change) %>% 
  summarize(min = min(pct_pop_change, na.rm = T),
                   ptile_25 = quantile(pct_pop_change, .25, na.rm = T), 
                   mean = mean(pct_pop_change, na.rm = T),
                   median = median(pct_pop_change, na.rm = T),
                   ptile_75 = quantile(pct_pop_change, .75, na.rm = T), 
                   max = max(pct_pop_change, na.rm = T)) %>% 
  slice(n = 1) %>% 
  kbl(format.args = list(big.mark = ",")) %>% 
  kable_styling(full_width = T, bootstrap_options = c("striped", "hover", "condensed"))
retirementLoc %>% 
  ggplot() +
  aes(pct_pop_change) +
  geom_density() +
  labs(title = "U.S. Population Change by County 2010 - 2020") +
  theme_minimal()

10 Largest Counties by Population

retirementLoc %>% 
  arrange(desc(pop_2020)) %>% 
  slice_head(n = 10) %>% 
  select(fips, state, county, pop_2020, pct_pop_change) %>% 
  kbl(format.args = list(big.mark = ",")) %>% 
  kable_styling(full_width = T, bootstrap_options = c("striped", "hover", "condensed"))

10 Smallest by Population

retirementLoc %>% 
  arrange(desc(pop_2020)) %>% 
  slice_tail(n = 10) %>% 
  select(fips, state, county, pop_2020, pct_pop_change) %>% 
  kbl(format.args = list(big.mark = ",")) %>% 
  kable_styling(full_width = T, bootstrap_options = c("striped", "hover", "condensed"))

Top 10 Counties by Population Change

retirementLoc %>% 
  arrange(desc(pct_pop_change)) %>% 
  slice_head(n = 10) %>% 
  select(fips, state, county, pop_2020, pct_pop_change) %>% 
  kbl(format.args = list(big.mark = ",")) %>% 
  kable_styling(full_width = T, bootstrap_options = c("striped", "hover", "condensed"))

Bottom 10 by Population Change

retirementLoc %>% 
  arrange(pct_pop_change) %>% 
  slice_head(n = 10) %>% 
   select(fips, state, county, pop_2020, pct_pop_change) %>% 
  kbl(format.args = list(big.mark = ",")) %>% 
  kable_styling(full_width = T, bootstrap_options = c("striped", "hover", "condensed"))

Top 10 Counties Population Change Greater than 100,000

retirementLoc %>% 
  dplyr::filter(pop_2020 > 1e5) %>% 
  select(fips, state, county, pop_2020, pct_pop_change) %>% 
  arrange(desc(pct_pop_change)) %>% 
  slice_head(n = 10) %>% 
  kbl(format.args = list(big.mark = ",")) %>% 
  kable_styling(full_width = T, bootstrap_options = c("striped", "hover", "condensed"))

Bottom 10 Counties Population Change Greater than 100,000

retirementLoc %>% 
  dplyr::filter(pop_2020 > 1e5) %>% 
  select(fips, state, county, pop_2020, pct_pop_change) %>% 
  arrange(pct_pop_change) %>% 
  slice_head(n = 10) %>% 
  kbl(format.args = list(big.mark = ",")) %>% 
  kable_styling(full_width = T, bootstrap_options = c("striped", "hover", "condensed"))


RobWiederstein/retirementData documentation built on Dec. 18, 2021, 10:53 a.m.