Time Series
#Load your data.
mydata <- read.csv("ExampleTestData.csv", sep=",")
attach(mydata)
plot(MW01~Year,data=mydata[!is.na(mydata$MW01),],
#ylim sets the range for the y axis
ylim=range(0,60),
#xlim sets the range for the x axis
xlim=range(1994,2008),
# sets the marker type, size and color for 1st series of data MW01
pch=1,cex=0.9,col=1,
#sets the X and Y axis labels
xlab="Year",ylab="EMD (mg/L)")
#Plots the other datasets points. col is the color, pch is the marker type, cex is the size of the marker.
with(mydata[!is.na(mydata$MW02),],points(Year,MW02,col=2,pch=2,cex=0.8))
with(mydata[!is.na(mydata$MW03),],points(Year,MW03,col=3,pch=3,cex=0.8))
with(mydata[!is.na(mydata$MW04),],points(Year,MW04,col=4,pch=4,cex=0.8))
with(mydata[!is.na(mydata$MW05),],points(Year,MW05,col=5,pch=5,cex=0.8))
with(mydata[!is.na(mydata$MW06),],points(Year,MW06,col=6,pch=6,cex=0.8))
with(mydata[!is.na(mydata$MW07),],points(Year,MW07,col=7,pch=7,cex=0.8))
with(mydata[!is.na(mydata$MW08),],points(Year,MW08,col=8,pch=8,cex=0.8))
with(mydata[!is.na(mydata$MW09),],points(Year,MW09,col=9,pch=9,cex=0.8))
with(mydata[!is.na(mydata$MW10),],points(Year,MW10,col=10,pch=10,cex=0.8))
with(mydata[!is.na(mydata$MW11),],points(Year,MW11,col=11,pch=11,cex=0.8))
with(mydata[!is.na(mydata$MW12),],points(Year,MW12,col=12,pch=12,cex=0.8))
with(mydata[!is.na(mydata$MW13),],points(Year,MW13,col=13,pch=13,cex=0.8))
with(mydata[!is.na(mydata$MW14),],points(Year,MW14,col=14,pch=14,cex=0.8))
#Plots the Lines. Lty is the line type, lwd is the line weight/thickness, col is the color
with(mydata[!is.na(mydata$MW01),],lines(Year,MW01,lty=c(1),lwd=c(1),col=1))
with(mydata[!is.na(mydata$MW02),],lines(Year,MW02,lty=c(2),lwd=c(1),col=2))
with(mydata[!is.na(mydata$MW03),],lines(Year,MW03,lty=c(3),lwd=c(1),col=3))
with(mydata[!is.na(mydata$MW04),],lines(Year,MW04,lty=c(4),lwd=c(1),col=4))
with(mydata[!is.na(mydata$MW05),],lines(Year,MW05,lty=c(5),lwd=c(1),col=5))
with(mydata[!is.na(mydata$MW06),],lines(Year,MW06,lty=c(6),lwd=c(1),col=6))
with(mydata[!is.na(mydata$MW07),],lines(Year,MW07,lty=c(7),lwd=c(1),col=7))
with(mydata[!is.na(mydata$MW08),],lines(Year,MW08,lty=c(8),lwd=c(1),col=8))
with(mydata[!is.na(mydata$MW09),],lines(Year,MW09,lty=c(9),lwd=c(1),col=9))
with(mydata[!is.na(mydata$MW10),],lines(Year,MW10,lty=c(10),lwd=c(1),col=10))
with(mydata[!is.na(mydata$MW11),],lines(Year,MW11,lty=c(11),lwd=c(1),col=11))
with(mydata[!is.na(mydata$MW12),],lines(Year,MW12,lty=c(12),lwd=c(1),col=12))
with(mydata[!is.na(mydata$MW13),],lines(Year,MW13,lty=c(13),lwd=c(1),col=13))
with(mydata[!is.na(mydata$MW14),],lines(Year,MW14,lty=c(14),lwd=c(1),col=14))
#Add Legend to graph.
legend("bottomleft",
# adds the text for the legend
c("MW01","MW02", "MW03", "MW04", "MW05","MW06","MW07","MW08","MW09","MW10","MW11","MW12","MW13","MW14"),
#makes the colors
col = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14),
# Sets the size of the legend. You can change the size of the box by changing cex = 0.75 Large # makes it larger.
cex = 1,
#sets the text color
text.col = "black",
#sets the line type
lty = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14),
#Sets the line weight
lwd=c(2),
#sets the marker type
pch = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14),
merge = TRUE, bg = 'gray90')
#Add title
title(main="Time Series Plot")
#done
#The following is for Individual graphs of each well. You can just search and replace MW01 with whatever well you need.
#loads the zoo Library. Note you have to have it installed 1st.
library(zoo)
#Sets the path / dir you will be working in.
setwd("c:/R")
#Load your data. The data is in a spreadsheet named KW-spreadsheet and we are going to call it "data" in R
mydata <- read.csv("ExampleTestData.csv", sep=",")
#Deals with missing data
mydata$MW01<-na.approx(mydata$MW01)
#Plots MW01 data.
plot(MW01~Year,data=mydata,col=ifelse(D_MW01,"black","red"),ylab="END (mg/L)",pch=ifelse(D_MW01,19,24),cex=1)
with(mydata,lines(Year,MW01,lty=c(1),col="black"))
#calculates 2 standard errors one each side of the loess line
plx<-predict(loess(MW01 ~ Year, data=mydata), se=T)
lines(mydata$Year,plx$fit+2*plx$s, lty=3)
lines(mydata$Year,plx$fit-2*plx$s, lty=3)
#Plots the Loess line. You can change the smoothing factor by changing the span number
y.loess <- loess(y ~ x, span=0.8, data.frame(x=mydata$Year,y=mydata$MW01))
y.predict <- predict(y.loess, data.frame(x=mydata$Year))
lines(mydata$Year,y.predict, lty=2, lwd=2)
#Add Legend to MW01. You can change the size of the box by changing cex = 0.75 Large # makes it larger.
legend ("topleft",
c("Smoothing Curve","Confidence Interval","Detect","Non-Detect"),
col = c(1,1,1,2),
# Sets the size of the legend. You can change the size of the box by changing cex = 0.75 Large # makes it larger.
cex = 0.7,
text.col = "black",
#sets the line type
lty = c(2,3,-1,-1),
#sets the line weight
lwd = c(2,1,-1,-1),
#sets the marker type
pch = c(-1,-1,19,24),
merge = TRUE, bg = 'gray90')
#Add title
title(main="MW01")
#Turns off the image storage up at top.
# Done
#The following is for Individual graphs of each well and saving it as image. Code here for the 1st 3 wells. You can just keep searching and replacing and adding new code.
#The following is for Individual graphs of each well. You can just search and replace MW01 with whatever well you need.
#loads the zoo Library. Note you have to have it installed 1st.
library(zoo)
#Sets the path / dir you will be working in.
setwd("c:/R")
#Load your data. The data is in a spreadsheet named KW-spreadsheet and we are going to call it "data" in R
mydata <- read.csv("ExampleTestData.csv", sep=",")
#Deals with missing data
mydata$MW01<-na.approx(mydata$MW01)
#Plots MW01 data.
plot(MW01~Year,data=mydata,col=ifelse(D_MW01,"black","red"),ylab="END (mg/L)",pch=ifelse(D_MW01,19,24),cex=1)
with(mydata,lines(Year,MW01,lty=c(1),col="black"))
#calculates 2 standard errors one each side of the loess line
plx<-predict(loess(MW01 ~ Year, data=mydata), se=T)
lines(mydata$Year,plx$fit+2*plx$s, lty=3)
lines(mydata$Year,plx$fit-2*plx$s, lty=3)
#Plots the Loess line. You can change the smoothing factor by changing the span number
y.loess <- loess(y ~ x, span=0.8, data.frame(x=mydata$Year,y=mydata$MW01))
y.predict <- predict(y.loess, data.frame(x=mydata$Year))
lines(mydata$Year,y.predict, lty=2, lwd=2)
#Add Legend to graph. You can change the size of the box by changing cex = 0.75 Large # makes it larger.
legend ("topright",
c("Detect", "Non-Detect", "LOWESS Curve", "Confidence Interval"),
col = c(1,2,1,1),
cex = 0.75,
text.col = "black",
lty = c(-1,-1,2,3),
pch = c(19, 24, -1, -1),
merge = TRUE, bg = 'gray90')
#Add title
title(main="Locally Weighted Scatterplot Smoothing Curve")
# Done
#Now we do the same for MW02
#The following is for Individual graphs of each well. You can just search and replace MW02 with whatever well you need.
#loads the zoo Library. Note you have to have it installed 1st.
library(zoo)
#Sets the path / dir you will be working in.
setwd("c:/R")
#Load your data. The data is in a spreadsheet named KW-spreadsheet and we are going to call it "data" in R
mydata <- read.csv("ExampleTestData.csv", sep=",")
#Deals with missing data
mydata$MW02<-na.approx(mydata$MW02)
#Plots MW02 data.
plot(MW02~Year,data=mydata,col=ifelse(D_MW02,"black","red"),ylab="END (mg/L)",pch=ifelse(D_MW02,19,24),cex=1)
with(mydata,lines(Year,MW02,lty=c(1),col="black"))
#calculates 2 standard errors one each side of the loess line
plx<-predict(loess(MW02 ~ Year, data=mydata), se=T)
lines(mydata$Year,plx$fit+2*plx$s, lty=3)
lines(mydata$Year,plx$fit-2*plx$s, lty=3)
#Plots the Loess line. You can change the smoothing factor by changing the span number
y.loess <- loess(y ~ x, span=0.8, data.frame(x=mydata$Year,y=mydata$MW02))
y.predict <- predict(y.loess, data.frame(x=mydata$Year))
lines(mydata$Year,y.predict, lty=2, lwd=2)
#Add Legend to graph. You can change the size of the box by changing cex = 0.75 Large # makes it larger.
legend ("topright",
c("Detect", "Non-Detect", "LOWESS Curve", "Confidence Interval"),
col = c(1,2,1,1),
cex = 0.75,
text.col = "black",
lty = c(-1,-1,2,3),
pch = c(19, 24, -1, -1),
merge = TRUE, bg = 'gray90')
#Add title
title(main="Locally Weighted Scatterplot Smoothing Curve")
# Done
#Now we do the same for MW03
#loads the zoo Library. Note you have to have it installed 1st.
library(zoo)
#Sets the path / dir you will be working in.
setwd("c:/R")
#Load your data. The data is in a spreadsheet named KW-spreadsheet and we are going to call it "data" in R
mydata <- read.csv("ExampleTestData.csv", sep=",")
#Deals with missing data
mydata$MW03<-na.approx(mydata$MW03)
#Plots MW03 data.
plot(MW03~Year,data=mydata,col=ifelse(D_MW03,"black","red"),ylab="END (mg/L)",pch=ifelse(D_MW03,19,24),cex=1)
with(mydata,lines(Year,MW03,lty=c(1),col="black"))
#calculates 2 standard errors one each side of the loess line
plx<-predict(loess(MW03 ~ Year, data=mydata), se=T)
lines(mydata$Year,plx$fit+2*plx$s, lty=3)
lines(mydata$Year,plx$fit-2*plx$s, lty=3)
#Plots the Loess line. You can change the smoothing factor by changing the span number
y.loess <- loess(y ~ x, span=0.8, data.frame(x=mydata$Year,y=mydata$MW03))
y.predict <- predict(y.loess, data.frame(x=mydata$Year))
lines(mydata$Year,y.predict, lty=2, lwd=2)
#Add Legend to graph. You can change the size of the box by changing cex = 0.75 Large # makes it larger.
legend ("topright",
c("Detect", "Non-Detect", "LOWESS Curve", "Confidence Interval"),
col = c(1,2,1,1),
cex = 0.75,
text.col = "black",
lty = c(-1,-1,2,3),
pch = c(19, 24, -1, -1),
merge = TRUE, bg = 'gray90')
#Add title
title(main="Locally Weighted Scatterplot Smoothing Curve")
# Done
#set the dir to where you data is
setwd("c:/R")
This is just some simple code mainly for me. The main thing here is this can deal with missing data.
Note the data file is attached below. This is based off the ProUCL data file built into the Mann-Kendall spreadsheet found on the Mann-Kendall page.
This plot all the datasets as one graph. There are a total of 14 datasets here which is way too much information but you can cut it down to what you need.