-
Notifications
You must be signed in to change notification settings - Fork 0
/
TT_2022_wk2.R
83 lines (68 loc) · 2.6 KB
/
TT_2022_wk2.R
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
# TidyTuesday 2022 Week 2
library(tidyverse)
library(lubridate)
library(geofacet)
library(camcorder)
library(ggokabeito)
colony <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-01-11/colony.csv')
stressor <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-01-11/stressor.csv')
glimpse(colony)
skimr::skim(colony)
glimpse(stressor)
# rows with months == 2019 are all Q2 2019. Can correct that but there is no data in colony or stressor tables regardless
# makes more sense to filter out
col_data <- colony %>%
filter(months != "2019") %>%
mutate(quarter = case_when(
months == "January-March" ~ "Q1",
months == "April-June" ~ "Q2",
months == "July-September" ~ "Q3",
months == "October-December" ~ "Q4"
)) %>%
unite(yearquarter, c("year","quarter")) %>%
mutate(date = yq(yearquarter))
stress_data <- stressor %>%
filter(months != "2019") %>%
mutate(quarter = case_when(
months == "January-March" ~ "Q1",
months == "April-June" ~ "Q2",
months == "July-September" ~ "Q3",
months == "October-December" ~ "Q4"
)) %>%
unite(yearquarter, c("year","quarter")) %>%
mutate(date = yq(yearquarter))
stress_data <- stress_data %>%
replace_na(list(stress_pct = 0)) %>%
pivot_wider(names_from = stressor, values_from = stress_pct, values_fill = 0) %>%
unite(id, c("state","yearquarter")) %>%
rename(Diseases = Disesases)
data <- col_data %>%
unite(id, c("state","yearquarter"), remove = FALSE) %>%
left_join(stress_data %>%
select(-months,-date)) %>%
pivot_longer(c("Varroa mites":"Unknown"), names_to = "stressor", values_to = "stress_pct")
data$stressor <- factor(data$stressor, levels = c("Varroa mites", "Diseases", "Pesticides",
"Other pests/parasites", "Other", "Unknown"))
gg_record(dir = file.path(tempdir(), "recording_plot1"),
device = "png",
width = 8,
height = 12)
p1 <- data %>%
ggplot(aes(x = date, y = stress_pct)) +
geom_area(aes(fill = stressor),
color = "lightgrey",
alpha = 0.6) +
facet_geo(~ state, grid = "us_state_grid1") +
scale_x_date(date_labels = "'%y",
date_breaks = "1 year") +
scale_fill_okabe_ito() +
labs(title = "Bee Colony Stressors in the United States",
x = "Year",
y = "Colonies affected by stressor (%)",
fill = "Stressor")
gg_playback(
name = file.path(tempdir(), "recording_plot1", "p1_gif.gif"),
first_image_duration = 10,
last_image_duration = 20,
frame_duration = 0.5
)