[R-gregmisc-users] SF.net SVN: r-gregmisc:[1555] trunk/gplots
Brought to you by:
warnes
|
From: <wa...@us...> - 2012-06-07 16:08:29
|
Revision: 1555
http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1555&view=rev
Author: warnes
Date: 2012-06-07 16:08:18 +0000 (Thu, 07 Jun 2012)
Log Message:
-----------
Object returned by hist2d() has been changed:
- $x and $y now contain the n midpoints of the bins in each direction
- $x.breaks, $y.breaks now contain the n+1 limits of the bins in each direction
The function print.hist2d() has been updated appropriately.
Modified Paths:
--------------
trunk/gplots/R/ci2d.R
trunk/gplots/R/hist2d.R
trunk/gplots/R/print.hist2d.R
trunk/gplots/man/hist2d.Rd
Modified: trunk/gplots/R/ci2d.R
===================================================================
--- trunk/gplots/R/ci2d.R 2012-06-07 15:19:02 UTC (rev 1554)
+++ trunk/gplots/R/ci2d.R 2012-06-07 16:08:18 UTC (rev 1555)
@@ -1,4 +1,4 @@
-# $Id: hist2d.R 1471 2011-08-16 01:03:31Z warnes $
+# $Id: ci2d.R 1471 2011-08-16 01:03:31Z warnes $
## first(...) selects the first element of which(...)
first <- function(x,...)
Modified: trunk/gplots/R/hist2d.R
===================================================================
--- trunk/gplots/R/hist2d.R 2012-06-07 15:19:02 UTC (rev 1554)
+++ trunk/gplots/R/hist2d.R 2012-06-07 16:08:18 UTC (rev 1555)
@@ -8,6 +8,8 @@
show=TRUE,
col=c("black", heat.colors(12)),
FUN=base::length,
+ xlab,
+ ylab,
... )
{
if(is.null(y))
@@ -32,13 +34,13 @@
if(same.scale)
{
- x.cuts <- seq( from=min(x,y), to=max(x,y), length=nbins[1])
- y.cuts <- seq( from=min(x,y), to=max(x,y), length=nbins[2])
+ x.cuts <- seq( from=min(x,y), to=max(x,y), length=nbins[1]+1)
+ y.cuts <- seq( from=min(x,y), to=max(x,y), length=nbins[2]+1)
}
else
{
- x.cuts <- seq( from=min(x), to=max(x), length=nbins[1])
- y.cuts <- seq( from=min(y), to=max(y), length=nbins[2])
+ x.cuts <- seq( from=min(x), to=max(x), length=nbins[1]+1)
+ y.cuts <- seq( from=min(y), to=max(y), length=nbins[2]+1)
}
index.x <- cut( x, x.cuts, include.lowest=TRUE)
@@ -51,15 +53,22 @@
## If we're using length, set empty cells to 0 instead of NA
if(identical(FUN,base::length))
m[is.na(m)] <- 0
+
+ if(missing(xlab)) xlab <- deparse(substitute(xlab))
+ if(missing(ylab)) ylab <- deparse(substitute(ylab))
if(show)
- image( x.cuts, y.cuts, m, col=col, ...)
+ image( x.cuts, y.cuts, m, col=col, xlab=xlab, ylab=ylab, ...)
+ midpoints <- function(x) (x[-1]+x[-length(x)])/2
+
retval <- list()
retval$counts <- m
- retval$x=x.cuts
- retval$y=y.cuts
- retval$nobs=length(x)
+ retval$x.breaks = x.cuts
+ retval$y.breaks = y.cuts
+ retval$x = midpoints(x.cuts)
+ retval$y = midpoints(y.cuts)
+ retval$nobs = length(x)
retval$call <- match.call()
class(retval) <- "hist2d"
retval
Modified: trunk/gplots/R/print.hist2d.R
===================================================================
--- trunk/gplots/R/print.hist2d.R 2012-06-07 15:19:02 UTC (rev 1554)
+++ trunk/gplots/R/print.hist2d.R 2012-06-07 16:08:18 UTC (rev 1555)
@@ -2,7 +2,7 @@
print.hist2d <- function(x, ...)
{
- cat("\n")
+ cat("\n")
cat("----------------------------\n")
cat("2-D Histogram Object\n")
cat("----------------------------\n")
@@ -11,9 +11,8 @@
print(x$call)
cat("\n")
cat("Number of data points: ", x$nobs, "\n")
- cat("Number of grid points: ", length(x$x), "x", length(x$y), "\n")
- cat("X range: (", min(x$x), ",", max(x$x), ")\n")
- cat("Y range: (", min(x$y), ",", max(x$y), ")\n")
+ cat("Number of grid bins: ", length(x$x), "x", length(x$y), "\n")
+ cat("X range: (", min(x$x.breaks), ",", max(x$x.breaks), ")\n")
+ cat("Y range: (", min(x$y.breaks), ",", max(x$y.breaks), ")\n")
cat("\n")
-
}
Modified: trunk/gplots/man/hist2d.Rd
===================================================================
--- trunk/gplots/man/hist2d.Rd 2012-06-07 15:19:02 UTC (rev 1554)
+++ trunk/gplots/man/hist2d.Rd 2012-06-07 16:08:18 UTC (rev 1555)
@@ -8,7 +8,8 @@
}
\usage{
hist2d(x,y=NULL, nbins=200, same.scale=FALSE, na.rm=TRUE, show=TRUE,
- col=c("black", heat.colors(12)), FUN=base::length, ... )
+ col=c("black", heat.colors(12)), FUN=base::length, xlab, ylab,
+ ... )
\method{print}{hist2d}(x, ...)
}
\arguments{
@@ -18,7 +19,7 @@
is matrix}
\item{nbins}{number of bins in each dimension. May be a scalar or a
2 element vector. Defaults to 200.}
- \item{same.scale}{use a single range for x and y. Defaults to FALSE.}
+ \item{same.scale}{use the same range for x and y. Defaults to FALSE.}
\item{na.rm}{ Indicates whether missing values should be
removed. Defaults to TRUE.}
\item{show}{ Indicates whether the histogram be displayed using
@@ -29,6 +30,7 @@
\item{FUN}{Function used to summarize bin contents. Defaults to
\code{base::length}. Use, e.g., \code{mean} to calculate means for each bin
instead of counts.
+ \item{xlab,ylab}{(Optional) x and y axis labels}
}
\item{\dots}{ Parameters passed to the image function. }
}
@@ -41,42 +43,53 @@
}
\value{
- A list containing 3 elements:
+ A list containing 5 elements:
\item{counts}{Matrix containing the number of points falling into each
bin}
- \item{x}{lower x limit of each bin}
- \item{y}{lower y limit of each bin}
+ \item{x.breaks, y.breaks}{Lower and upper limits of each bin}
+ \item{x,y}{midpoints of each bin}
}
\author{ Gregory R. Warnes \email{gr...@wa...}}
\seealso{ \code{\link{image}}, \code{\link{persp}}, \code{\link{hist}} }
\examples{
- # example data, bivariate normal, no correlation
+ ## example data, bivariate normal, no correlation
x <- rnorm(2000, sd=4)
y <- rnorm(2000, sd=1)
- # separate scales for each axis, this looks circular
+ ## separate scales for each axis, this looks circular
hist2d(x,y)
- # same scale for each axis, this looks oval
+ ## same scale for each axis, this looks oval
hist2d(x,y, same.scale=TRUE)
- # use different # bins in each dimension
+ ## use different ## bins in each dimension
hist2d(x,y, same.scale=TRUE, nbins=c(100,200) )
- # use the hist2d function to create inputs for a perspective plot ...
+ ## use the hist2d function to create an h2d object
h2d <- hist2d(x,y,show=FALSE, same.scale=TRUE, nbins=c(20,30))
+
+ ## show object summary
+ h2d
+
+ ## object contents
+ str(h2d)
+
+ ## perspective plot
persp( h2d$x, h2d$y, h2d$counts,
ticktype="detailed", theta=30, phi=30,
expand=0.5, shade=0.5, col="cyan", ltheta=-30)
- # for contour (line) plot ...
+ ## for contour (line) plot ...
contour( h2d$x, h2d$y, h2d$counts, nlevels=4 )
- # for a filled contour plot ...
+ ## for a filled contour plot ...
filled.contour( h2d$x, h2d$y, h2d$counts, nlevels=4,
col=gray((4:0)/4) )
+
+
+
}
\keyword{dplot}
\keyword{hplot}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|