Preliminaries

Load libraries

library(knitr)       #for R Markdown
library(lubridate)   #for time/date variable conversions
library(circular)    #for circular angle variable conversion
library(tidyverse)   #for data processing, put last to avoid function masking

Data processing

data.raw <- read.csv("tadpoledata_041426.csv",
                     fileEncoding="UTF-8-BOM", 
                     na.strings = "")

#clean up
data <- data.raw %>%
  
  #drop trailing NA reads
  select(where(~ !all(is.na(.x)))) %>%
  
  #correct variable types
  mutate_at(c("tad_ID","species","morph","GS","pair_ID","parent_IDs"), as.factor) %>%
  mutate_at(c("found_date","test_date"), mdy) %>%
  mutate(test_time = hm(test_time)) %>%
  mutate(response_time = period_to_seconds(ms(response_time))) 
  

#make long format for graphing
data_long <- data %>%
  
  pivot_longer(
    cols = starts_with(c("angle_", "movements_")),
    names_to = c(".value", "exp_stage"),
    names_sep = "_"
  ) %>%
  mutate(angle = circular(angle, units = "degrees", template = "none"))

Figures

Movement across periods

Fig_mov <- 
  ggplot(data_long, aes(x = exp_stage, y = movements, group = tad_ID)) +
  geom_line(color = "grey80", size = 1, position = position_dodge(width = 0.3))+
  stat_summary(aes(group = 1), fun = mean, geom = "line", color = "coral2", size = 1.5) +
  stat_summary(aes(group = 1), fun = mean, geom = "point", color = "coral2", size = 4) +
  stat_summary(aes(group = 1), fun.data = mean_se, geom = "errorbar", color = "coral2", width = 0.2, size = 1.5) +
  labs(x = "Experiment Stage", y = "Angle change/min") +
  scale_x_discrete(
    limits = c("ws1", "b1", "b2", "b3", "ws2"),
    labels = c("Pre-test", "B1", "B2", "B3", "Post-test")
  )+
  
  theme_classic(base_size = 20)

Fig_mov

# use this to save your figure
ggsave("Fig_mov.tiff", Fig_mov, width = 4, height = 7, dpi = 500)

Average angle

# set colors for each stage
stage_colors <- c("ws1" = "grey70", "b1" = "aquamarine4", "b2" = "coral2", 
                  "b3" = "#377EB8", "ws2" = "grey70")


# calculate circular means per stage
data_means <- data_long %>%
  group_by(exp_stage) %>%
  summarize(mean_angle = mean.circular(angle))
#define file that you want to save
#can't use ggplot here since they don't have build in for circular graph so have to do this the annoying way

# tiff("Fig_angle.tiff", width = 7, height = 5, units = "in", res = 500)

# Base plot
plot(data_long$angle, 
     stack = FALSE, 
     bins = 360, 
     sep = 0.05, 
     axes = FALSE)  

# Overlay colored individual points
for(stage in names(stage_colors)) {
  stage_data <- data_long %>% filter(exp_stage == stage)
  points(stage_data$angle, 
         stack = TRUE, 
         bin = 360,      # Smoothness of stacking
         col = stage_colors[stage], 
         pch = 16,       # Solid dot
         cex = 1.2)      # Dot size
}


# Add the mean arrows
for(i in 1:nrow(data_means)) {
  current_stage <- data_means$exp_stage[i]
  
  # arrows.circular draws from center to the mean angle
  arrows.circular(data_means$mean_angle[i], 
                  col = stage_colors[[current_stage]], 
                  lwd = 4,      #width
                  length = 0.1) #arrowhead size
}

# Add a legend
legend("topright", legend = c("Pre-test","B1","B2","B3","Post-test"), 
       fill = stage_colors)

# dev.off()