-
Notifications
You must be signed in to change notification settings - Fork 0
/
cats.R
83 lines (70 loc) · 3.31 KB
/
cats.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
# load packages
library(tidyverse)
library(showtext)
library(htmltools)
showtext_auto()
# load data
cats_uk <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-01-31/cats_uk.csv')
cats_uk_reference <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-01-31/cats_uk_reference.csv')
# clean and wrangle data
cats_uk <- cats_uk |>
filter(algorithm_marked_outlier == FALSE & manually_marked_outlier == FALSE) |>
select(-c(study_name, algorithm_marked_outlier, manually_marked_outlier))
cats <- cats_uk |>
group_by(tag_id) |>
summarise(avg_speed = round(mean(ground_speed),2),
min_speed = min(ground_speed),
max_speed = max(ground_speed))
cats_reference <- cats_uk_reference |>
select(tag_id, animal_id, hunt, prey_p_month, animal_sex, hrs_indoors, n_cats, age_years)
cats_joined <- cats |>
left_join(cats_reference, on = c("tag_id" = "tag_id"))
cats_joined <- cats_joined |>
mutate(hrs_outdoors = 24 - hrs_indoors)
cats_joined <- cats_joined |>
drop_na(age_years)
cats_joined$age_years <- as.character(cats_joined$age_years)
cats_joined$age_years <- factor(cats_joined$age_years, levels = c("0", "1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13"))
# load fonts
font_add_google("Commissioner")
font_add_google(name = "Ubuntu", family = "Ubuntu")
font <- "Commissioner"
font_add(family = "fb",
regular = "C:/Users/Bradf/AppData/Local/Microsoft/Windows/Fonts/Font Awesome 6 Brands-Regular-400.otf")
# load caption
caption = paste0("<span style='font-family:fb;'><br></span>",
"<span style='font-family:sans;color:white;'>.</span>",
"<span style='font-family:sans;'>bradfordjohnson | TidyTuesday - 2023 Week 5</span>")
# visual
cats_joined |>
filter(hunt == TRUE) |>
ggplot(aes(x = age_years, y = avg_speed, size = prey_p_month)) +
geom_point(alpha = .5) +
guides(size = guide_legend(reverse = TRUE)) +
labs(
title = "Cats - The Natural Hunters",
subtitle = "Fast or slow, young or old; cats remain successful hunters.",
x = "Age (years)",
y = "Average Speed (m/s)",
size = "Monthly Prey",
caption = caption
) +
theme_minimal() +
theme(
plot.background = element_rect(fill = "gray100", color = "gray100"),
plot.title = element_text(family = "Ubuntu", hjust = .5, vjust = 1, size = 58, margin = unit(c(0, 0, 0, 6), "mm")),
plot.subtitle = element_text(family = font, hjust = .5, size = 32),
plot.caption = ggtext::element_textbox_simple(color="gray20", size = 27),
plot.margin = unit(c(5.0,7.0,2.5,5.0), "mm"),
axis.title.x = element_text(family = font, size = 30, margin = unit(c(3, 0, 0, 0), "mm")),
axis.title.y = element_text(family = font, size = 30, margin = unit(c(0, 3, 0, 0), "mm")),
axis.text.x = element_text(family = font, size = 28),
axis.text.y = element_text(family = font, size = 28),
legend.title.align = 0.5,
legend.text.align = 0.5,
legend.title = element_text(family = font, size = 30),
legend.text = element_text(family = font, size = 28),
legend.position = c(.9,.9),
)
ggsave("cats.png", width = 9, height = 9)