Question 1: For this exercise, use your newly-developed ggplot chops to create some nice graphs from your own data (If you do not have a good data frame to use for graphics, use one of the many built-in data frames from R (other than mpg, which we are using in class)). Experiment with different themes, theme base sizes, aesthetics, mappings, and faceting. When you are finished, try exporting them to high quality pdfs, jpgs, eps files, or other formats that you would use for submission to a journal.

To start, I loaded in the necessary libraries, set up some paths that I will use, and read in the data files I may want to use (actually using the same code I used in hmwk 11!).

#Initialize -----------------------------------------
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.1.3
## Warning: package 'tibble' was built under R version 4.1.3
## Warning: package 'tidyr' was built under R version 4.1.3
## Warning: package 'readr' was built under R version 4.1.3
## Warning: package 'purrr' was built under R version 4.1.3
## Warning: package 'dplyr' was built under R version 4.1.3
## Warning: package 'stringr' was built under R version 4.1.3
## Warning: package 'forcats' was built under R version 4.1.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggplot2)
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.1.3
library(patchwork)
## Warning: package 'patchwork' was built under R version 4.1.3
# set.seed(1234)

#library(TeachingDemos)
#char2seed('espress withdrawl')
#char2seed('espress withdrawl', set=FALSE)

# Load functions--------------------------------------


# Global Variables-------------------------------------
path.root <- 'D:/OneDrive - University of Vermont/Classes/Spring2022/CompBio/Lab/Bio381Scott'
path.data <- paste(path.root,'/hmwk11Data',sep='')
path.graphs <-paste(path.root,'/graphs',sep='')
setwd(path.root)
file_names <- list.files(path=path.data)
head(file_names)
## [1] "DMC.master.csv"  "P1S1 master.csv" "RawData.csv"
#for loop to read in CSV files)
setwd(path.data)
list <- list()
for(i in 1:length(file_names)){
  file.name <- file_names[i]
  list[[i]] <- read.csv(file = file.name)
}
names(list) <- c('Exp3','Exp1','Exp2')
list2env(list,.GlobalEnv)
## <environment: R_GlobalEnv>

Then I start with just a super simple graph:

#making simple plots w/ exp1#
exp1Plot <- ggplot(data = Exp1,aes(x = as.factor(Density), y = Rafts, fill = as.factor(Density)))
simpPlot <- exp1Plot +
  geom_bar(stat = 'summary')+
  theme_classic()+
  ggtitle('Simple Plot')
simpPlot
## No summary function supplied, defaulting to `mean_se()`

Then I add some complexity by modifying font size

#modify font + font size
modFont<- exp1Plot+
  geom_bar(stat = 'summary')+
  theme_classic(base_size = 20, base_family = 'serif')+
  ggtitle('Modify Font Sizes')
modFont
## No summary function supplied, defaulting to `mean_se()`

Then I try to make the axis more understandable

#change axis labels
changAxis <- exp1Plot+
  geom_bar(stat = 'summary')+
  labs(x = 'larval density', y = 'Average egg rafts')+
  theme_classic(base_size = 20, base_family = 'serif')+
  ggtitle('Change Axis Titles')
changAxis
## No summary function supplied, defaulting to `mean_se()`

Then I play around with color

#custom colour pallete
custCol <- exp1Plot+
  geom_bar(stat = 'summary')+
  labs(x = 'larval density', y = 'Average egg rafts')+
  theme_classic(base_size = 20, base_family = 'serif')+
  scale_fill_manual(values = c('gray71', 'grey30','grey30','grey30'))+
  ggtitle('Custom Colour Pallete')
custCol
## No summary function supplied, defaulting to `mean_se()`

Then, for my final trick, I wanted to practice with facet. But the data set I was using really only had one predictor variable, that being density. So, I made up another predictor. I simulated a temperature variable. Then created plots for each of the different density variables with temperature as the predictor and number of egg rafts as the response

#Okay. We've had some fun with colors, not lets use facet
#to start, I'm gonna try to make up some data on a variable we'll call temperature
temp <- rnorm(n=200,mean=37,sd=3) #creat fake temp variable
exp1.2 <- cbind(Exp1,temp) #adding temp variable to our df

#okay so now lets try to plot
exp1.2Plot <- ggplot(exp1.2,aes(x=temp, y=Rafts))
facetPlot<-exp1.2Plot+
  geom_line()+
  facet_grid(as.factor(Density)~.,scales='free')+
  ggtitle('Facet Plots')
facetPlot

But we’re still not over. We have to save the plots. And I could just use ggsave for each plot. But I wanna be super cool and assert my R dominance. So I created a list + a for loop to save everything .

setwd(path.graphs)
savePlots <- list(simpPlot,modFont,changAxis,custCol,facetPlot)
names(savePlots) <- c('simpPlot','modFont','changAxis','custCol','facetPlot')
for(i in 1:length(savePlots)){
  ggsave(filename = paste(names(savePlots[i]),sep = '','.png'),plot = savePlots[[i]], device = 'png')
}
## Saving 7 x 5 in image
## No summary function supplied, defaulting to `mean_se()`
## Saving 7 x 5 in image
## No summary function supplied, defaulting to `mean_se()`
## Saving 7 x 5 in image
## No summary function supplied, defaulting to `mean_se()`
## Saving 7 x 5 in image
## No summary function supplied, defaulting to `mean_se()`
## Saving 7 x 5 in image

And there you have it! I’m a ggplot pro.