https://github.com/rfordatascience/tidytuesday/blob/master/data/acs2015_county_data.csv
skimr::skim(county_data)
## Skim summary statistics
## n obs: 3220
## n variables: 37
##
## Variable type: factor
## variable missing complete n n_unique
## County 0 3220 3220 1928
## State 0 3220 3220 52
## top_counts ordered
## Was: 31, Jef: 26, Fra: 25, Jac: 24 FALSE
## Tex: 254, Geo: 159, Vir: 133, Ken: 120 FALSE
##
## Variable type: integer
## variable missing complete n mean sd p0 p25
## CensusId 0 3220 3220 31393.61 16292.08 1001 19032.5
## Citizen 0 3220 3220 69935.07 205118.91 80 8450.5
## Employed 0 3220 3220 45593.52 149699.5 62 4550.75
## IncomePerCap 0 3220 3220 23981.77 6204.34 5878 20238.5
## IncomePerCapErr 0 3220 3220 1362.52 1049.88 113 755
## Men 0 3220 3220 48896.94 156681.28 42 5637.25
## TotalPop 0 3220 3220 99409.35 319305.45 85 11218
## Women 0 3220 3220 50512.41 162661.95 43 5572
## median p75 p100 hist
## 30024 46105.5 72153 ▃▆▇▆▆▇▁▁
## 19643 49920.5 6e+06 ▇▁▁▁▁▁▁▁
## 10508 28632.75 4635465 ▇▁▁▁▁▁▁▁
## 23460 27053.25 65600 ▁▅▇▂▁▁▁▁
## 1096.5 1631 15266 ▇▁▁▁▁▁▁▁
## 12932 32992.75 4945351 ▇▁▁▁▁▁▁▁
## 26035 66430.5 1e+07 ▇▁▁▁▁▁▁▁
## 13057 33487.5 5093037 ▇▁▁▁▁▁▁▁
##
## Variable type: numeric
## variable missing complete n mean sd p0 p25
## Asian 0 3220 3220 1.23 2.63 0 0.2
## Black 0 3220 3220 8.67 14.28 0 0.5
## Carpool 0 3220 3220 10.28 2.91 0 8.4
## ChildPoverty 1 3219 3220 24.18 11.7 0 16.3
## Construction 0 3220 3220 12.71 4.22 1.7 9.8
## Drive 0 3220 3220 79.18 7.66 5.2 76.6
## FamilyWork 0 3220 3220 0.29 0.46 0 0.1
## Hispanic 0 3220 3220 11.01 19.24 0 1.9
## Income 1 3219 3220 46129.87 12911.3 10499 38191.5
## IncomeErr 1 3219 3220 2850.4 1918.94 270 1635
## MeanCommute 0 3220 3220 23.28 5.6 4.9 19.5
## Native 0 3220 3220 1.72 7.25 0 0.1
## Office 0 3220 3220 22.22 3.2 4.1 20.2
## OtherTransp 0 3220 3220 1.61 1.67 0 0.9
## Pacific 0 3220 3220 0.083 0.73 0 0
## Poverty 0 3220 3220 17.49 8.32 1.4 12.1
## PrivateWork 0 3220 3220 74.22 7.86 25 70.5
## Production 0 3220 3220 15.73 5.74 0 11.5
## Professional 0 3220 3220 30.99 6.37 13.5 26.7
## PublicWork 0 3220 3220 17.56 6.51 5.8 13.1
## SelfEmployed 0 3220 3220 7.93 3.91 0 5.4
## Service 0 3220 3220 18.35 3.64 5 16
## Transit 0 3220 3220 0.97 3.06 0 0.1
## Unemployment 0 3220 3220 8.09 4.1 0 5.5
## Walk 0 3220 3220 3.32 3.76 0 1.4
## White 0 3220 3220 75.43 22.93 0 64.1
## WorkAtHome 0 3220 3220 4.63 3.18 0 2.7
## median p75 p100 hist
## 0.5 1.2 41.6 ▇▁▁▁▁▁▁▁
## 1.9 9.6 85.9 ▇▁▁▁▁▁▁▁
## 9.9 11.8 29.9 ▁▂▇▃▁▁▁▁
## 22.7 30 81.6 ▂▇▇▃▁▁▁▁
## 12.1 14.9 40.3 ▁▇▇▂▁▁▁▁
## 80.7 83.7 94.6 ▁▁▁▁▁▁▇▃
## 0.2 0.3 9.8 ▇▁▁▁▁▁▁▁
## 3.9 9.83 99.9 ▇▁▁▁▁▁▁▁
## 44749 52074 123453 ▁▃▇▃▁▁▁▁
## 2406 3446 21355 ▇▃▁▁▁▁▁▁
## 23 26.8 44 ▁▁▅▇▆▂▁▁
## 0.3 0.6 92.1 ▇▁▁▁▁▁▁▁
## 22.4 24.4 35.4 ▁▁▁▃▇▆▁▁
## 1.3 1.9 39.1 ▇▁▁▁▁▁▁▁
## 0 0 35.3 ▇▁▁▁▁▁▁▁
## 16.15 20.7 64.2 ▂▇▆▂▁▁▁▁
## 75.7 79.7 88.3 ▁▁▁▁▁▃▇▃
## 15.25 19.33 55.6 ▁▇▇▃▁▁▁▁
## 29.9 34.4 74 ▁▇▇▂▁▁▁▁
## 16.2 20.5 66.2 ▅▇▂▁▁▁▁▁
## 6.9 9.4 36.6 ▂▇▂▁▁▁▁▁
## 18.1 20.3 38.2 ▁▁▇▇▂▁▁▁
## 0.4 0.8 61.7 ▇▁▁▁▁▁▁▁
## 7.6 9.9 36.5 ▂▇▃▁▁▁▁▁
## 2.4 4 71.2 ▇▁▁▁▁▁▁▁
## 84.1 93.2 99.8 ▁▁▁▁▂▂▃▇
## 3.9 5.6 37.2 ▇▅▁▁▁▁▁▁
county_data %>% count(State) %>% arrange(desc(n))
## # A tibble: 52 x 2
## State n
## <fct> <int>
## 1 Texas 254
## 2 Georgia 159
## 3 Virginia 133
## 4 Kentucky 120
## 5 Missouri 115
## 6 Kansas 105
## 7 Illinois 102
## 8 North Carolina 100
## 9 Iowa 99
## 10 Tennessee 95
## # ... with 42 more rows
per_cap_state <- county_data %>% ggplot(aes(x=reorder(State, IncomePerCap, FUN = median), y=IncomePerCap)) + geom_boxplot() + theme(axis.text.x = element_text(angle=90)) + ggtitle("Income Per Capita By State")
per_cap_state
Here we look at a subset of the variables and compare the boxplots of the Rhode Island values to the other states.
county_data %>% mutate(is_ri = ifelse(State == "Rhode Island", "RI", "OTHER")) %>%
tidyr::gather("variable", "value", -CensusId, -State, -County, -TotalPop, -Men, -Women, -is_ri, -Employed, -Citizen, -Income, -IncomeErr,-IncomePerCap, -IncomePerCapErr) %>%
ggplot(aes(x=variable, y=value, group=is_ri, fill=is_ri)) + geom_boxplot() + facet_grid(.~variable,scales = "free_x") + theme(axis.text.x = element_text(angle=90))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).
If we pick out some of the variables that show a large difference, we get the below plot:
large_diff_variables <- c("Carpool", "ChildPoverty", "Construction", "Poverty", "PrivateWork", "Production", "Professional")
large_diff <- county_data %>% mutate(is_ri = ifelse(State == "Rhode Island", "RI", "OTHER")) %>%
tidyr::gather("variable", "value", -CensusId, -State, -County, -TotalPop, -Men, -Women, -is_ri, -Employed, -Citizen, -Income, -IncomeErr,-IncomePerCap, -IncomePerCapErr) %>%
filter(variable %in% large_diff_variables) %>%
ggplot(aes(x=variable, y=value, group=is_ri, fill=is_ri)) + geom_boxplot() + facet_grid(.~variable,scales = "free_x") + theme(axis.text.x = element_text(angle=90),
strip.background = element_blank(), strip.text.x = element_blank()) +
ggtitle("Why is Rhode Island So Rich Compared to Other States?")
large_diff
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).
Finally, we plot both per capita income by state and the Rhode Island differences in one single plot.
cowplot::plot_grid(per_cap_state, large_diff, nrow=2)
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).