#######################################################################
#######################################################################
#######################################################################
######                                                           ######
######             3rd VALUE training workshop, 2014             ######
######                                                           ######
######         Preparatory material for Rglimclim training:      ######
######                     self-study exercises                  ######
######                                                           ######
######              Analysis of Galapagos Islands data           ######
######                                                           ######
######  This script contains the commands used in a short        ######
######  (and non-definitive) analysis of the dataset on          ######
######  Galapagos islands tortoises that is considered in        ######
######                                                           ######
######  Faraway, J. (2005). Linear Models with R. Chapman &      ######
######   Hall / CRC Press, Boca Raton.                           ######
######                                                           ######
######  Actually, the data are provided in the "faraway" R       ######
######  package: if this package is installed, the data can      ######
######  be accessed here by typing                               ######
######                                                           ######
######  require(faraway); data(gala)                             ######
######                                                           ######
######  at the R prompt. However, (i) the "faraway" package is   ######
######  not installed by default (ii) it is helpful to learn     ######
######  how to read data from a file. The data here are          ######
######  therefore provided in the file galapagos.dat, which      ######
######  should be in the same directory as this script. The      ######
######  file is an ASCII file and is space-delimited. It         ######
######  contains data on seven variables for each of 30 islands. ######
######  The first row contains the variable names, which are as  ######
######  follows:                                                 ######
######                                                           ######
######  Species    - the number of species of tortoise found     ######
######               on the island                               ######
######  Endemics   - the number of endemic species               ######
######  Area       - the area of the island in square km         ######
######  Elevation  - the maximum height of the island in m       ######
######  Nearest    - the distance to the nearest other island    ######
######               in km                                       ######
######  Scruz      - the distance from Santa Cruz island in km   ######
######  Adjacent   - the area of the adjacent island in km^2     ######
######                                                           ######
######  The remaining rows contain the values of the seven       ######
######  variables for each island, along with the island names.  ######
######  Fields are separated by spaces and are not aligned       ######
######  in columns.                                              ######
######                                                           ######
#######################################################################
#######################################################################
#######################################################################
##
##	First step: read the data and have a look at them
##
tortoise.data <- read.table("galapagos.dat",header=TRUE)
tortoise.data
##
##	Alternatively, just look at the first few rows ...
##
head(tortoise.data)
##
##	... or the column names ...
##
names(tortoise.data)
##
##	... or look at some useful summaries of each variable.
##
summary(tortoise.data)
##
##	It might be useful to plot the data to visualise any 
##	relationships between variables ...
##
plot(tortoise.data)
##
##	Notice the outlying point on all of the graphs involving "area"
##	variables (i.e. Area and Adjacent). Which island is this? 
##
big.island <- (tortoise.data$Area > 3000)
big.island
tortoise.data[big.island,]	# It's Isabela, with an area of 4669.32km^2
##
##	Next, let's look at some inter-variable relationships. The 
##	earlier plot suggests that there is a linear relationship 
##	between Elevation and Endemics - look at this in more detail.
##	
plot(tortoise.data$Elevation,tortoise.data$Endemics)
##
##	... or, for a publication-quality version
##
plot(tortoise.data$Elevation,tortoise.data$Endemics,
     xlab="Elevation (m)",ylab="No. of species",
     main="Variation of endemic species numbers with\nisland elevation",
     pch=15,col="blue")
box(lwd=2)				# Nice frame round the plot
##
##	And copy to a PDF file ...
##
dev.copy(pdf,"endemics.pdf",width=6,height=6)
dev.off()
##
##	Or, for JPEG (e.g. for embedding in web pages)
##
#dev.copy(jpeg,"endemics.jpg",width=6*72,height=6*72,quality=99)
#dev.off()
##
##	Or, for EPS (e.g. for submitting to journals)
##
#dev.copy(postscript,"endemics.eps",width=6,height=6,onefile=FALSE)
#dev.off()
##
##	Now let's fit a linear regression model to the data
##
endemics.model <- lm(Endemics ~ Elevation, data=tortoise.data)
##
##	Look at the result ...
##
endemics.model
##
##	... and get a more informative summary ...
##
summary(endemics.model)
##
##	Add the fitted regression line to the plot (rerun the previous
##    plot() command first, if necessary)
##
abline(endemics.model, col="red", lty=2, lwd=2)
##
##	And plot some diagnostics (R produces 4 plots by default, 
##	so make space for them)
##
par(mfrow=c(2,2))
plot(endemics.model)
##
##	Reset graphics window to 1 plot per page
##
par(mfrow=c(1,1))	
####################################################################
####################################################################
##
##  DON'T GO BELOW HERE UNTIL YOU HAVE INSTALLED EXTRA PACKAGES!!!
##  (see Section 7 of the preparatory reading materials)
##
####################################################################
####################################################################
##
##  Load the "lattice" package for doing fancy graphics
##
library(lattice)
##
##  Load the topography data
##
load("Topography.rda")
##
##  Open a graphics window that is roughly the same shape as the study area
##
x11(width=8,height=4)
##
##  And make a beautiful map
##
filled.contour(topo.longs,topo.lats,100*topo.data,
               color.palette=terrain.colors,
               plot.title=
                 title("Topographic map of northern Iberia",
                       xlab="Longitude (degrees)",
                       ylab="Latitude (degrees)"),
               key.title=title("Altitude (m)",cex.main=0.8))
##
##  Finally: copy to a PDF file to demonstrate the usefulness of controlling 
##  the size of the graphics window
##
dev.copy(pdf,"NorthernIberiaMap.pdf",width=8,height=4)
dev.off()
