/
2023-w02-birds.qmd
340 lines (247 loc) · 8.31 KB
/
2023-w02-birds.qmd
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
Set working directory
```{r}
setwd(here::here('2023-w02-birds'))
```
Load packages and data
```{r}
library(tidyverse)
feederwatch <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-01-10/PFW_2021_public.csv')
site_data <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-01-10/PFW_count_site_data_public_2021.csv')
```
Clean names.
This makes it easier to work with column names.
```{r}
feederwatch_clean <- feederwatch |> janitor::clean_names()
site_data_clean <- site_data |> janitor::clean_names()
```
Take a look at column names and compare with data dictionary
```{r}
colnames(feederwatch_clean)
colnames(site_data_clean)
```
It looks like there are many 'fed_in' variable names in the 'site_data' data set.
Let's take a look at all of them.
Tidyselect helpers will give us a selection.
```{r}
site_data_clean |>
select(starts_with('fed'))
```
This looks weird.
It's only zeroes and ones and NAs.
Probably a true/false kind of thing.
Let's bring more columns into this.
There's `loc_id` and `proj_period_id`.
```{r}
site_data_clean |>
select(loc_id, proj_period_id, starts_with('fed'))
```
This is starting to make sense.
Each feeding site has a unique location and a project id that contains what looks like a year.
Let's check how many project IDs there are.
```{r}
unique(site_data_clean$proj_period_id)
```
All project IDs contain the same prefix.
Let's remove it and transform the character vector into an actual numeric vector.
`parse_number()` can take care of that.
```{r}
parse_number(site_data_clean$proj_period_id)[1:10]
```
Perfect, now let's save this data set.
```{r}
sites_fed <- site_data_clean |>
select(loc_id, proj_period_id, starts_with('fed')) |>
mutate(year = parse_number(proj_period_id), .before = 2) |>
select(-proj_period_id)
sites_fed
```
Next, we're going to take care of missing values.
Let's have a look how many missing values there are.
::: panel-tabset
## Using `across()`
```{r}
sites_fed |>
summarise(across(.cols = everything(), .fns = ~sum(is.na(.))))
```
## Using for-loop
```{r}
columns <- colnames(sites_fed)
missing_vals <- seq_along(columns)
names(missing_vals) <- columns
for (col in columns) {
missing_vals[col] <- sum(is.na(sites_fed[[col]]))
}
missing_vals
```
:::
There is missing data.
Let's filter those that have missing data in any of the month columns.
The fed_yr_round column can be filled by us then.
::: panel-tabset
## Functional programming `{purrr}`
```{r}
complete_monthly_infos <- sites_fed |>
drop_na(fed_in_jan:fed_in_dec) |>
mutate(across(-c(loc_id, year), as.logical))
complete_monthly_infos$fed_yr_round <- pmap_lgl(
.l = complete_monthly_infos |> select(-c(loc_id, year, fed_yr_round)),
.f = all
)
```
## Using `rowSums()`
```{r}
complete_monthly_infos <- sites_fed |>
drop_na(fed_in_jan:fed_in_dec) |>
mutate(across(-c(loc_id, year), as.logical))
number_of_months_fed <- complete_monthly_infos |>
select(-c(loc_id, year, fed_yr_round)) |>
rowSums()
complete_monthly_infos$fed_yr_round <- (number_of_months_fed == 12)
```
:::
Now, let us bring our data into a tidy format.
That's what `pivot_longer()` will do for us.
```{r}
sites_fed_infos <- complete_monthly_infos |>
pivot_longer(
cols = -c(loc_id, year),
names_to = 'month',
names_prefix = 'fed_in_',
values_to = 'fed'
)
sites_fed_infos
```
Next, we're able to do a little bit of counting.
```{r}
fed_counts <- sites_fed_infos |>
count(year, month, fed)
fed_counts
```
Let's check how many sites there are over the years.
```{r}
sites_over_years <- fed_counts |>
filter(month != 'fed_yr_round') |>
group_by(year) |>
summarise(n = sum(n))
sites_over_years |>
ggplot(aes(year, n)) +
geom_line()
```
Looks like overall the number of sites increased over the years.
This plot was just something we did for ourselves.
No need to customize it further.
Finally, let's have a look at how many sites feed all-year.
Maybe over time more or maybe less bird sites are active every month.
```{r}
sites_fed_infos |>
filter(month == 'fed_yr_round') |>
ggplot(aes(x = year, fill = fed)) +
geom_bar(position = 'fill')
```
Alright, it looks like there is a trend that more and more bird sites are active every month.
Let's make this viz a bit prettier.
First, let's apply `theme_minimal()` and make the bars wider (plus black border).
```{r}
sites_fed_infos |>
filter(month == 'fed_yr_round') |>
ggplot(aes(x = year, fill = fed)) +
geom_bar(position = 'fill', col = 'black', width = 1) +
theme_minimal(base_size = 14)
```
Second, add labels.
Don't forget to put your Twitter handle into the caption.
```{r}
sites_fed_infos |>
filter(month == 'fed_yr_round') |>
ggplot(aes(x = year, fill = fed)) +
geom_bar(position = 'fill', col = 'black', width = 1) +
theme_minimal(base_size = 14) +
labs(
x = element_blank(),
y = 'Share of bird sites',
fill = 'Feeds all-year',
title = 'Over the years, the share of bird sites that feed every\nmonth of the year increased',
caption = 'TidyTuesday 2023 - Week 02 | Viz: @rappa753'
)
```
Third, let us format the y-axis as percent.
```{r}
sites_fed_infos |>
filter(month == 'fed_yr_round') |>
ggplot(aes(x = year, fill = fed)) +
geom_bar(position = 'fill', col = 'black', width = 1) +
theme_minimal(base_size = 14) +
labs(
x = element_blank(),
y = 'Share of bird sites',
fill = 'Feeds all-year',
title = 'Over the years, the share of bird sites that feed every\nmonth of the year increased',
caption = 'TidyTuesday 2023 - Week 02 | Viz: @rappa753'
) +
scale_y_continuous(labels = scales::percent_format())
```
Fourth, let's pick better colors manually.
```{r}
sites_fed_infos |>
filter(month == 'fed_yr_round') |>
ggplot(aes(x = year, fill = fed)) +
geom_bar(position = 'fill', col = 'black', width = 1) +
theme_minimal(base_size = 14) +
labs(
x = element_blank(),
y = 'Share of bird sites',
fill = 'Feeds all-year',
title = 'Over the years, the share of bird sites that feed every\nmonth of the year increased',
caption = 'TidyTuesday 2023 - Week 02 | Viz: @rappa753'
) +
scale_y_continuous(labels = scales::percent_format()) +
scale_fill_manual(values = c('grey90', 'dodgerblue2'))
```
Fifth, get rid of the extra spacing surrounding the bars.
```{r}
sites_fed_infos |>
filter(month == 'fed_yr_round') |>
ggplot(aes(x = year, fill = fed)) +
geom_bar(position = 'fill', col = 'black', width = 1) +
theme_minimal(base_size = 14) +
labs(
x = element_blank(),
y = 'Share of bird sites',
fill = 'Feeds all-year',
title = 'Over the years, the share of bird sites that feed every\nmonth of the year increased',
caption = 'TidyTuesday 2023 - Week 02 | Viz: @rappa753'
) +
scale_y_continuous(labels = scales::percent_format()) +
scale_fill_manual(values = c('grey90', 'dodgerblue2')) +
coord_cartesian(expand = FALSE)
```
Finally, move the legend and title.
```{r}
sites_fed_infos |>
filter(month == 'fed_yr_round') |>
ggplot(aes(x = year, fill = fed)) +
geom_bar(position = 'fill', col = 'black', width = 1) +
theme_minimal(base_size = 14) +
labs(
x = element_blank(),
y = 'Share of bird sites',
fill = 'Feeds all-year',
title = 'Over the years, the share of bird sites that feed every\nmonth of the year increased',
caption = 'TidyTuesday 2023 - Week 02 | Viz: @rappa753'
) +
scale_y_continuous(labels = scales::percent_format()) +
scale_fill_manual(values = c('grey90', 'dodgerblue2')) +
coord_cartesian(expand = FALSE) +
theme(
legend.position = 'top',
plot.title.position = 'plot'
)
```
There's lots more one can do with the data or the plot.
But as a start, this is probably okay.
For now, you can share your plot on Twitter using the #tidyTuesday hashtag.
If you do, think about sharing your code as well.
Common practices for sharing the code: A dedicated tidyTuesday repo on Github.
Or you can just upload the code at [gist.github.com](https://gist.github.com/).
If you want to learn more, then check out [https://www.rscreencasts.com/](https://www.rscreencasts.com/).
It's a project that documents all of [David Robinson](https://www.youtube.com/@safe4democracy/videos)'s great screencasts in which he's analyzing TidyTuesday data in lightning speed.