Chapter 13 Merry Christmas!!!

You have all done fanatically well in semester A so I have got you all a lovely Christmas gift… Fully commented and completed versions of the scripts you have been working on. I would still suggest you try to complete the workbook in your own time, this will mean that you have a much better understanding of how the code works and will be better for you in the long run. But these commented and completed scripts may give you something to compare your own work too and will hopefully allow us all to start on the same page in the New Year.

Show me the Tribolium parasites script
# An analysis of parasite abundance between male and female tribolium beetles from two thermal treatments (30 and 35 degrees centigrade)
# Ellen Bell 
# 08/08/2022

library(tidyverse) # Loads the tidyverse package
library(janitor) # Loads the janitor package

# Read the data from the .csv file in the data folder into my project
tribolium <- read_csv("data/Tribolium_parasites.csv") #

head(tribolium)

# First of all lets create some variables
beetle_id <- c(1:20) # Notice that this notation will create of a list of numbers 1 to 20
sex <- c("F","F","F","F","F","M","M","M","M","M","F","F","F","F","F","M","M","M","M","M")
treatment <- c(35,35,35,35,35,35,35,35,35,35,30,30,30,30,30,30,30,30,30,30)

# Turning variables into a tibble
tribolium_extras <- tibble(beetle_id,sex,treatment)

# Renaming some variables in your original data set
tribolium <- tribolium %>%
  rename(beetle_id = beetle_id)
tribolium <- tribolium %>%
  rename(wing_case_size = wing_case_size)
tribolium <- tribolium %>%
  rename(parasite_burden = parasite_burden)

# Check how your data frame now looks
head(tribolium)

# Merge data frames
tribolium_combo <- merge(tribolium, tribolium_extras, by = "beetle_id") # This merge function combines both of your data frames but matches rows by beetle_id

glimpse(tribolium_combo)

# Save your combined data frame as a .csv file (which you could download) to your data folder
write.csv(tribolium_combo,"data/tribolium_combo.csv", row.names = FALSE)

# Use the n_distinct function to count unique/distinct entries in the beetle_id column
n_distinct(tribolium_combo$beetle_id)

# Use the tabyl function from the janitor package, comment on what it does. 
tabyl(tribolium_combo, sex , treatment)

# Making a histogram and saving it in an object
# Call the ggplot function and direct it to your data and define your x axis
size_histogram <- ggplot(data = tribolium_combo,aes(x = wing_case_size)) + 
  geom_histogram(bins = 6) # Tell ggplot that you want it to build a histogram with 6 equal sized bins
print(size_histogram) # Print your new figure

parasite_burden_histogram <- ggplot(data = tribolium_combo,aes(x = parasite_burden)) + 
  geom_histogram(bins = 6) # Tell ggplot that you want it to build a histogram with 6 equal sized bins
print(parasite_burden_histogram) # Print your new figure

# Saving outputs
ggsave("figures/tribolium_wingcase_size_histogram.pdf", # Give R a path to save to and a file name
       plot = size_histogram, # Tell R what to save - in this case your object
       device = "pdf") # Tell R what file type to create, in this case a pdf
ggsave("figures/tribolium_parasite_burden_histogram.pdf", # Give R a path to save to and a file name
       plot = parasite_burden_histogram, # Tell R what to save - in this case your object
       device = "pdf") # Tell R what file type to create, in this case a pdf

# Making a box plot and saving it in an object
# Call the ggplot function and direct it to your data and define your x axis and y axis
size_differences_by_sex_boxplot <- ggplot(data = tribolium_combo,aes(x = sex, y = wing_case_size)) + 
  geom_boxplot() # Tell ggplot that you want it to build a boxplot
print(size_differences_by_sex_boxplot) # Print your new figure

# Saving outputs
ggsave("figures/size_differences_by_sex_boxplot.pdf", # Give R a path to save to and a file name
       plot = size_differences_by_sex_boxplot, # Tell R what to save - in this case your object
       device = "pdf") # Tell R what file type to create, in this case a pdf

# Instruct R to treat the treatment variable as a factor 
tribolium_combo$treatment <- as.factor(tribolium_combo$treatment) # Note this will edit your data frame

# Making a box plot and saving it in an object
# Call the ggplot function and direct it to your data and define your x axis and y axis
size_differences_by_treatment_boxplot <- ggplot(data = tribolium_combo,aes(x = treatment, y = wing_case_size)) + 
  geom_boxplot() # Tell ggplot that you want it to build a boxplot
