######################################################
#
# Haemoglobin and Elevation
#
######################################################

#### GRAEME'S ATTEMPT ####

blood <- read.table(file.choose(), header = T, sep = ",") # load the dataframe

summary(blood) # have a look at it 

blood$origin <- factor(blood$origin, levels = c("Tibet", "Andes", "Ethiopia", 
                                                "USA")) # re-order the four regions
                                                        # in terms of elevation

######## boxplot ########
boxplot(haemoglobin~origin, data = blood, xlab = "Region", 
        ylab = "haemoglobin concentration (g/dl)", col = "red", ylim = c(10,30))

summary(blood$origin)
text(1,27, labels="N = 59")
text(2,27, labels="N = 71")
text(3,27, labels="N = 128")
text(4,27, labels="N = 1704")
text(1,29, labels="4000m")
text(2,29, labels="4000m")
text(3,29, labels="3530m")
text(4,29, labels="0m")

######## violin plot ########
library(vioplot)
vioplot(haemoglobin~origin, data = blood, xlab = "Region", 
        ylab = "haemoglobin concentration (g/dl)", col = "red", ylim = c(10,30))

summary(blood$origin)
text(1,27, labels="N = 59")
text(2,27, labels="N = 71")
text(3,27, labels="N = 128")
text(4,27, labels="N = 1704")
text(1,29, labels="4000m")
text(2,29, labels="4000m")
text(3,29, labels="3530m")
text(4,29, labels="0m")


#### ROSALIND'S ATTEMPT ####

blood <- read.table(file.choose(), header = T, sep = ",") # load the data
View(blood) # view the data

blood$origin <- factor(blood$origin) # make origin a factor

andes<-subset(blood, origin=="Andes") # subset data by origin
tibet<-subset(blood, origin=="Tibet")
ethiopia<-subset(blood, origin=="Ethiopia")
usa<-subset(blood, origin=="USA")

boxplot(andes$haemoglobin,tibet$haemoglobin,
        ethiopia$haemoglobin,usa$haemoglobin, # plot subsets' haemoglobin 
                                              # in order of descending elevation
        xlab = "Residence", ylab = "Haemoglobin concentration (g/dl)", 
        names=c("Andes","Tibet","Ethiopia","USA"),
        col = c("red","red","hotpink","pink"), ylim = c(10,30),yaxs="i")
legend("topright", inset=0.01,title="Elevation:",legend=c("4000m","3530m","0m, sea level"),
       fill=c("red","hotpink","pink"),bty="n") # add a legend with elevation
                                               # data (extra variable)

summary(blood$origin)
n=c(length(andes$origin),length(tibet$origin),
    length(ethiopia$origin),length(usa$origin)) # create list of sample sizes
text(x= c(1,2,3,4), 
     y= c(max(andes$haemoglobin)+2,max(tibet$haemoglobin)+2,
        max(ethiopia$haemoglobin)+2,max(usa$haemoglobin)+2), # add sample sizes to plot
                                                             # 2 g/dl above each maximum
     labels=paste("n=",n,sep=""),cex=0.9)


######################################################
#
# Nova Scotian Birds
#
######################################################

#### GRAEME'S ATTEMPT ####

birds <- read.table(file.choose(), header = T, sep = ",") # load the dataframe

summary(birds) # have a look at it 

length(birds$species_reported)

allbirds <- aggregate(species_reported ~ area, data = birds, FUN = sum)
allbirds

neworder <- with(allbirds, reorder(area, -species_reported))
neworder

abline(h=(seq(0,400,20)), col="lightgray", lty=1)
abline(h=(seq(0,400,100)), col="darkgray", lty=3)
abline(h=0, col="darkgray", lty=1, lwd=3)
par(new=TRUE)

barplot(species_reported ~ neworder, data = allbirds,
        col="cornflowerblue",
        ylim=c(0,400),las=1,
        names.arg=c("Wolfville", "Broad \nCove", "Sheet \nHarbour",
                    "Apple \nRiver", "Truro","Tata- \ngouche", "Spring- \nville"), cex.names = 0.75,
        ylab="Total species count", xlab="Area")

birds <- read.table(file.choose(), header = T, sep = ",")

birds$year <- factor(birds$year)
birds2014 <- subset(birds, year == '2014')
birds2015 <- subset(birds, year == '2015')
birds2016 <- subset(birds, year == '2016')
birds2017 <- subset(birds, year == '2017')
birds2018 <- subset(birds, year == '2018')

allyears <- rbind(birds2014$species_reported, birds2015$species_reported,
                  birds2016$species_reported, birds2017$species_reported,
                                  birds2018$species_reported)

cols <- c("cornflowerblue", "yellow", "red", "magenta", "green")

barplot(allyears, beside = TRUE, 
        ylim=c(0,80),las=1)
abline(h=(seq(0,80,2)), col="lightgray", lty=1)
abline(h=(seq(0,80,10)), col="darkgray", lty=3)
abline(h=0, col="darkgray", lty=1, lwd=3)

legend("top",legend=c("2014","2015","2016","2017","2018"),
       fill=cols, bty = "n", cex = 0.6)

par(new=TRUE)

barplot(allyears, beside = TRUE,
        col = cols,
        ylim=c(0,80),las=1,
        names.arg=c("Apple \nRiver", "Broad \nCove", "Sheet \nHarbour",
                    "Spring- \nville","Tata- \ngouche", "Truro", "Wolfville"), cex.names = 0.65,
        ylab="Annual species count", xlab="Area")


#### ROSALIND'S ATTEMPT ####

birds <- read.table(file.choose(), header = T, sep = ",") # load the data
View(birds) # view the data

birds$area <- factor(birds$area) # make area a factor

