3.21 Plotting Allele Frequency Trajectories in R

Here’s the code we worked on together in class to plot the trajectories and compare between populations and within each trial.

# load libraries
# install.packages("ggplot2") # install the first time 
library(ggplot2)

# load data from p1
p1_AF_trial1 <- read_csv("slim/p1_AF_trial1.csv")
p1_AF_trial2 <- read_csv("slim/p1_AF_trial2.csv")
p1_AF_trial3 <- read_csv("slim/p1_AF_trial3.csv")
p1_AF_trial4 <- read_csv("slim/p1_AF_trial4.csv")

# load data from p2
p2_AF_trial1 <- read_csv("slim/p2_AF_trial1.csv")
p2_AF_trial2 <- read_csv("slim/p2_AF_trial2.csv")
p2_AF_trial3 <- read_csv("slim/p2_AF_trial3.csv")
p2_AF_trial4 <- read_csv("slim/p2_AF_trial4.csv")

# create one file for all trials and all populations
af_trials_merged <- rbind(p1_AF_trial1, p1_AF_trial2, p1_AF_trial3, p1_AF_trial4, 
                          p1_AF_trial1, p1_AF_trial2, p2_AF_trial3, p2_AF_trial4)

# plot the four trials together 
# color by trial, line type by population 
ggplot(data = af_trials_merged, 
       aes(x = Generation, y = Allele.Frequency, color = as.factor(Trial))) + 
    geom_line(aes(linetype=as.factor(Population)))