LPlot Demo

RStudio plotting of tags released vs detected using ERDDAP data

# bob.branton@dal.ca - May 2013
# Purpose: basic and RStudio plotting of tags released vs detected using ERDDAP data
# Overview of procedure:
# 1) get data using read.csv function
# 2) conditioning UTC time using as.Date function
# 3) create zero filled daily time series using seq and rep functions ...
# 4) add tag release counts to the zero based series and plot
# 5) aggregate and bin daily detection counts by array_name, transmiiter ...
# 6) add detection data to time series and plot on independant right axis
# 7) grey out the zero line, add labels and legend
# 8) create and execute RStudio manipulate widget

#############################

# 1) get tag release data from ERDDAP using read.csv

if(1!=1){  
my.t<-
read.csv(
"http://nile.apl.washington.edu/erddap/tabledap/otnnepAnTags.csvp?project_reference,time,expected_enddate",
as.is=TRUE
)
write.csv(my.t,'plot_demo1.csv')

# get detection data from ERDDAP using read.csv

my.d<-
read.csv(
"http://nile.apl.washington.edu/erddap/tabledap/otnnepDetects.csvp?tracker_reference,array_name,transmittername,time",
as.is=TRUE
)  
write.csv(my.d,'plot_demo2.csv')

}
my.t<-read.csv('plot_demo1.csv',as.is=TRUE)
my.d<-read.csv('plot_demo2.csv',as.is=TRUE)

# create a basic plot function ...

my.lplot<-function(my.proj='MOSER',by.ndays=30,zoom=TRUE,leg.xy='topright'){

# get subsets of the data  

my.t<-my.t[my.t$project==my.proj,]
my.d<-my.d[my.d$tracker==my.proj,]

# 2) condition UTC times using as.Date function
# then aggregate by date released and expected enddate

my.t<-
aggregate(
count~startdate+enddate,
FUN=sum,
data=data.frame(
startdate=as.Date(my.t$time..UTC.),
enddate=as.Date(my.t$expected_enddate..UTC.),
count=1,
stringsAsFactors=FALSE
)
)

# 3) create zero filled daily time series using seq function
#    and day.bin using rep and seq functions

my.min.d<-min(my.t$start)-1
my.max.d<-max(my.t$end)+1
my.ndays<-as.numeric(my.max.d-my.min.d)+1

my.ts<-data.frame(
day=
seq(
my.min.d,
my.max.d,
'days'
),
day.bin=
rep(
seq(
my.min.d,
my.max.d,
paste(
by.ndays,
'days'
)
),
each=by.ndays
)[1:my.ndays],  
active=0
)  

# 4) add the tag release counts to the zero based series and plot

for (i in 1:dim(my.t)[1]){
s<-(my.ts$day>=my.t$start[i])&(my.ts$day<=my.t$end[i])
my.ts$active[s]<-my.ts$active[s]+my.t$count[i]
}    

par(mar=c(4,4,4,4))  

plot(
my.ts$day,
my.ts$active,
type='l',
lty=2,
lwd=2,
col='grey',
main=my.proj,
xlab="date",
ylab='')

mtext('number of active tags',side=2,line=3,cex=.8)

# 5) aggregate and bin daily detection counts by array_name, transmiiter
#  note match function is used to assign day to day.bin

my.d<-
aggregate(
detections~array_name+date,
FUN=sum,
data=
unique(
data.frame(
array_name=my.d$array_name,
transmittername=my.d$transmittername,
date=my.ts$day.bin[match(as.Date(my.d$time..UTC.),my.ts$day)],
detections=1
)
)
)

# 6) add detection data to time series and plot using separate independant right axis

my.y.max<-max(my.ts$active)
if(zoom){my.y.max<-max(my.d$detections)}

my.col<-2
for(i in names(table(my.d$array_name))){
my.ts[,i]<-0
my.sub.d<-my.d[my.d$array_name==i,]
for (j in 1:dim(my.sub.d)[1]){
s<-my.ts$day.bin==my.sub.d$date[j]
my.ts[s,i]<-my.ts[s,i]+my.sub.d$detections[j]
}  
par(new=TRUE)
plot(
my.ts$day,
my.ts[,i],
yaxt='n',
type='l',
xlab='',
ylab='',
ylim=c(0,my.y.max),
col=my.col)
my.col<-my.col+1
}

# grey out the zero count line  
lines(
my.ts$day,
rep(0,dim(my.ts)[1]),
col='white',
lwd=2
)  

# label plot

axis(4,seq(0,my.y.max))
mtext('number of tags detected',side=4,line=3,cex=.8)
mtext(paste('by.ndays=',by.ndays),side=3,line=.5,cex=.8)

# create legend

my.legend<-names(table(my.d$array_name))

legend(
leg.xy,
names(table(my.d$array_name)),
col=2:(length(my.legend)+1),
lty=1,
bty='n',
cex=.75,
title='Arrays')

}

# par(mfrow=c(2,2))
# my.plot('LIND',by.ndays=90)
# my.plot('MOSER',by.ndays=60)
# my.plot('PSS2',by.ndays=10,leg.xy='topleft')
# my.plot('VOGL')

##########################

# 8) create and execute RStudio manipulate widget ...

par(mfrow=c(1,1))

options(useFancyQuotes = FALSE)
cat(paste(
'manipulate(my.lplot(my.proj,by.ndays,zoom,leg.xy)',',\n',
'my.proj=picker(',paste(dQuote(names(table(my.t$project))),collapse=","),')',',\n',  
'by.ndays = slider(1, 365, step=1, initial=30),\n',
'zoom = checkbox(FALSE, "zoom"),\n',
'leg.xy=picker("topright","topleft"))\n',  
collapse=''),file='temp.r')

source('temp.r')

##########################