print(size_differences_by_treatment_boxplot) # Print your new figure

# Saving outputs
ggsave("figures/size_differences_by_treatment_boxplot.pdf", # Give R a path to save to and a file name
       plot = size_differences_by_treatment_boxplot, # Tell R what to save - in this case your object
       device = "pdf") # Tell R what file type to create, in this case a pdf

# Making a box plot and saving it in an object
# Call the ggplot function and direct it to your data and define your x axis and y axis
size_differences_by_treatment_and_sex_boxplot <- ggplot(data = tribolium_combo,aes(x = treatment, y = wing_case_size, fill = sex)) + 
  geom_boxplot() # Tell ggplot that you want it to build a boxplot
print(size_differences_by_treatment_and_sex_boxplot) # Print your new figure

# Saving outputs
ggsave("figures/size_differences_by_treatment_and_sex_boxplot.pdf", # Give R a path to save to and a file name
       plot = size_differences_by_treatment_and_sex_boxplot, # Tell R what to save - in this case your object
       device = "pdf") # Tell R what file type to create, in this case a pdf

# Making a scatter plot and saving it in an object
# Call the ggplot function and direct it to your data and define your x axis and y axis
size_vs_parasites_point <- ggplot(data = tribolium_combo,aes(x = wing_case_size , y = parasite_burden )) + 
  geom_point() + # geom_point is ggplots scatter plot
  geom_smooth(method="lm")
print(size_vs_parasites_point)

# Saving outputs
ggsave("figures/size_vs_parsite_point.pdf", # Give R a path to save to and a file name
       plot = size_vs_parasites_point, # Tell R what to save - in this case your object
       device = "pdf") # Tell R what file type to create, in this case a pdf
Show me the ggplot fundamentals script
# ggplot fundamentals  
# Ellen Bell 
# 08/08/2022

# Clean up your environment
rm(list = ls())

# Reload your tribolium_combo data frame
tribolium_combo <- read_csv("data/tribolium_combo.csv")

# Instruct R to treat the treatment variable as a factor 
tribolium_combo$treatment <- as.factor(tribolium_combo$treatment) # Note this will edit your data frame

# Making a box plot and saving it in an object
# Call the ggplot function and direct it to your data and define your x axis and y axis
size_differences_boxplot <- ggplot(data = tribolium_combo, aes(x = sex, y = wing_case_size)) + 
  geom_boxplot(aes(fill = treatment), width = 0.9) + # Tell ggplot that you want it to build a boxplot
  labs(x = "Sex", y = "Wing case size (mm)\n") + # Adjust your x and y axis labels 
  scale_x_discrete(labels = c("Female","Male")) + # Rename the categories on the x axis 
  scale_fill_manual(labels = c("30°C", "35°C"), values = c("cornflowerblue", "coral")) + # Rename your treatments
  theme_bw() +
  theme(panel.border = element_rect(color="black"), # Specifies that the plot boarder is coloured black
        panel.grid.minor = element_blank(), # Removes minor grid lines 
        panel.grid.major = element_blank(), # Removes major grid lines 
        axis.text = element_text(size = 15), # Changes the size of text on both axis 
        axis.title = element_text(size = 20), # Changes size of your axis labels 
        legend.text = element_text(size = 15), # Changes the size of text within your legend
        legend.title = element_text(size = 20)) # Changes the size of the legend title  
print(size_differences_boxplot) # Print the object your plot is stored in to view it
Show me some examples of scripts for pretty figures
# Pretty figure examples 
# Ellen Bell 
# 08/08/2022

# Clean up your environment
rm(list = ls())

# Reload your tribolium_combo data frame
tribolium_combo <- read_csv("data/tribolium_combo.csv")

# Instruct R to treat the treatment variable as a factor 
tribolium_combo$treatment <- as.factor(tribolium_combo$treatment) # Note this will edit your data frame

# Make a pretty histogram 

# Making a histogram and saving it in an object
# Call the ggplot function and direct it to your data and define your x axis
size_histogram <- ggplot(data = tribolium_combo,aes(x = wing_case_size)) + 
  geom_histogram(bins = 6, fill="cornflowerblue", color="cornflowerblue") + # Tell ggplot that you want it to build a histogram with 6 equal sized bins
  labs(x = "\nWing Case Size (mm)", y = "Frequency\n") + # Adjust your x and y axis labels 
  theme_bw() +
  theme(panel.border = element_rect(color="black"), # Specifies that the plot boarder is coloured black
        panel.grid.minor = element_blank(), # Removes minor grid lines 
        panel.grid.major = element_blank(), # Removes major grid lines 
        axis.text = element_text(size = 15), # Changes the size of text on both axis 
        axis.title = element_text(size = 20), # Changes size of your axis labels 
        legend.text = element_text(size = 15), # Changes the size of text within your legend
        legend.title = element_text(size = 20)) # Changes the size of the legend title  
