forked from dgrtwo/data-screencasts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2021_05_11_broadband.Rmd
220 lines (157 loc) · 5.45 KB
/
2021_05_11_broadband.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
---
title: "TidyTemplate"
date: 2021-05-11
output: html_output
---
# TidyTuesday
Join the R4DS Online Learning Community in the weekly #TidyTuesday event!
Every week we post a raw dataset, a chart or article related to that dataset, and ask you to explore the data.
While the dataset will be “tamed”, it will not always be tidy! As such you might need to apply various R for Data Science techniques to wrangle the data into a true tidy format.
The goal of TidyTuesday is to apply your R skills, get feedback, explore other’s work, and connect with the greater #RStats community!
As such we encourage everyone of all skills to participate!
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(tidytuesdayR)
library(scales)
theme_set(theme_light())
```
# Load the weekly Data
Dowload the weekly data and make available in the `tt` object.
```{r Load}
tt <- tt_load("2021-05-11")
broadband_county <- tt$broadband %>%
janitor::clean_names() %>%
rename(state = st) %>%
mutate(state = state.name[match(state, state.abb)]) %>%
mutate(state = ifelse(is.na(state), "District of Columbia", state),
broadband_availability_per_fcc = parse_number(broadband_availability_per_fcc, na = "-"),
broadband_usage = parse_number(broadband_usage, na = "-")) %>%
mutate(county = paste0(str_remove(county_name, " County$"), ", ", state),
county = fct_reorder(county, broadband_availability_per_fcc),
county_id = sprintf("%05d", county_id))
broadband_zip <- tt$broadband_zip %>%
janitor::clean_names() %>%
rename(state = st) %>%
mutate(state = state.name[match(state, state.abb)]) %>%
mutate(state = ifelse(is.na(state), "District of Columbia", state)) %>%
mutate(county = paste0(str_remove(county_name, " County$"), ", ", state),
postal_code = sprintf("%05d", postal_code))
broadband_zip
```
```{r}
library(tidycensus)
library(readxl)
library(rio)
county_population <- read_excel("~/Downloads/co-est2019-annres.xlsx", skip = 3) %>%
select(county = ...1,
population_2017 = `2017`,
population_2019 = `2019`) %>%
separate(county, c("county_name", "state"), sep = ", ") %>%
mutate(county_name = str_remove(county_name, "^\\."))
broadband_with_population <- broadband_county %>%
inner_join(county_population, by = c("county_name", "state"))
broadband_with_population %>%
arrange(desc(population_2017)) %>%
head(40) %>%
ggplot(aes(broadband_availability_per_fcc, county)) +
geom_point() +
scale_x_continuous(labels = percent_format())
broadband_with_population %>%
arrange(desc(population_2017)) %>%
filter(population_2017 >= 30000) %>%
ggplot(aes(population_2017, broadband_availability_per_fcc)) +
geom_point() +
geom_text(aes(label = county), check_overlap = TRUE, vjust = 1, hjust = 1) +
scale_x_log10(labels = comma_format()) +
scale_y_continuous(labels = percent_format()) +
expand_limits(x = 10000) +
labs(y = "Broadband availability in 2017",
x = "Population in 2017")
broadband_with_population %>%
filter(population_2019 >= 10000) %>%
ggplot(aes(population_2019, broadband_usage)) +
geom_point() +
geom_text(aes(label = county), check_overlap = TRUE, vjust = 1, hjust = 1) +
geom_smooth(method = "lm") +
scale_x_log10(labels = comma_format()) +
scale_y_continuous(labels = percent_format()) +
expand_limits(x = 10000) +
labs(y = "Broadband usage in 2019",
x = "Population in 2019")
```
Zip code data
```{r}
library(zipcodeR)
broadband_zip_joined <- broadband_zip %>%
inner_join(zip_code_db %>% select(-state, -county),
by = c(postal_code = "zipcode"))
broadband_zip_joined %>%
ggplot(aes(population_density)) +
geom_histogram() +
scale_x_log10()
broadband_zip_joined %>%
filter(population >= 10000) %>%
filter(state %in% c("Texas", "New York", "California", "Pennsylvania")) %>%
ggplot(aes(population_density, broadband_usage)) +
geom_point() +
scale_x_log10() +
scale_y_continuous(labels = percent) +
facet_wrap(~ state) +
geom_smooth(method = "lm") +
labs(x = "People / square mile in zip code",
y = "Broadband usage")
as_tibble(zip_code_db)
```
```{r}
library(tigris)
library(sf)
zip_code_shapes <- zctas(starts_with = c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"))
zip_code_shapes %>%
head(20) %>%
ggplot() +
geom_sf()
counties_sf <- counties()
counties_sf %>%
inner_join(broadband_with_population, by = c(GEOID = "county_id")) %>%
ggplot() +
geom_sf()
counties_sf %>%
filter(GEOID == "01001")
counties %>%
st_simplify(dTolerance = .1) %>%
ggplot() +
geom_sf()
counties %>%
head(100) %>%
ggplot() +
geom_sf()
```
# Readme
Take a look at the readme for the weekly data to get insight on the dataset.
This includes a data dictionary, source, and a link to an article on the data.
```{r Readme, eval = interactive()}
tt
```
# Glimpse Data
Take an initial look at the format of the data available.
```{r Glimpse}
tt %>%
map(glimpse)
```
# Wrangle
Explore the data and process it into a nice format for plotting! Access each dataset by name by using a dollarsign after the `tt` object and then the name of the data set.
```{r Wrangle}
```
# Visualize
Using your processed dataset, create your unique visualization.
```{r Visualize}
```
# Save Image
Save your image for sharing. Be sure to use the `#TidyTuesday` hashtag in your post on twitter!
```{r}
# This will save your most recent plot
ggsave(
filename = "My TidyTuesday Plot.png",
device = "png")
```