3d-scatterplot

Rstudio 3d scatter plot of detection data by array, station and year

# bob.branton@dal.ca - May 2013
# Purpose: 3d scatter plot of detection data
library(scatterplot3d)

# 1) get tag release data from ERDDAP using read.csv
if(1!=1){

x<-read.csv("http://nile.apl.washington.edu/erddap/tabledap/otnnepDetects.csvp?tracker_reference,transmittername,array_name,platform_name,time&distinct()",as.is=TRUE)
write.csv(x,'demo_3d_x.csv')

x2<-aggregate(
count~tracker+array+station+year,
data=
unique(
data.frame(
tracker=x$tracker_reference,
transmitter=x$transmitter,
array=x$array,
station=x$platform,
year=format(as.Date(x$time..UTC),'%Y'),
count=1,
stringsAsFactors=FALSE
)
),
FUN=sum
)
write.csv(x2,'demo_3d_x2.csv')
}

x<-read.csv('demo_3d_x.csv',as.is=TRUE)
x2<-read.csv('demo_3d_x2.csv',as.is=TRUE)

x2$tracker[x2$tracker=='']<-'Mystery'

# 3) create baisc plot function to subset and plot aggregated data

my.3dplot<-function(array='JDF',tracker='...',zoom=FALSE,angle=45,scale=1){

zmax<-max(x2$count)

x3<-x2[x2$array==array,]
x3$station<-as.numeric(factor(x3$station))

my.3d<-
data.frame(
station=
rep(seq(min(x3$station),max(x3$station)),
each=length(seq(min(x3$year),max(x3$year)))
),
year=
rep(
seq(min(x3$year),max(x3$year)),
length(seq(min(x3$station),max(x3$station)))
),
count=0)  

if(tracker!='...'){
x4<-x3[x3$tracker==tracker,]
}else{
x4<-x3
}

xlim<-c(min(my.3d$year),max(my.3d$year))
ylim<-c(1,max(my.3d$station))
zlim<-c(0,zmax)

for (i in 1:dim(x4)[1]){
s<-my.3d$station==x4$station[i]&as.character(my.3d$year)==x4$year[i]
my.3d$count[s]<-my.3d$count[s]+x4$count[i]  
}

my.3d<-my.3d[my.3d$count!=0,]

if(zoom){
xlim<-c(min(my.3d$year),max(my.3d$year))
ylim<-c(min(my.3d$station),max(my.3d$station))
zlim<-c(0,max(my.3d$count))  
}

scatterplot3d(
my.3d$year,
factor(my.3d$station),
my.3d$count,
main=paste(array,'-',tracker,'transmitters'),
xlab='Year',
ylab='Station',
zlab='Count',
xlim=xlim,
ylim=ylim,
zlim=zlim,
type='h',
scale.y=scale,
angle=angle,
box=FALSE,
cex.symbols=.5)

}

#my.plot('JDF','MOSER',45,1)

# 4) create and execute RStudio manipulate widget

library(manipulate)

manipulate(
my.3dplot(array,tracker,zoom,angle,scale),
array=picker("JDF","QCS","WIL"),
tracker=picker("...","LIND","MOSER","PSS2","VOGL","Mystery"),
zoom = checkbox(FALSE, "zoom"),
angle=slider(0,90,step=5,initial=45),
scale=slider(0,2,step=.1,initial=1))