Chapter 7 Conclusions and Recommendations
B.2 R Program for Water Data Analysis
################################################################################### ## Parameter Names
## To make everything a little more accessible, we replace the ## parameter codes with the parameter names as defined in the file ## "Params-all.txt".
## Replace punctuation and whitespace with "." fix.names <- function(txt)
gsub("^\\.|\\.$","",gsub("[\\.]{2,}",".",gsub("[[:space:][:punct:]]",".",txt))) ## Read parameters file
codes <-readLines("Params-all.txt") ## Drop lines beginning with line feed codes <- codes[-grep("\f",codes)] ## Drop empty lines
codes <- codes[codes!=""] ## Drop header line codes <- codes[-1] ## Split lines at tabs codes <- strsplit(codes,"\t")
## Restructure code and names as data frame
codes <- data.frame(code=as.integer(round(100*as.numeric(sapply(codes,"[",1)))), name=fix.names(sapply(codes,"[",2)))
################################################################################### ## Excel Data
## For each water body the data are stored as a single Excel sheet. ## Seems that ODBC and Excel are unreliable, so we save Excel files as ## csv and read them in this format.
## Fix up odd date format fix.date <- function(txt) { sub("^([0-9]+/[0-9]+/[0-9]+) @ ([0-9]+:[0-9]+):[0-9]+$","\\1 \\2",txt) } fix.colnames <- function(txt) { txt <- sub("\\.$","",txt) txt <- sub("^val$","Value",txt) txt <- sub("^Param$","Parameter",txt) txt <- sub("^ParameterNo$","Parameter",txt) txt <- sub("^LocationNo$","Location",txt)
txt <- sub("^SampleDepth$","Depth",txt) txt <- sub("^DateAndTime$","Date",txt) txt
}
## Read one Lake_Name.csv file lake.csv <- function(file) { ## Read csv file
d <- read.csv(paste("Lake_",file,".csv",sep="")) ## Standardize names
colnames(d) <- fix.colnames(colnames(d))
## Add site name, trim dates and replace parameter code with parameter name d$Date <- as.Date(strptime(fix.date(d$Date),"%d/%m/%Y %H:%M")) if(!is.integer(d$Parameter)) d$Parameter <- as.integer(round(100*d$Parameter)) d$Site <- rep(file,nrow(d)) d <- d[,c("Site","Date","Depth","Parameter","Value")] d <- d[order(d$Parameter,d$Date,d$Depth),] d$Parameter <- codes$name[match(d$Parameter,codes$code)] d }
## Read all data and merge
lakes <- c("Rosebery","Pieman","Parangana","Mackintosh","Great","Gordon","Burbury","Gairdner") d <- NULL for(l in lakes) { d <- rbind(lake.csv(l),d) } rownames(d) <- NULL ## Add in rough seasons
season <- c("Sum","Aut","Win","Spr")
d$Season <- factor(season[((as.POSIXlt(d$Date)$mon+1)%%12)%/%3+1],labels=season) d$Year <- as.POSIXlt(d$Date)$year+1900
d$Epoch <- factor(ifelse(d$Year >= 1990,"Post 90","Pre 90")) ## These are all the parameters we have.
unique(d$Parameter)
################################################################################### ## Example plots
## xyplot
## pch - sets plot symbol ## alpha - sets transparency
## ylim - sets the range of the y axis (need to set this because there are some huge outliers). ## subset - condition that describes the subset of the data to plot
## xlab, ylab - the x and y axis labels ## main - main plot label
## Plot Water Temperature against Depth separately for each Site, coloured by Season. xyplot(Value~Depth | Site, group=Season,
data=d,subset=Parameter=="Water.Temperature", ylim=c(0,25),
pch=20,alpha=0.1,
ylab="Water Temperature Degree C", xlab="Depth (m)",
main="Water Temperature - Depth") ## Plot temperatures in Depth > 5m bwplot(Value~Site,
data=d,subset=Parameter=="Water.Temperature" & Depth > 5, ylim=c(0,20),
ylab="Water Temperature Degree C", main="Water Temperature - Site Depth > 5 m", ## Rotate x axis labels
scales=list(x=list(rot=90)))
## Make tables of mean, min, max, 5th, 50th and 95th percentiles ## Function to construct the summary
table.summary <- function(x) { c("mean"=mean(x,na.rm=T), "min"=min(x,na.rm=T), "max"=max(x,na.rm=T), quantile(x,prob=c(0.05,0.25,0.5,0.75,0.95),na.rm=T)) }
## with(data,expression) - evaluate expression using data ## split(x,g) - divide x into groups defined by g
## sapply(l,f) - apply function f to each element of list g ## t - transpose
## Basically, using the water temp data, group the temperatures by ## site and compute the summary for each. Transpose results so each ## row is a site.
with(d[d$Parameter=="Water.Temperature",], t(sapply(split(Value,Site),table.summary)))
################################################################ ##Plot pH Value against Depth for each Site
xyplot(Value~Depth | Site, data=d,subset=Parameter=="pH.field", ylim=c(4,9), pch=20,alpha=0.5, ylab="Field pH ", xlab="Depth (m)", main="Field pH - Depth") ## Boxplot of PH by Site in Depth> 5m bwplot(Value~Site,
data=d,subset=Parameter=="pH.field" & Depth > 5, ylim=c(4,8),
ylab="Field pH ",
main="Field pH - Site Depth > 5 m", ## Rotate x axis labels
scales=list(x=list(rot=90)))
## Make tables of mean, min, max, 5th, 25th,50th 75th and 95th percentiles with(d[d$Parameter=="pH.field",],
t(sapply(split(Value,Site),table.summary)))
################################################################ ## Plot Turbidity against Date separately for each Site
xyplot(Value~Depth | Site, data=d,subset=Parameter=="Turbidity", ylim=c(0,80), pch=20,alpha=0.5, ylab="Turbidity NTU", xlab="Depth (m)", main="Turbidity - Depth") ## Boxplots of Turbidity by Site bwplot(Value~Site,
data=d,subset=Parameter=="Turbidity" & Depth > 5, ylim=c(-0.1,20),
ylab="Turbidity NTU",
main="Turbidity - Site Depth > 5 m", ## Rotate x axis labels
scales=list(x=list(rot=90)))
## Make tables of mean, min, max, 5th, 25th,50th 75th and 95th percentiles ## Turbidity only
with(d[d$Parameter=="Turbidity",],
t(sapply(split(Value,Site),table.summary)))
################################################################ ## Plot Conductivity against Date separately for each Site
xyplot(Value~Depth | Site, data=d,subset=Parameter=="Conductivity.TRef", ylim=c(0,100), pch=20,alpha=0.5, ylab="Conductivity us/cm", xlab="Depth (m)", main="Conductivity - Depth") ## Boxplots of Conductivity by Site bwplot(Value~Site,
data=d,subset=Parameter=="Conductivity.TRef" & Depth > 5, ylim=c(10,70),
ylab="Conductivity us/cm",
main="Conductivity - Site Depth > 5 m", ## Rotate x axis labels
scales=list(x=list(rot=90)))
## Make tables of mean, min, max, 5th, 50th and 95th percentiles with(d[d$Parameter=="Conductivity.TRef",],
t(sapply(split(Value,Site),table.summary)))
################################################################ ## Plot Iron against Date separately for each Site
xyplot(Value~Date | Site, data=d,subset=Parameter=="Iron.Total.NonFilt", ylim=c(0,2), pch=20,alpha=0.5, ylab="Total Iron mg/l", xlab="Time", main="Total Iron - Time") ## Boxplots of Iron by Site bwplot(Value~Site,
data=d,subset=Parameter=="Iron.Total.NonFilt", ylim=c(0,1.2),
ylab="Iron.Total mg/l", main="Total Iron - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
## Make tables of mean, min, max, 5th, 50th and 95th percentiles with(d[d$Parameter=="Iron.Total.NonFilt" & d$Value <2,], t(sapply(split(Value,Site),table.summary)))
################################################################ ## Boxplots of dissolved Iron by Site
bwplot(Value~Site,
data=d,subset=Parameter=="Iron.Dissolved.Fi", ylim=c(0,1.5),
ylab="Iron.Dissolved mg/l", main="Dissolved Iron - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
################################################################# ## Plot Manganese against Date separately for each Site
xyplot(Value~Date | Site, data=d,subset=Parameter=="Manganese.Total.No", ylim=c(0,0.5), pch=20,alpha=0.5, ylab="Total Manganese mg/l", xlab="Time",
main="Total Manganese - Time") ## Boxplots of Manganese by Site bwplot(Value~Site,
data=d,subset=Parameter=="Manganese.Total.No", ylim=c(0,0.15),
ylab="Manganese.Total mg/l", main="Total Manganese - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
with(d[d$Parameter=="Manganese.Total.No" & d$Value <2,], t(sapply(split(Value,Site),table.summary)))
########################################################### ## Boxplots of Zinc by Site
data=d,subset=Parameter=="Zinc.Total.NonFilt", ylim=c(0,0.15),
ylab="Zinc.Total mg/l", main="Total Zinc - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
########################################################### ## Boxplots of Aluminium by Site
bwplot(Value~Site,
data=d,subset=Parameter=="Aluminium.Total.No", ylim=c(0,0.6),
ylab="Aluminium.Total mg/l", main="Total Aluminium - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
with(d[d$Parameter=="Aluminium.Total.No" & d$Value <1,], t(sapply(split(Value,Site),table.summary)))
########################################################### ## Boxplots of Dissolved Oxygen by Site
bwplot(Value~Site,
data=d,subset=Parameter=="Dissolved.Oxygen.DO" & Depth > 15, ylim=c(-0.5,120),
ylab="Dissolved Oxygen mg/l",
main="Dissolved Oxygen - Site Depth >15 m", ## Rotate x axis labels
scales=list(x=list(rot=90))) ## Value in Depth >=15 m only
with(d[d$Parameter=="Dissolved.Oxygen.DO" & d$Depth >= 15,], t(sapply(split(Value,Site),table.summary)))
########################################################### ## Plot Dissolved Organic.Carbon.Dis against site
bwplot(Value~Site,
data=d,subset=Parameter=="Organic.Carbon.Dis", ylim=c(0,25),
ylab="Dissolved Organic Carbon mg/l", main="Dissolved Organic Carbon - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
with(d[d$Parameter=="Organic.Carbon.Dis" & d$Value <35,], t(sapply(split(Value,Site),table.summary)))
############################################################# ## Boxplots of Sodium by Site
bwplot(Value~Site,
data=d,subset=Parameter=="Sodium.Total.as.Na", ylim=c(0,10),
ylab="Sodium.Total mg/l", main="Total Sodium - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
with(d[d$Parameter=="Sodium.Total.as.Na" & d$Value <8,], t(sapply(split(Value,Site),table.summary)))
############################################################# ## Plot Sulphate.Total against Date separately for each Site
bwplot(Value~Site,
data=d,subset=Parameter=="Sulphate.Total.as", ylim=c(0,6),
ylab="Sulphate.Total mg/l", main="Total Sulphate - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
with(d[d$Parameter=="Sulphate.Total.as" & d$Value <10,], t(sapply(split(Value,Site),table.summary)))
############################################################# ## Boxplots of Potassium by Site
bwplot(Value~Site,
data=d,subset=Parameter=="Potassium.Total.a", ylim=c(0,2),
ylab="Potassium.Total mg/l", main="Total Potassium - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
with(d[d$Parameter=="Potassium.Total.a" & d$Value <10,], t(sapply(split(Value,Site),table.summary)))
############################################################# ## Plot Phosphorus against Date separately for each Site
data=d,subset=Parameter=="Phosphorus.as.P.To", ylim=c(0,0.015),
ylab="Phosphorus mg/l", main="Phosphorus - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
############################################################# ## Plot Kjeldahl.Nitrogen against Date separately for each Site
bwplot(Value~Site,
data=d,subset=Parameter=="Kjeldahl.Nitrogen.T", ylim=c(0,0.4),
ylab="Kjeldahl.Nitrogen mg/l", main="Kjeldahl Nitrogen - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
with(d[d$Parameter=="Kjeldahl.Nitrogen.T" & d$Value <1,], t(sapply(split(Value,Site),table.summary)))
############################################################# ## Plot Nitrate.as.NO3 against Date separately for each Site
bwplot(Value~Site,
data=d,subset=Parameter=="Nitrate.as.NO3.Tot", ylim=c(0,0.1),
ylab="Total Nitrate as NO3 mg/l", main="Total Nitrate as NO3 - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))
with(d[d$Parameter=="Nitrate.as.NO3.Tot" & d$Value <0.1,], t(sapply(split(Value,Site),table.summary)))
############################################################# ## Plot Ammonia.as.N against Date separately for each Site
bwplot(Value~Site,
data=d,subset=Parameter=="Ammonia.as.N", ylim=c(0,0.06),
ylab="Ammonia as N mg/l", main="Ammonia as N - Site",
## Rotate x axis labels scales=list(x=list(rot=90)))