# Load packages ----
library(tidyverse)
library(janitor)
library(here)
library(showtext)
library(tmap)
library(spData)
library(sf)
# Read in pollinator data ----
pp_interactions <- read_csv(here("data", "pp-interactions.csv")) |>
clean_names()
################################ Petal Plot ################################
# Add a column with pollinator generic type to group hover flies together ----
pp_interactions <- pp_interactions |>
mutate(pol_type = case_when(str_detect(vissp_type, "beetle") ~ "Beetle",
str_detect(vissp_type, "hover-fly") ~ "Hover Fly",
str_detect(vissp_type, " fly") ~ "Fly",
str_detect(vissp_type, "wasp") ~ "Wasp",
str_detect(vissp_type, "butterfly") ~ "Butterfly",
str_detect(vissp_type, "grasshopper") ~ "Grasshopper",
str_detect(vissp_type, "moth") ~ "Moth",
str_detect(vissp_type, "hummingbird") ~ "Hummingbird",
str_detect(vissp_type, "social") ~ "Social Bee",
str_detect(vissp_type, "solitary") ~ "Solitary Bee",
str_detect(vissp_type, "ant") ~ "Ant",
str_detect(vissp_type, "spider") ~ "Spider",
.default = "other"))
# Wrangle data for petal plot ----
petal_plot_data <- pp_interactions |>
# Remove 2021, as only bees were sampled in this year
filter(year != 2021) |>
# Filter out bees and "other" pollinators
filter(pol_type != "other" & pol_type != "Social Bee" & pol_type != "Solitary Bee") |>
# Count total visits per pollinator
group_by(vissp_type) |>
summarize(visits = sum(no_int, na.rm = TRUE)) |>
ungroup() |>
# Pull out the top 5 busiest pollinators
slice_max(order_by = visits, n = 5)
# Font
font_add_google(name = "Quicksand", family = "quicksand")
showtext_auto()
# Define number of petals and petal angle
petals = 5
petal_angle = 360/petals
# Plot
petal_plot_data |>
# Calculate angles and radii
mutate(petal = row_number(),
theta0 = petal * petal_angle) |>
reframe(theta = theta0 + c(0, -petal_angle/2, 0,
petal_angle/2, 0),
r = visits * c(0, 0.6, 1, 0.6, 0), .by = c(vissp_type, visits, petal, theta0)) |>
# Plot theta and r and group by petal no.
ggplot(aes(theta, r, group = petal)) +
ggforce::stat_bspline(geom = "area", n = 1000, fill = "#E195AB", ) +
coord_radial() +
theme_void() +
theme(
plot.title = element_text(family = "quicksand",
face = "bold",
hjust = 0.5,
size = 18),
plot.subtitle = element_text(family = "quicksand",
hjust = 0.5),
plot.background = element_rect(fill = "white", color = "white")
)
################################ Map ################################
# Grab Oregon basemap ----
oregon <- us_states |>
filter(NAME == "Oregon")
# Define region with the coordinates from metadata (min_lat, min_lon, max_lat, max_lon) ----
min_lat <- 44.20308930
min_lon <- -122.15208820
max_lat <- 44.28020840
max_lon <- -122.12605670
# Create a box of the study region ----
bbox <- st_sfc(st_polygon(list(matrix(c(min_lon, min_lat,
min_lon, max_lat,
max_lon, max_lat,
max_lon, min_lat,
min_lon, min_lat),
ncol = 2, byrow = TRUE))), crs = st_crs(oregon))
# Find the centroid of the box ----
centroid <- st_centroid(bbox, crs = st_crs(oregon))
# Create buffers around centroid for bee and hover fly ----
# Transform to a projected crs
centroid_projected <- st_transform(centroid, crs = 26910)
# Create a 1km buffer (1000 meters)
bee_buffer <- st_buffer(centroid_projected, dist = 1000) |>
st_transform(crs = st_crs(oregon))
# Create a 100km buffer (100000 meters)
fly_buffer <- st_buffer(centroid_projected, dist = 100000) |>
st_transform(crs = st_crs(oregon))
# Map ----
tm_shape(oregon) +
tm_fill(col = "#FDFAFB") +
tm_borders(lwd = 5, col = "#666666") +
tm_layout(frame = FALSE) +
tm_shape(fly_buffer) +
tm_borders(col = "#CB904A", lwd = 4) +
tm_shape(centroid) +
tm_dots(col = "#E195AB", shape = 4, size = 1, border.lwd = 5) +
tm_shape(bee_buffer) +
tm_fill(col = "black", border.lwd = 2)
################################ Favorite Flowers Plot ################################
# Create a pollinator data frame that only includes bumble bees and hover flies ----
hvb <- pp_interactions |>
filter(year != 2021) |>
filter(str_detect(vissp_type, "hover-fly") | str_detect(vissp_name, "Bombus")) |>
mutate(pollinator = case_when(str_detect(vissp_type, "hover-fly") ~ "Hover-fly",
str_detect(vissp_type, "bee") ~ "Bee")) |>
group_by(pltsp_code, pollinator) |>
summarise(visits = sum(no_int, na.rm = TRUE)) |>
ungroup()
# Lollipop chart for bees ----
hvb |>
filter(pollinator == "Bee") |>
slice_max(order_by = visits, n = 3) |>
mutate(rank = c("A", "B", "C")) |>
ggplot(aes(x = rank, y = visits)) +
geom_point(fill = "#ECCED7") +
geom_linerange(aes(ymin = 0, ymax = visits)) +
labs(y = "Number of flower visits") +
scale_x_discrete(labels = c( "#1. Nuttail's Larkspur",
"#2. Blue Thimble Flower",
"#3. Mountain Owl's Clover")) +
scale_y_continuous(labels = scales::label_comma()) +
theme_minimal() +
theme(
# Axis labels
axis.title.x = element_blank(),
axis.title.y = element_text(family = "quicksand", margin = margin(10,15,0,10),
size = 40),
axis.text.x = element_text(size = 40, family = "quicksand", face = "bold"),
axis.text.y = element_text(size = 40, family = "quicksand"),
# Grid lines
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
# Margins
plot.margin = margin(10, 10, 10, 10),
plot.background = element_rect(color = "white", fill = "white")
)
# Lollipop chart for flies ----
hvb |>
filter(pollinator == "Hover-fly") |>
slice_max(order_by = visits, n = 3) |>
mutate(rank = c("A", "B", "C")) |>
ggplot(aes(x = rank, y = visits)) +
geom_point(fill = "#ECCED7") +
geom_linerange(aes(ymin = 0, ymax = visits)) +
labs(y = "Number of flower visits") +
scale_x_discrete(labels = c( "#1. Gray's Licorice Root",
"#2. Oregon Sunshine",
"#3. Blue Thimble Flower" )) +
scale_y_continuous(labels = scales::label_comma(),
breaks = c(0, 1000, 2000, 3000, 4000)) +
theme_minimal() +
theme(
# Axis labels
axis.title.x = element_blank(),
axis.title.y = element_text(family = "quicksand", margin = margin(0,15,0,0),
size = 40),
axis.text.x = element_text(size = 40, family = "quicksand", face = "bold"),
axis.text.y = element_text(size = 40, family = "quicksand"),
# Grid lines
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
# Margins
plot.margin = margin(10, 10, 10, 10),
plot.background = element_rect(color = "white", fill = "white")
)