#################################
##                             ##
## CHAPTER 5: MULTIPLE SAMPLES ##
##                             ##
#################################

###########################################################################

##    AN EXAMPLE OF   ##
##  MULTIPLE-SAMPLES  ##
##         DATA       ##

piglets <- read.table(file.choose(), header = T, sep = ",") # load the data in from the Excel CSV. file
                                                            # 'pig_litters' and name it 'piglets'
View(piglets) # view the data

cleanpiglets <- lapply(piglets, function(col)col[!is.na(col)]) # remove NA values
View(cleanpiglets) # view the cleaned data

###########################################################################

##            ##
##  BOXPLOTS  ##
##            ##

### Simple boxplot

boxplot(cleanpiglets, # plot the data as a boxplot
        ylab = "Birth weight (lb)", # set y-axis label
        ylim=c(0,5), yaxs = "i", # specify y-axis limits
        col="plum") # set colour

### Refined boxplot

lit1<-cleanpiglets$litter1 # give each sample to plot a convenient name
lit2<-cleanpiglets$litter2
lit3<-cleanpiglets$litter3
lit4<-cleanpiglets$litter4
lit5<-cleanpiglets$litter5
lit6<-cleanpiglets$litter6
lit7<-cleanpiglets$litter7

n <- c(length(lit1),length(lit2),length(lit3),length(lit4),
       length(lit5),length(lit6),length(lit7)) # create list of sample sizes

n # view n

text(x= c(1,2,3,4,5,6,7), # add text at x-coordinates 1-7
     y= 0.5, # set y-coordinate
     labels= c(n), # use the list of sample sizes as the text
     col= "purple4") # set text colour

switcheddata <- cleanpiglets[c(2,6,7,3,4,5,1)] # switch the order of the samples in the data,
                                               # ordering by increasing sample size
View(switcheddata) # view the switched data

switchedn<- n[c(2,6,7,3,4,5,1)] # switch the order of the sample sizes list

boxplot(switcheddata, # plot the switched order data as a boxplot
        ylab = "Birth weight (lb)", # set y-axis label
        ylim=c(0,5), yaxs = "i", # specify y-axis limits
        col="plum") # set colour

## Add the sample sizes in the new order:
text(x= c(1,2,3,4,5,6,7), # add text at x-coordinates 1-7
     y= 0.5, # set y-coordinate
     labels= c(switchedn), # use the list of switched sample sizes as the text
     col= "purple4") # set text colour

boxplot(switcheddata,
        ylab = "Birth weight (lb)", 
        ylim=c(0,5), yaxs = "i", 
        names=c("Litter 2", "Litter 6", "Litter 7", "Litter 3", "Litter 4", 
                "Litter 5", "Litter 1"), # re-name x-axis tick labels
        col="plum")
text(x= c(1,2,3,4,5,6,7), y= 0.5, labels= c(switchedn), col= "purple4")

###########################################################################

##              ##
##  HISTOGRAMS  ##
##              ##

### Multiple histogram plots

par(mfrow=c(3,1)) # arrange 3 plots in 3 rows with 1 column
hist(lit1, # plot the data for litter 1 as a histogram
     col="darkgoldenrod1", # set colour
     xlim=c(1,5), xaxs = "i", ylim=c(0,4), yaxs = "i", # set axis limits
     xlab="Birth weight (lb)", # set x-axis label
     main="Litter 1") # include the sample as a title
hist(lit4, col="darkgoldenrod1",xlim=c(1,5), xaxs = "i", ylim=c(0,4), yaxs = "i", 
     xlab="Birth weight (lb)",main="Litter 4") # the same for litter 4
hist(lit7, col="darkgoldenrod1",xlim=c(1,5), xaxs = "i", ylim=c(0,4), yaxs = "i", 
     xlab="Birth weight (lb)",main="Litter 7") # the same for litter 7

par(mfrow=c(1,3)) # arrange 3 plots in 1 row with 3 columns
hist(lit1, col="darkgoldenrod1",xlim=c(1,5), xaxs = "i", ylim=c(0,4), yaxs = "i", 
     xlab="Birth weight (lb)",main="Litter 1")
hist(lit4, col="darkgoldenrod1",xlim=c(1,5), xaxs = "i", ylim=c(0,4), yaxs = "i", 
     xlab="Birth weight (lb)",main="Litter 4")
hist(lit7, col="darkgoldenrod1",xlim=c(1,5), xaxs = "i", ylim=c(0,4), yaxs = "i", 
     xlab="Birth weight (lb)",main="Litter 7")

### Overlaying histograms in a single plot

par(mfrow=c(1,1)) # return to plotting in 1 row, 1 column

hist(lit1, # plot this sample as a histogram
     breaks=c(seq(1,5,0.5)), # specify bins
     col="yellow2", # set a colour for this sample
     xlim=c(1,5), xaxs = "i", ylim=c(0,4), yaxs = "i", # set axis limits
     xlab="Birth weight (lb)", # set x-axis label
     main=NULL) # do not include a title