trimmedbirds<-subset(birds,area=="Apple_River"|area=="Broad_Cove"|
                             area=="Tatamagouche"|area=="Wolfville") # subset 4 areas

trimmedbirds$area <- factor(trimmedbirds$area) # drop unused 'area' levels

areas<-c("Apple River","Broad Cove","Tatamagouche","Wolfville") # create list of area names

boxplot(trimmedbirds$species_reported~trimmedbirds$area, # plot boxplot of data
        ylim=c(30,80),yaxs = "i", # set y-axis limits
        ylab="Species reported", xlab="Area", # set axis labels
        col="seagreen1", # choose colour
        names=areas, # use list of areas as x-axis tick labels
        boxwex=0.5, # make boxes narrower with scaling factor
        at=1.4:4.4) # position boxes slightly to the right along the x-axis

year1<-subset(trimmedbirds,year==2014) # subset data by year
year2<-subset(trimmedbirds,year==2015)
year3<-subset(trimmedbirds,year==2016)
year4<-subset(trimmedbirds,year==2017)
year5<-subset(trimmedbirds,year==2018)

stripchart(year1$species_reported ~year1$area, # add strip-charts of year 1 data
           ylim=c(30,80),yaxs = "i", # set y-axis limits
           method="jitter", # jitter the points
           jitter=0.1, # set amount of jitter
           vertical=TRUE, # draw strip-charts vertically rather than horizontally
           ylab = "", # do not include y-axis label
           pch=15, # set point shape
           col="firebrick4", # set point colour
           add=TRUE) # add strip-charts onto the existing plot
stripchart(year2$species_reported ~year2$area, ylim=c(30,80),yaxs = "i",
           method="jitter",jitter=0.11,vertical=TRUE,ylab = "",
           pch=16,col="hotpink",add=TRUE) # add year 2 data
stripchart(year3$species_reported ~year3$area, ylim=c(30,80),yaxs = "i",
           method="jitter",jitter=0.12,vertical=TRUE,ylab = "",
           pch=17,col="forestgreen",add=TRUE) # add year 3 data
stripchart(year4$species_reported ~year4$area, ylim=c(30,80),yaxs = "i",
           method="jitter",jitter=0.13,vertical=TRUE,ylab = "",
           pch=18,col="royalblue",add=TRUE) # add year 4 data
stripchart(year5$species_reported ~year5$area, ylim=c(30,80),yaxs = "i",
           method="jitter",jitter=0.14,vertical=TRUE,ylab = "",
           pch=19,col="darkorange1",add=TRUE) # add year 5 data

legend("bottomright", # add legend to bottom right of plot
       title="Year:", # include legend title
       legend=c(2014,2015,2016,2017,2018), # assign names to strip-chart samples
       inset=0.03, # position legend slightly in from the bottom right
       pch=15:19, # list point shapes in order of sample names
       col=c("firebrick4","hotpink","forestgreen",
                "royalblue","darkorange1")) # list point colours
                                            # in order of sample names

######################################################
#
# Caffeine and Finger Tapping
#
######################################################

#### GRAEME'S ATTEMPT ####

tapping <- read.table(file.choose(), header = T, sep = ",")
head(tapping)
summary(tapping)

tapping$participant <- factor(tapping$participant)
tapping$caffeine_mg <- factor(tapping$caffeine_mg)

gamers <- subset(tapping, partipant = "gamer")
non <- subset(tapping, partipant = "non-gamer")

boxplot(gamers$taps~gamers$caffeine_mg, 
        ylab = "Number of taps", xlab = "Caffeine (mg)",
        ylim=c(230,260),xlim=c(0.7,3.3), xaxs= "i",
        yaxs = "i", xaxt="n",col="gray", 
        boxwex=0.27,at = 1:3 - 0.15)
par(new=TRUE)
boxplot(non$taps~non$caffeine_mg, 
        ylab = "Number of taps", xlab = "Caffeine (mg)",
        ylim=c(230,260),xlim=c(0.7,3.3),
        yaxs = "i",col="green", 
        boxwex=0.27,at = 1:3 + 0.15)

legend("bottomright", legend=c("Gamers","Non-gamers"),
       fill=c("gray","green"), bty = "n")


#### ROSALIND'S ATTEMPT ####

tapping <- read.table(file.choose(), header = T, sep = ",") # load the data
View(tapping) # view the data

tapping$participant <- factor(tapping$participant) # make participant a factor
tapping$caffeine_mg <- factor(tapping$caffeine_mg) # make caffeine level a factor

gamers <- subset(tapping, participant == "gamer") # subset 'gamers'
non <- subset(tapping, participant == "non-gamer") # subset 'non-gamers'

boxplot(gamers$taps~gamers$caffeine_mg, 
        ylab = "Number of taps", xlab = "Caffeine (mg)", # set axis labels
        ylim=c(235,260),xlim=c(0.7,3.3), xaxs= "i",yaxs = "i", # set axis limits
        col="gray", 
        boxwex=0.27, # make boxes narrower with scaling factor
        at = 1:3 - 0.15) # position boxes slightly to the left along the x-axis
par(new=TRUE)
boxplot(non$taps~non$caffeine_mg, 
        ylab = "", xlab = "", # keep axis labels blank
        ylim=c(235,260),xlim=c(0.7,3.3),yaxs = "i", # set axis limits
        col="green", 
        xaxt="n",yaxt="n", # do not draw axes 
        boxwex=0.27, # make boxes narrower with scaling factor
        at = 1:3 + 0.15) # position boxes slightly to the right along the x-axis

legend("bottomright", inset=0.02,legend=c("Gamers","Non-gamers"),
       fill=c("gray","green"), bty = "n")