[Vegan-commits] r1545 - in pkg/vegan: R inst man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Mar 16 22:30:59 CET 2011
Author: gsimpson
Date: 2011-03-16 22:30:59 +0100 (Wed, 16 Mar 2011)
New Revision: 1545
Added:
pkg/vegan/R/plot.ordisurf.R
Modified:
pkg/vegan/R/ordisurf.R
pkg/vegan/inst/ChangeLog
pkg/vegan/man/ordisurf.Rd
Log:
adds new plot method for ordisurf, plus new arguments 'plot' and 'gamma'
Modified: pkg/vegan/R/ordisurf.R
===================================================================
--- pkg/vegan/R/ordisurf.R 2011-03-16 15:29:02 UTC (rev 1544)
+++ pkg/vegan/R/ordisurf.R 2011-03-16 21:30:59 UTC (rev 1545)
@@ -20,7 +20,7 @@
col = "red", thinplate = TRUE, add = FALSE, display = "sites",
w = weights(x), main, nlevels = 10, levels, labcex = 0.6,
bubble = FALSE, cex = 1, select = FALSE,
- method = "GCV.Cp", ...)
+ method = "GCV.Cp", gamma = 1, plot = TRUE, ...)
{
weights.default <- function(object, ...) NULL
GRID = 31
@@ -50,9 +50,11 @@
family = family, weights = w, method = method)
else if (thinplate)
mod <- gam(y ~ s(x1, x2, k = knots), family = family,
- weights = w, select = select, method = method)
+ weights = w, select = select, method = method,
+ gamma = gamma)
else mod <- gam(y ~ s(x1, k = knots) + s(x2, k = knots), family = family,
- weights = w, select = select, method = method)
+ weights = w, select = select, method = method,
+ gamma = gamma)
xn1 <- seq(min(x1), max(x1), len=GRID)
xn2 <- seq(min(x2), max(x2), len=GRID)
newd <- expand.grid(x1 = xn1, x2 = xn2)
@@ -69,26 +71,28 @@
as.integer(np), as.double(newd[,1]), as.double(newd[,2]),
inpoly = as.integer(inpoly), PACKAGE="vegan")$inpoly
is.na(fit) <- inpoly == 0
- if (!add) {
- if (bubble) {
- if (is.numeric(bubble))
- cex <- bubble
- cex <- (y - min(y))/diff(range(y)) * (cex-0.4) + 0.4
+ if(plot) {
+ if (!add) {
+ if (bubble) {
+ if (is.numeric(bubble))
+ cex <- bubble
+ cex <- (y - min(y))/diff(range(y)) * (cex-0.4) + 0.4
+ }
+ plot(X, asp = 1, cex = cex, ...)
}
- plot(X, asp = 1, cex = cex, ...)
+ if (!missing(main) || (missing(main) && !add)) {
+ if (missing(main))
+ main <- yname
+ title(main = main)
+ }
+ if (missing(levels))
+ levels <- pretty(range(fit, finite = TRUE), nlevels)
+ ## Only plot surface is select is FALSE or (TRUE and EDF is diff from 0)
+ if(!select || (select && !isTRUE(all.equal(as.numeric(summary(mod)$edf), 0))))
+ contour(xn1, xn2, matrix(fit, nrow=GRID), col = col, add = TRUE,
+ levels = levels, labcex = labcex,
+ drawlabels = !is.null(labcex) && labcex > 0)
}
- if (!missing(main) || (missing(main) && !add)) {
- if (missing(main))
- main <- yname
- title(main = main)
- }
- if (missing(levels))
- levels <- pretty(range(fit, finite = TRUE), nlevels)
- ## Only plot surface is select is FALSE or (TRUE and EDF is diff from 0)
- if(!select || (select && !isTRUE(all.equal(as.numeric(summary(mod)$edf), 0))))
- contour(xn1, xn2, matrix(fit, nrow=GRID), col = col, add = TRUE,
- levels = levels, labcex = labcex,
- drawlabels = !is.null(labcex) && labcex > 0)
mod$grid <- list(x = xn1, y = xn2, z = matrix(fit, nrow = GRID))
class(mod) <- c("ordisurf", class(mod))
mod
Added: pkg/vegan/R/plot.ordisurf.R
===================================================================
--- pkg/vegan/R/plot.ordisurf.R (rev 0)
+++ pkg/vegan/R/plot.ordisurf.R 2011-03-16 21:30:59 UTC (rev 1545)
@@ -0,0 +1,35 @@
+`plot.ordisurf` <- function(x, what = c("contour","persp","gam"),
+ add = FALSE, bubble = FALSE, col = "red", cex = 1,
+ nlevels = 10, levels, labcex = 0.6, ...) {
+ what <- match.arg(what)
+ y <- x$model$y
+ x1 <- x$model$x1
+ x2 <- x$model$x2
+ X <- x$grid$x
+ Y <- x$grid$y
+ Z <- x$grid$z
+ force(col)
+ force(cex)
+ if(isTRUE(all.equal(what, "contour"))) {
+ if(!add) {
+ if(bubble) {
+ if (is.numeric(bubble))
+ cex <- bubble
+ cex <- (y - min(y))/diff(range(y)) * (cex-0.4) + 0.4
+ }
+ plot(x1, x2, asp = 1, cex = cex, ...)
+ }
+ if (missing(levels))
+ levels <- pretty(range(x$grid$z, finite = TRUE), nlevels)
+ contour(X, Y, Z, col = col, add = TRUE,
+ levels = levels, labcex = labcex,
+ drawlabels = !is.null(labcex) && labcex > 0)
+ } else if(isTRUE(all.equal(what, "persp"))) {
+ persp(X, Y, Z, col = col, cex = cex, ...)
+ } else {
+ class(x) <- class(x)[-1]
+ plot(x, ...) ##col = col, cex = cex, ...)
+ class(x) <- c("ordisurf", class(x))
+ }
+ invisible(x)
+}
Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog 2011-03-16 15:29:02 UTC (rev 1544)
+++ pkg/vegan/inst/ChangeLog 2011-03-16 21:30:59 UTC (rev 1545)
@@ -12,7 +12,12 @@
argument `method`. Both `select` and `method` have been set to
defaults that were implied in previous versions of the function.
If smooth is penalized to (effectively) 0, countour plotting is
- suppressed.
+ suppressed. New argument `gamma` can be used to increase the cost
+ of each degree of freedom used by a model in the GCV or UBRE/AIC
+ scores used in smoothness selection. New argument `plot` can be
+ used to stop ordisurf producing any graphical output.
+ A plot method is now provided that can draw contour or perspective
+ plots of ordisurf results, or plot the fitted GAM surface.
* metaMDSrotate: can now rotate > 2-dim solutions so that the
first axis is parallel to a given vector and all others are
Modified: pkg/vegan/man/ordisurf.Rd
===================================================================
--- pkg/vegan/man/ordisurf.Rd 2011-03-16 15:29:02 UTC (rev 1544)
+++ pkg/vegan/man/ordisurf.Rd 2011-03-16 21:30:59 UTC (rev 1545)
@@ -3,6 +3,7 @@
\alias{ordisurf.default}
\alias{ordisurf.formula}
\alias{calibrate.ordisurf}
+\alias{plot.ordisurf}
\title{ Fit and Plot Smooth Surfaces of Variables on Ordination. }
\description{
@@ -13,14 +14,23 @@
\method{ordisurf}{default}(x, y, choices=c(1, 2), knots=10, family="gaussian", col="red",
thinplate = TRUE, add = FALSE, display = "sites",
w = weights(x), main, nlevels = 10, levels, labcex = 0.6,
- bubble = FALSE, cex = 1, select = FALSE, method = "GCV.Cp", ...)
+ bubble = FALSE, cex = 1, select = FALSE, method = "GCV.Cp",
+ gamma = 1, plot = TRUE, ...)
+
\method{ordisurf}{formula}(formula, data, ...)
+
\method{calibrate}{ordisurf}(object, newdata, ...)
+
+\method{plot}{ordisurf}(x, what = c("contour","persp","gam"),
+ add = FALSE, bubble = FALSE, col = "red", cex = 1,
+ nlevels = 10, levels, labcex = 0.6, \dots)
}
\arguments{
- \item{x}{Ordination configuration, either a matrix or a result known
- by \code{\link{scores}}. }
+ \item{x}{For \code{ordisurf} an ordination configuration, either a
+ matrix or a result known by \code{\link{scores}}. For
+ \code{plot.ordisurf} and object of class \code{"ordisurf"} as
+ returned by \code{ordisurf}.}
\item{y}{ Variable to be plotted. }
\item{choices}{Ordination axes. }
\item{knots}{Number of initial knots in \code{\link[mgcv]{gam}} (one
@@ -66,6 +76,12 @@
estimation for both known and unknown scale; and \code{"P-REML"} and
\code{"P-ML"} use REML or ML estimation but use a Pearson estimate
of the scale.}
+ \item{gamma}{Multiplier to inflate model degrees of freedom in GCV or
+ UBRE/AIC score by. This effectively places an extra penalty on
+ complex models. An oft used value if \code{gamma = 1.4}.}
+ \item{plot}{logical; should any plotting be done by
+ \code{ordisurf}? Useful if all you want is the fitted response
+ surface model.}
\item{formula, data}{Alternative definition of the fitted model as
\code{x ~ y}, or left-hand side is the ordination \code{x} and
right-hand side the single fitted continuous variable
@@ -73,9 +89,17 @@
or in the data frame or environment given by \code{data}. All
other arguments of are passed to the default method.}
\item{object}{An \code{ordisurf} result object.}
- \item{newdata}{Coordinates in two-dimensional ordination for new points.}
+ \item{newdata}{Coordinates in two-dimensional ordination for new
+ points.}
+ \item{what}{character; what type of plot to produce. \code{"contour"}
+ produces a contour plot of the response surface, see
+ \code{\link{contour}} for details. \code{"persp"} produces a
+ perspective plot of the same, see \code{\link{persp}} for
+ details. \code{"gam"} plots the fitted GAM model, an object that
+ inherits from class \code{"gam"} returned by \code{ordisurf}, see
+ \code{\link[mgcv]{plot.gam}}.}
\item{\dots}{Other parameters passed to \code{\link[mgcv]{gam}}, or
- to the graphical functions.}
+ to the graphical functions. See Note below for exceptions.}
}
\details{
@@ -133,11 +157,22 @@
predicting new values (see \code{\link[mgcv]{predict.gam}}).
}
-\author{ Dave Roberts and Jari Oksanen }
+\author{ Dave Roberts, Jari Oksanen and Gavin L. Simpson }
\note{
The default is to use thinplate splines. These make sense in
ordination as they have equal smoothing in all directions and are
- rotation invariant.
+ rotation invariant.
+
+ Graphical arguments supplied to \code{plot.ordisurf} are passed on to
+ the underlying plotting functions, \code{contour}, \code{persp}, and
+ \code{\link[mgcv]{plot.gam}}. The exception to this is that arguments
+ \code{col} and \code{cex} can not currently be passed to
+ \code{\link[mgcv]{plot.gam}} because of a bug in the way that function
+ evaluates arguments when arranging the plot.
+
+ A work-around is to call \code{\link[mgcv]{plot.gam}} directly on the
+ result of a call to \code{ordisurf}. See the Examples for an
+ illustration of this.
}
\seealso{ For basic routines \code{\link[mgcv]{gam}},
@@ -161,6 +196,15 @@
fit <- with(varespec, ordisurf(vare.mds, Cla.arb, family=quasipoisson))
## Get fitted values
calibrate(fit)
+
+## Plot method
+plot(fit, what = "contour")
+
+## Plotting the "gam" object
+plot(fit, what = "gam") ## 'col' and 'cex' not passed on
+## or via plot.gam directly
+plot.gam(fit, cex = 2, pch = 1, col = "blue")
+## 'col' effects all objects drawn...
}
\keyword{ multivariate }
\keyword{ aplot }
More information about the Vegan-commits
mailing list