# Load necessary libraries
pacman::p_load(pacman, readr, dplyr, ggplot2, tidyr, gridExtra)
small_arena_file <- "small_arena_choice_test.csv"
large_arena_file <- "large_arena_choice_test.csv"
wind_tunnel_file <- "wind_tunnel_preference_tests_flours_2022.csv"
# Load datasets
small_arena_data <- read_csv(small_arena_file)
## Rows: 883 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): Treatment, setup, count, flour
## dbl (3): rep, egg_count, block
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
large_arena_data <- read_csv(large_arena_file)
## Rows: 96 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): Side, Wheat_Side_of_Box, Other_Flour, Other_Flour_Side_of_Box, Star...
## dbl (3): Position, Eggs_Wheat, Eggs_Other
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
wind_tunnel_data <- read_csv(wind_tunnel_file)
## Rows: 675 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Flour, Choice, Date
## dbl (1): Beetle
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Small Arena Visualisation
# Summarise egg counts by flour type
small_summary <- small_arena_data %>%
group_by(Treatment) %>%
summarize(mean_eggs = mean(egg_count), sd_eggs = sd(egg_count), .groups = "drop")
# Plot mean egg counts for each flour
ps <- ggplot(small_summary, aes(x = reorder(Treatment, mean_eggs), y = mean_eggs)) +
geom_bar(stat = "identity", fill = "#3399ff") +
geom_errorbar(aes(ymin = mean_eggs - sd_eggs, ymax = mean_eggs + sd_eggs), width = 0.2) +
coord_flip() +
labs(title = "Red Flour Beetles (Tribolium Castaneum); Egg Counts by Flour Type
Small Arena (Choice Assay)", subtitle = "Size: 90-mm Petri dish. Setup: Single female beetle, 48-hour duration.
Source: Department of Agriculture - USDA", x = "Flour Type", y = "Mean Egg Count") +
theme_minimal()
# Large Arena Visualisation
# Summarise egg counts by flour type
large_summary <- large_arena_data %>%
group_by(Other_Flour) %>%
summarize(mean_eggs_other = mean(Eggs_Other), sd_eggs_other = sd(Eggs_Other), .groups = "drop")
# Plot mean egg counts for alternative flours
pl <- ggplot(large_summary, aes(x = reorder(Other_Flour, mean_eggs_other), y = mean_eggs_other)) +
geom_bar(stat = "identity", fill = "#33cc33") +
geom_errorbar(aes(ymin = mean_eggs_other - sd_eggs_other, ymax = mean_eggs_other + sd_eggs_other), width = 0.2) +
coord_flip() +
labs(title = "Red Flour Beetles (Tribolium Castaneum); Egg Counts by Flour Type
Large Arena (Choice Assay)", subtitle = "Size: 45.7 × 55 × 8.9 cm. Setup: 20 mixed-sex adults, 4-day duration
Source: Department of Agriculture - USDA", x = "Flour Type", y = "Mean Egg Count") +
theme_minimal()
# Combine plots
grid.arrange(ps, pl, ncol = 2)

# Summarise choices for each wind direction
wind_directions <- wind_tunnel_data %>%
group_by(Flour, Choice) %>%
summarize(count = n(), .groups = "drop") %>%
group_by(Flour) %>%
mutate(percentage = (count / sum(count)) * 100) # Calculate percentage
# Function to create a bar plot for each wind direction
plot_direction <- function(direction, color, title_label) {
wind_directions %>%
filter(Choice == direction) %>%
ggplot(aes(x = reorder(Flour, percentage), y = percentage)) +
geom_bar(stat = "identity", fill = color) +
coord_flip() +
labs(title = paste(title_label, "Red Flour Beetles (Tribolium Castaneum) Choices by Flour Type"),
subtitle = "Wind Tunnel (Preference Assay); Airflow: 0.45–0.53 m/s
Source: Department of Agriculture - USDA",
x = "Flour Type", y = "Percentage of Choices") +
theme_minimal()
}
# Create individual plots with labels
plot_U <- plot_direction("U", "#3399ff", "Upwind")
plot_L <- plot_direction("L", "#33cc33", "Left")
plot_R <- plot_direction("R", "#ff9933", "Right")
plot_D <- plot_direction("D", "#cc0033", "Downwind")
# Combine plots in a 2x2 grid
grid.arrange(plot_U, plot_L, plot_R, plot_D, ncol = 2, nrow = 2)

# Combine Small Arena and Large Arena data with wind percentages
# Prepare wind data for merging
wind_data <- wind_directions %>%
select(Flour, Choice, percentage) %>%
spread(key = Choice, value = percentage, fill = 0) %>%
rename(Upwind_Percentage = U, Downwind_Percentage = D, Left_Percentage = L, Right_Percentage = R)
# Combine with Small Arena
small_combined <- small_summary %>%
rename(Flour = Treatment, Small_Mean = mean_eggs) %>%
inner_join(wind_data, by = "Flour")
# Combine with Large Arena
large_combined <- large_summary %>%
rename(Flour = Other_Flour, Large_Mean = mean_eggs_other) %>%
inner_join(wind_data, by = "Flour")
# Function to create scatter plots for Small and Large Arena
plot_correlation <- function(data, x_col, y_col, title_label, color = "#3399ff") {
ggplot(data, aes_string(x = x_col, y = y_col, color = "Flour")) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "#666666", linetype = "dashed") +
labs(
title = title_label,
subtitle = "Red Flour Beetles (Tribolium Castaneum) Choices; Egg Counts by Flour Type
Wind Tunnel (Preference Assay); Airflow: 0.45–0.53 m/s
Source: Department of Agriculture - USDA",
x = paste(x_col, "(%)"),
y = "Mean Egg Count"
) +
theme_minimal() +
theme(legend.position = "bottom", legend.title = element_blank())
}
# Create plots for Small Arena
plot_U_small <- plot_correlation(small_combined, "Upwind_Percentage", "Small_Mean", "Small Arena: Upwind vs. Egg Count")
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plot_D_small <- plot_correlation(small_combined, "Downwind_Percentage", "Small_Mean", "Small Arena: Downwind vs. Egg Count")
plot_L_small <- plot_correlation(small_combined, "Left_Percentage", "Small_Mean", "Small Arena: Left vs. Egg Count")
plot_R_small <- plot_correlation(small_combined, "Right_Percentage", "Small_Mean", "Small Arena: Right vs. Egg Count")
# Create plots for Large Arena
plot_U_large <- plot_correlation(large_combined, "Upwind_Percentage", "Large_Mean", "Large Arena: Upwind vs. Egg Count", color = "#33cc33")
plot_D_large <- plot_correlation(large_combined, "Downwind_Percentage", "Large_Mean", "Large Arena: Downwind vs. Egg Count", color = "#33cc33")
plot_L_large <- plot_correlation(large_combined, "Left_Percentage", "Large_Mean", "Large Arena: Left vs. Egg Count", color = "#33cc33")
plot_R_large <- plot_correlation(large_combined, "Right_Percentage", "Large_Mean", "Large Arena: Right vs. Egg Count", color = "#33cc33")
# Display scatter plots individually
plot_U_small
## `geom_smooth()` using formula = 'y ~ x'

plot_D_small
## `geom_smooth()` using formula = 'y ~ x'

plot_L_small
## `geom_smooth()` using formula = 'y ~ x'

plot_R_small
## `geom_smooth()` using formula = 'y ~ x'

plot_U_large
## `geom_smooth()` using formula = 'y ~ x'

plot_D_large
## `geom_smooth()` using formula = 'y ~ x'

plot_L_large
## `geom_smooth()` using formula = 'y ~ x'

plot_R_large
## `geom_smooth()` using formula = 'y ~ x'
