# Load required libraries
pacman::p_load(pacman, tidyverse, tidyr, dplyr, readr, grid, gridExtra, ggplot2, RColorBrewer)

# Load the CSV file
survey_data <- read_csv("Ferraro_etal_survey_IdentifyerMasked.csv")
## Rows: 665 Columns: 34
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (26): date, WeekDayEnd, trail, treatment, DIV_NUMTYPE, PR_1, PR_2, PR_3,...
## dbl  (8): RecordID, num_adults, num_children, PR_8_BATREVCO, Perceivedrestor...
## 
## ℹ 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.
# Convert categorical variables to factors
survey_data$trail <- as.factor(survey_data$trail)
survey_data$treatment <- as.factor(survey_data$treatment)
survey_data$DIV_NUMTYPE <- factor(survey_data$DIV_NUMTYPE, 
                                  levels = c("0-3 different types of birds", 
                                             "4-7 different types of birds", 
                                             "8-11 different types of birds", 
                                             "12-15 different types of birds", 
                                             "More than 15 different types of birds"))

# Viz 1: Violin Plot of Perceived Restoration by Treatment and Trail
ggplot(survey_data, aes(x = treatment, y = Perceivedrestoration, fill = treatment)) +
  geom_violin(alpha = 0.7) +
  geom_boxplot(width = 0.1, fill = "white") +
  facet_wrap(~ trail) +
  labs(title = "Perceived Restoration by Treatment and Trail",
       subtitle = "Impact of Phantom Chorus on Well-Being; Phantom Chorus Treatment on (“Treatment”) or off (“Control”)",
       x = "Treatment", y = "Perceived Restoration Score") +
  theme_minimal() +
  scale_fill_manual(values = c("#66c2a5", "#fc8d62")) +  # Soft, appealing colours
  theme(legend.position = "top", 
        plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
        plot.subtitle = element_text(hjust = 0.5, size = 12))

# Viz 2: Bar Plot of Perceived Bird Diversity by Treatment and Trail
survey_summary <- survey_data %>%
  group_by(trail, treatment, DIV_NUMTYPE) %>%
  summarise(count = n(), .groups = "drop") %>%
  group_by(trail, treatment) %>%
  mutate(proportion = count / sum(count))

ggplot(survey_summary, aes(x = DIV_NUMTYPE, y = proportion, fill = treatment)) +
  geom_bar(stat = "identity", position = "dodge") +
  facet_wrap(~ trail) +
  labs(title = "Perceived Bird Diversity by Treatment and Trail",
       subtitle = "Effect of Phantom Chorus on Biodiversity Perception; Phantom Chorus Treatment on (“Treatment”) or off (“Control”)",
       x = "Perceived Number of Bird Types", y = "Proportion of Responses") +
  theme_bw() +
  scale_fill_manual(values = c("#8da0cb", "#e78ac3")) +  # Eye-catching palette
  theme(axis.text.x = element_text(angle = 45, hjust = 1, face = "bold"),
        plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
        plot.subtitle = element_text(hjust = 0.5, size = 12))

# Viz 3: Scatter Plot of Restoration Components by Treatment
survey_long <- survey_data %>%
  pivot_longer(cols = c(Sound_Coh, Sound_Fasc, Sound_Comp), 
               names_to = "Component", values_to = "Score")

ggplot(survey_long, aes(x = Perceivedrestoration, y = Score, color = Component, shape = treatment)) +
  geom_point(size = 3, alpha = 0.8) +
  facet_wrap(~ trail) +
  labs(title = "Restoration Components vs. Overall Restoration",
       subtitle = "Exploring Coherence, Fascination, and Compatibility; Phantom Chorus Treatment on (“Treatment”) or off (“Control”)",
       x = "Perceived Restoration Score", y = "Component Score") +
  theme_minimal() +
  scale_color_manual(values = c("#a6cee3", "#1f78b4", "#b2df8a")) +  # Harmonious colours
  theme(legend.position = "top",  
        plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
        plot.subtitle = element_text(hjust = 0.5, size = 12))