par(new=TRUE) # prepare to draw on top of existing plot
hist(lit4, # plot this sample as a histogram
     breaks=c(seq(1,5,0.5)), # specify bins
     col="springgreen3", # set a colour for this sample
     xlim=c(1,5), xaxs = "i", ylim=c(0,4), yaxs = "i", # set axis limits
     xlab="",ylab="", # keep axis labels blank
     main=NULL) # do not include a title
par(new=TRUE) # prepare to draw on top of existing plot
hist(lit7, # plot this sample as a histogram
     breaks=c(seq(1,5,0.5)), # specify bins
     col="royalblue3", # set a colour for this sample
     xlim=c(1,5), xaxs = "i", ylim=c(0,4), yaxs = "i", # set axis limits
     xlab="",ylab="", # keep axis labels blank
     main=NULL) # do not include a title

legend("topright", # add a legend in the top right of the plot
       title="Litter", # set legend title
       legend=c("1","4","7"), # define sample names
       fill=c("yellow2","springgreen3","royalblue3"), # set legend colours in the 
                                                      # order of the sample names
       bty="n") # do not encase legend in a box

install.packages("yarrr") # install package to edit transparency of colours
library(yarrr) # activate package

hist(lit1, breaks=c(seq(1,5,0.5)), 
     col=transparent(orig.col = "yellow2", trans.val = 0.4), # set colour to 40% transparent yellow2
     xlim=c(1,5),xaxs = "i", ylim=c(0,4), yaxs = "i", xlab="Birth weight (lb)", main=NULL)
par(new=TRUE)
hist(lit4, breaks=c(seq(1,5,0.5)),
     col=transparent(orig.col = "springgreen3", trans.val=0.6), # set colour to 60% transparent springgreen3
     xlim=c(1,5), xaxs = "i", ylim=c(0,4), yaxs = "i", xlab="",ylab="",main=NULL)
par(new=TRUE)
hist(lit7, breaks=c(seq(1,5,0.5)),
     col=transparent(orig.col = "royalblue3", trans.val=0.8), # set colour to 80% transparent royalblue3
     xlim=c(1,5), xaxs = "i", ylim=c(0,4), yaxs = "i", xlab="",ylab="",main=NULL)

legend("topright",title="Litter", c("1","4","7"), 
       fill=c(transparent(orig.col = "yellow2", trans.val = 0.4),
              transparent(orig.col = "springgreen3", trans.val=0.6),
              transparent(orig.col = "royalblue3", trans.val=0.8)), # set legend colours in the 
                                                                    # order of the sample names
                                                                    # including transparency
       bty="n")

###########################################################################

##       GROUPED     ##
##  MULTIPLE-SAMPLES ##
##        DATA       ##

### An example of grouped multiple-samples data

# Load in the data set (seed_data.csv):
seeds <- read.table(file.choose(), header = T, sep = ",") # load the data in from the Excel CSV. file
                                                          # 'seed_data' and name it 'seeds'
View(seeds) # view the data

uncovered <- subset(seeds, treatment=="uncovered") # subset 'uncovered' data
covered <- subset(seeds, treatment=="covered") # subset 'covered' data

boxplot(uncovered$germinated~uncovered$water, # plot the 'uncovered' data
        ylab = "Seeds germinating per box", xlab = "Amount of water", # set axis labels
        ylim=c(0,100),yaxs = "i",xlim=c(0.7,5.3), xaxs= "i", # set axis limits
        xaxt="n", # do not draw x-axis
        col="gray", # set colour for 'uncovered' data
        boxwex=0.27, # make boxes narrower with scaling factor
        at = 1:5 - 0.15) # set where to position boxes along x-axis (slightly to the left)
par(new=TRUE) # prepare to draw on top of existing plot
boxplot(covered$germinated~covered$water, # plot the 'covered' data
        ylab = "", xlab = "", # keep axis labels blank
        ylim=c(0,100),  yaxs = "i", xlim=c(0.7,5.3),xaxs= "i", # set axis limits
        yaxt="n",xaxt="n", # do not draw axes
        col="yellow", # set colour for the 'covered' data
        boxwex=0.27, # make boxes narrower with scaling factor
        at = 1:5 + 0.15) # set where to position boxes along x-axis (slightly to the right)

axis(side=1, # draw an x-axis at the bottom of the plot
     at=1:5, # specify spacing of tick marks
     labels=c(1:5)) # label tick marks with numbers 1-5

legend("topleft", # add legend to top left of plot
       bty="n", # do not encase legend in box
       legend=c("Uncovered","Covered"), # define sample names
       fill=c("gray","yellow")) # set legend colours in the 
                                # order of the sample names




