-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTidyTuesday_NPSpecies.R
107 lines (87 loc) · 3.8 KB
/
TidyTuesday_NPSpecies.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Load all the libraries required
library(tidyverse)
library(tidyr)
library(devtools)
library(MetBrewer)
library(NPSpecies)
library(dplyr)
library(magrittr) # dplyr or magrittr both works
library(grid)
library(png)
library(ggimage)
library(magick)
library(ggplot2)
library(showtext)
# (1) Add custom fonts
font_add("Cookie", "Cookie-Regular.ttf") # Font for scientific names
font_add_google("Rye", "Static") # Font for plot.title
font_add("TimesNewRomanPS-BoldMT","C:/WINDOWS/Fonts/timesbd.ttf") # Font for legends title
font_add("TimesNewRomanPSMT", "C:/WINDOWS/Fonts/times.ttf") # Font for legends text
# Automatically use showtext to render plots
showtext_auto()
# (2) Read the image for background
# Image used: https://pixabay.com/illustrations/deer-bambi-watercolor-painting-8187893/
obj <- image_read("wt_deer.png")
# Apply image transformations
obj <- obj |>
# Blur function
image_blur(8, 2.75) |>
# Flip the image horizontally
image_flop()
# Adjust opacity and colorize the image
obj <- image_colorize(obj, 50, "#e8d8c4")
# Save the transformed image as a new file
image_write(obj, "deer_blur.png")
# (3) Filter mammal data for Great Smoky Mountains and mammals category
mammal_data <- NPSpecies::species |>
dplyr::filter(ParkCode == "GRSM") |>
dplyr::filter(CategoryName == "Mammal") |>
dplyr::select(ParkName, Family, SciName, CommonNames, Observations, Abundance, Nativeness) |>
dplyr::arrange(desc(Observations))
# (4) Clean the data to include only observations greater than 20
clean_mammal <- mammal_data |>
dplyr::filter(Observations > 20) |>
dplyr::arrange(desc(Observations)) # Sort by descending order again for clarity
# (5) Plot
plot <- ggplot(clean_mammal, aes(x = reorder(SciName, Observations),
y = Observations,
color = Family))+
geom_segment(aes(xend = reorder(SciName, Observations), yend = 0),
linetype = 1,
linewidth = 1.5) +
geom_point(size = 6) +
coord_flip() +
labs(color = "Mammalian Family", # changing legend title
title = "Great Smoky Mountains:\nHunting for Mammalian Diversity",
subtitle = "Ursidae rules the Biodiversity of Great Smokies. Generically referred to as the BEARS! \nThe Black Bears dominates the population of mammalians in the Smoky Mountains. \nWhile Soricidae lingers as the least abundant.",
x = "Scientific Name",
y = "Observations") +
theme_minimal() +
scale_color_manual(values = met.brewer("Peru2"))+
theme(plot.background = element_rect(fill = "transparent",
color = NA),
plot.title = element_text(family = "Static",
size = 50,
colour = "black",
face = "bold",
hjust = 0),
plot.subtitle = element_text(family = "TimesNewRomanPSMT",
size = 18,
color = "black"),
axis.text = element_text(family = "Cookie",
size = 25,
face = "bold",
colour = "black"),,
panel.grid.major = element_line(colour = "gray50",
linewidth = 0.75,
linetype = "dotted"),
legend.title = element_text(family = "TimesNewRomanPS-BoldMT",
face = "bold",
size = 12),
legend.text = element_text(family = "TimesNewRomanPSMT",
face = "bold",
size = 12),
legend.justification = c("right", "bottom")
)
# Set the background
ggbackground(plot, "deer_blur.png")