print(size_histogram) # Print your new figure

ggsave("figures/pretty_size_histogram.pdf", # Give R a path to save to and a file name
       plot = size_histogram, # Tell R what to save - in this case your object
       width = 15, # Set .pdf width
       height = 10, # Set .pdf height
       units = "cm", # Specify units for .pdf width and height
       device = "pdf") # Tell R what file type to create, in this case a pdf
# Note, use trial and error to select a good width and height for your figure

# Make a pretty boxplot

# Making a box plot and saving it in an object
# Call the ggplot function and direct it to your data and define your x axis and y axis
size_differences_boxplot <- ggplot(data = tribolium_combo, aes(x = treatment, y = wing_case_size)) + 
  geom_boxplot(aes(fill = sex), width = 0.9) + # Tell ggplot that you want it to build a boxplot
  labs(x = "\nTreatment", y = "Wing case size (mm)\n") + # Adjust your x and y axis labels 
  scale_x_discrete(labels = c("30°C", "35°C")) + # Rename the categories on the x axis 
  scale_fill_manual(name = "Sex", labels = c("Female","Male"), values = c("cornflowerblue", "coral")) + # Rename your treatments
  theme_bw() +
  theme(panel.border = element_rect(color="black"), # Specifies that the plot boarder is coloured black
        panel.grid.minor = element_blank(), # Removes minor grid lines 
        panel.grid.major = element_blank(), # Removes major grid lines 
        axis.text = element_text(size = 15), # Changes the size of text on both axis 
        axis.title = element_text(size = 20), # Changes size of your axis labels 
        legend.text = element_text(size = 15), # Changes the size of text within your legend
        legend.title = element_text(size = 20)) # Changes the size of the legend title  
print(size_differences_boxplot) # Print the object your plot is stored in to view it

ggsave("figures/pretty_tribolium_wingcase_size_boxplot.pdf", # Give R a path to save to and a file name
       plot = size_differences_boxplot, # Tell R what to save - in this case your object
       width = 15, # Set .pdf width
       height = 10, # Set .pdf height
       units = "cm", # Specify units for .pdf width and height
       device = "pdf") # Tell R what file type to create, in this case a pdf
# Note, use trial and error to select a good width and height for your figure

# Make a pretty scatter graph

# Making a scatter plot and saving it in an object
# Call the ggplot function and direct it to your data and define your x axis and y axis
size_vs_parasites_point <- ggplot(data = tribolium_combo,aes(x = wing_case_size , y = parasite_burden )) + 
  geom_point(aes(shape = treatment, colour = sex)) + # geom_point is ggplots scatter plot
  geom_smooth(method="lm", colour = "cornflowerblue", fill = "lightblue", size = 1, linetype = "dashed") +
  labs(x = "\nWing Case Size (mm)", y = "Parasite Burden\n") + # Adjust your x and y axis labels 
  scale_color_manual(name = "Sex", labels = c("Female", "Male"), values = c("deepskyblue3", "coral")) +
  scale_shape_manual(name = "Treatment", labels = c("30°C", "35°C"), values = c(15,16)) +
  theme_bw() +
  theme(panel.border = element_rect(color="black"), # Specifies that the plot boarder is coloured black
        panel.grid.minor = element_blank(), # Removes minor grid lines 
        panel.grid.major = element_blank(), # Removes major grid lines 
        axis.text = element_text(size = 15), # Changes the size of text on both axis 
        axis.title = element_text(size = 20), # Changes size of your axis labels 
        legend.text = element_text(size = 15), # Changes the size of text within your legend
        legend.title = element_text(size = 20)) # Changes the size of the legend title  
print(size_vs_parasites_point)

ggsave("figures/pretty_tribolium_wingcase_size_parasite_scatter.pdf", # Give R a path to save to and a file name
       plot = size_vs_parasites_point, # Tell R what to save - in this case your object
       width = 25, # Set .pdf width
       height = 10, # Set .pdf height
       units = "cm", # Specify units for .pdf width and height
       device = "pdf") # Tell R what file type to create, in this case a pdf
# Note, use trial and error to select a good width and height for your figure