Topic: CTFS Plot Maps

Topic Description:

Functions for maps of CTFS plot data. The file map.r has functions for tree distribution maps, including overlays of elevation contours. The file mapresponse.r has functions for creating color or contour maps of any numerical responses, based on the R functions image, contour, and filled.contour.



File: map/mapresponse.r

View File Source Download File No help file available

Function: imageGraph

Function Description: imageGraph

Maps a response variable with R's contour or image functions, or both, using the output of coldata.to.imagemat. Also can return contours as vector data, using R's contourLines. The graph can be sent to the screen or exported. There are many options for tweaking the graph, explained with the arguments.

Function Arguments:

ArgumentDefault Value
matrixdata
levelsNULL
breaksc(-33000,0,150,250,500,1000,2000)
xname"x"
yname"y"
xrangeNULL
yrangeNULL
axisdivc(100,100)
imgTRUE
cntrsFALSE
filledTRUE
drawgridFALSE
clrsNULL
lwidth1
returnlineFALSE
newgraphFALSE
addFALSE
plotsize6
h11
w11
exportX11
graphfileNULL

Arguments Description:

  • matrixdata: a list having components x, y, and mat, exactly the output of coldata.to.imagemat
  • levels: if set, then contours are drawn (or colors assigned) to breaks are at levels equally-spaced divisions of the response variable; can be NULL to let breaks control
  • breaks: if set, a vector of breaks for the image colors and the contour lines; if NULL, levels controls the breaks
  • xname and yname: names for the axes
  • xrange: if NULL, all x are included, otherwise, the graph will only include x inside xrange
  • yrange: likewise for y
  • axisdiv: the division between tick marks; must be a vector of 2, first for x ticks then for y ticks
  • clrs: colors for the image; if NULL, they are chosen by default, otherwise, must be 1 + the number of breaks
  • returnline: TRUE or FALSE, whether to return the contour lines; see R's base function contourLines for details
  • img: TRUE or FALSE, whether to draw the color image
  • cntrs: TRUE or FALSE, whether to add contour lines; both img and cntrs can be TRUE
  • filled; TRUE or FALSE, whether to draw filled contours; if TRUE, neither image or standard contours are included
  • newgraph: if FALSE, just graph to the screen and export is ignored; use TRUE to create a new plot window or export to a file
  • plotsize: size of graph's x-dimension in inches; ignored if newgraph=FALSE
  • h, w: height and width of graph window in units which depend on the export device chosen (some are pixels, some are inches); ignored if newgraph=FALSE
  • export: if newgraph=TRUE, this defines the graphics device; options include X11, quartz, win.graph, win.metafile, png, bmp, jpeg, pdf (not quoted!)
  • graphfile: the file name if export is pdf, png, jpeg, win.metafile, etc.


Sample Usage:

CTFSplot('bci',6,'full') N=colSums(abundanceperquad(bci.full6,plotdim=c(1000,500),grid=20)$abund) matdata=coldata.to.imagemat(z=N,gridsize=20,plotmax=c(1000,500)) imageGraph(matrixdata=matdata,b=seq(100,400,by=25),img=TRUE,cntrs=FALSE,newgraph=FALSE,plotsize=6,h=11,w=11) imageGraph(matrixdata=matdata,b=seq(100,400,by=25),img=TRUE,cntrs=TRUE,newgraph=FALSE,plotsize=6,h=11,w=11) imageGraph(matrixdata=matdata,b=seq(100,400,by=25),img=TRUE,cntrs=FALSE,newgraph=TRUE,export=win.graph,h=11,w=11,plotsize=8)


Function Source:


imageGraph=function(matrixdata,levels=NULL,breaks=c(-33000,0,150,250,500,1000,2000),xname="x",yname=
        "y",xrange=NULL,yrange=NULL,axisdiv=c(100,100),
img=TRUE,cntrs=FALSE,filled=TRUE,drawgrid=FALSE,addaxes=1:4,clrs=NULL,cntcolor='
        black',lwidth=1,returnline=FALSE, newgraph=FALSE,add=FALSE,plotsize=6,h=11,w=11,export=X11,graphfile=NULL)
{
xax=matrixdata$x
yax=matrixdata$y

if(is.null(xrange)) xrange=range(xax)
if(is.null(yrange)) yrange=range(yax)
incx=xax>=xrange[1] & xax<=xrange[2]
incy=yax>=yrange[1] & yax<=yrange[2]
mat=matrixdata$mat[incy,incx]
xax=xax[incx]
yax=yax[incy]

dim=dim(mat)
aspect=dim[1]/dim[2]

if(is.null(levels)) levels=10
if(is.null(breaks)) breaks=seq(min(mat),max(mat),length=levels)

if(is.null(clrs)) clrs=c("blue",terrain.colors(length(breaks)-2))

if(newgraph)
{
if(is.null(graphfile)) export(height=h,width=w)
else export(height=h,width=w,file=graphfile)
}
oldpar=par(pin=c(plotsize,plotsize*aspect))

xgrid=diff(xrange)/dim(mat)[2]
ygrid=diff(yrange)/dim(mat)[1]

if(img)
{
imagex=seq(xrange[1]+xgrid/2,xrange[2]-xgrid/2,len=length(xax))
imagey=seq(yrange[1]+ygrid/2,yrange[2]-ygrid/2,len=length(yax))
}

if(filled)

filled.contour(x=xax,y=yax,z=t(mat),levels=breaks,asp=1,col=clrs,xlab=xname,ylab=yname,axes=FALSE
        ,lwd=lwidth,
plot.axes={draw.axes(whichaxs=1:2,xrange=xrange,yrange=yrange,div=axisdiv)},key.ax
        es=axis(4,round(breaks,0)))
else if(img) image(x=imagex,y=imagey,z=t(mat),breaks=breaks,col=clrs,xlab=xname,ylab=yname,axes=FAL
        SE,add=TRUE)
else contour(x=xax,y=yax,z=t(mat),levels=breaks,xlab=xname,ylab=yname,axes=FALSE,lwd=lwidth,add=add
        ,col=cntcolor)

if(img&cntrs&!filled) contour(x=xax,y=yax,z=t(mat),levels=breaks,axes=FALSE,lwd=lwidth,add=TRUE,col
        =cntcolor)
if(!filled & !is.null(addaxes)) draw.axes(addaxes,xrange=xrange,yrange=yrange,div=axisdiv)

if(drawgrid)
{
abline(h=seq(yrange[1],yrange[2],by=ygrid))
abline(v=seq(xrange[1],xrange[2],by=xgrid))
}

if(!is.null(graphfile)) graphics.off()

par(oldpar)
if(returnline) return(contourLines(x=xax,y=yax,z=t(mat),levels=breaks))
}