├── .Rbuildignore ├── data └── NHANES.rda ├── .travis.yml ├── vignettes └── references.bib ├── README.md ├── man ├── list2hexList.Rd ├── panel.hexgrid.Rd ├── old-classes.Rd ├── inout.hex.Rd ├── getHMedian.Rd ├── hsmooth-methods.Rd ├── pushHexport.Rd ├── hcell2xyInt.Rd ├── panel.hexloess.Rd ├── hexVP.abline.Rd ├── hexList.Rd ├── hexMA.loess.Rd ├── optShape.Rd ├── colramp.Rd ├── hexViewport.Rd ├── NHANES.Rd ├── hcell2xy.Rd ├── hexTapply.Rd ├── panel.hexboxplot.Rd ├── hexGraphPaper.Rd ├── erode.hexbin.Rd ├── hexpolygon.Rd ├── hexplom.Rd ├── hexVP-class.Rd ├── grid.hexlegend.Rd ├── smooth.hexbin.Rd ├── hboxplot.Rd ├── hexbin.Rd ├── hdiffplot.Rd ├── gplot.hexbin.Rd ├── plotMAhex.Rd ├── grid.hexagons.Rd └── hexbinplot.Rd ├── tests ├── viewp-ex.R ├── hray.R ├── large.R ├── hdiffplot.R ├── viewp-ex.Rout.save ├── hray.Rout.save ├── large.Rout.save └── hdiffplot.Rout.save ├── src ├── reg.c ├── hcell.f ├── hbin.f ├── hsm.f └── herode.f ├── TODO ├── R ├── smoothHexbin.R ├── hexpanel.R ├── hbox.R ├── LINGRAY.R ├── hexbinList.R ├── hexutil.R ├── grid.hexlegend.R ├── LOCS.R ├── HO.R ├── BTC.R ├── BTY.R ├── MAG.R ├── hexPlotMA.R ├── RB.R └── hexViewport.R ├── DESCRIPTION ├── .github └── workflows │ └── R-CMD-check.yaml ├── ChangeLog └── NAMESPACE /.Rbuildignore: -------------------------------------------------------------------------------- 1 | \.travis\.yml 2 | README.md 3 | ^\.github$ 4 | -------------------------------------------------------------------------------- /data/NHANES.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edzer/hexbin/HEAD/data/NHANES.rda -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: r 2 | sudo: required 3 | 4 | warnings_are_errors: true 5 | 6 | bioc_packages: 7 | - marray 8 | - affy 9 | - Biobase 10 | -------------------------------------------------------------------------------- /vignettes/references.bib: -------------------------------------------------------------------------------- 1 | @article{carretal, 2 | author = {Daniel B. Carr and Anthony R. Olsen and Denis White}, 3 | title = {Hexagon Mosaic Maps for Display of Univariate and Bivariate Geographical Data}, 4 | journal = {Cartography and Geographic Information Systems}, 5 | volume = {19}, 6 | number = {4}, 7 | pages = {228-236}, 8 | year = {1992}, 9 | publisher = {Taylor & Francis}, 10 | doi = {10.1559/152304092783721231} 11 | } 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | hexbin 2 | ====== 3 | 4 | [![CRAN](http://www.r-pkg.org/badges/version/hexbin)](http://cran.rstudio.com/package=hexbin) [![Downloads](http://cranlogs.r-pkg.org/badges/hexbin?color=brightgreen)](http://www.r-pkg.org/pkg/hexbin) 5 | [![R-CMD-check](https://github.com/edzer/hexbin/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/edzer/hexbin/actions/workflows/R-CMD-check.yaml) 6 | 7 | 8 | An R Package with binning and plotting functions for hexagonal bins. 9 | -------------------------------------------------------------------------------- /man/list2hexList.Rd: -------------------------------------------------------------------------------- 1 | \name{list2hexList} 2 | \alias{list2hexList} 3 | \title{Convert list to hexList} 4 | \description{ 5 | Converts a list of hexbin objects with same xbnds, ybnds, shape and 6 | xbins to a \code{\link{hexList}} object. 7 | } 8 | \usage{ 9 | list2hexList(binlst) 10 | } 11 | \arguments{ 12 | \item{binlst}{A list of hexbin objects} 13 | } 14 | 15 | \value{ 16 | a \code{\link{hexList}} object 17 | } 18 | 19 | \author{Nicholas Lewin-Koh} 20 | 21 | 22 | \seealso{\code{\link{hexList}},\code{\link{hdiffplot}} } 23 | 24 | \keyword{misc} 25 | 26 | -------------------------------------------------------------------------------- /tests/viewp-ex.R: -------------------------------------------------------------------------------- 1 | library(hexbin) 2 | 3 | ## a variation on Nicholas' post to bioconductor & example(hexViewport) 4 | set.seed(545) 5 | x <- rnorm(2^15) 6 | y <- 3*x - .2*x^2 + rnorm(2^15) 7 | hbin <- hexbin(x,y) 8 | 9 | ## 10 | hp <- hexViewport(hbin, newpage = TRUE) 11 | pushHexport(hp) 12 | library("grid") 13 | grid.rect() 14 | grid.xaxis() 15 | grid.yaxis() 16 | grid.hexagons(hbin, style = "centroid") 17 | hloess <- loess(y ~ x, data = hcell2xy(hbin), weights = hbin @ count) 18 | xx <- seq(hbin@xbnds[1], hbin@xbnds[2], length = 500) 19 | grid.lines(xx, predict(hloess, xx), 20 | gp = gpar(col = 'red', lwd = 2), default.units = "native") 21 | popViewport() 22 | -------------------------------------------------------------------------------- /man/panel.hexgrid.Rd: -------------------------------------------------------------------------------- 1 | \name{panel.hexgrid} 2 | \alias{panel.hexgrid} 3 | \title{Hexagonal grid for a lattice plot} 4 | \description{ 5 | A panel function to add a hexagonal grid to a lattice plot. 6 | } 7 | \usage{ 8 | panel.hexgrid(h, border = grey(0.85)) 9 | } 10 | 11 | \arguments{ 12 | \item{h}{an object of class \code{hexbin}.} 13 | \item{border}{a color for the hexagon border colors} 14 | } 15 | \value{ 16 | There is no return value from this function. The results are plotted on 17 | the current active device. 18 | } 19 | \author{Nicholas Lewin-Koh \email{nikko@hailmail.net}} 20 | \seealso{\code{\link{hexbinplot}}, \code{\link{hexGraphPaper}}} 21 | \keyword{hplot} 22 | -------------------------------------------------------------------------------- /tests/hray.R: -------------------------------------------------------------------------------- 1 | library(hexbin) 2 | 3 | set.seed(572) 4 | 5 | x <- rnorm(100) 6 | y <- rnorm(100) 7 | val <- rnorm(100) 8 | inc <- abs(rnorm(100,sd = .3)) 9 | loB <- val-inc 10 | hiB <- val+inc 11 | 12 | if(exists("hray", mode="function")) { # 'real soon now' 13 | 14 | ## no confidence bounds 15 | plot(x,y,type = 'n') 16 | hray(x,y,val) 17 | 18 | ## confidence bounds 19 | plot(x,y,type = 'n') 20 | hray(x,y,val, lo = loB, hi = hiB) 21 | 22 | ## clockwise orientation 23 | plot(x,y,type = 'n') 24 | hray(x,y,val, loB, hiB, clockwise = TRUE) 25 | 26 | ## no tics and small filled dots 27 | plot(x,y,type = 'n') 28 | hray(x,y,val, loB, hiB, ticlength = FALSE, 29 | dotside = 20, dotlength = .025, dotden = -1) 30 | 31 | } 32 | -------------------------------------------------------------------------------- /man/old-classes.Rd: -------------------------------------------------------------------------------- 1 | \name{old-classes} 2 | \title{Class "unit" and "viewport" as S4 classes} 3 | % 4 | \docType{class} 5 | \alias{unit-class} 6 | \alias{viewport-class} 7 | % 8 | \description{Package "hexbin" now uses S4 classes throughout and hence 9 | needs to \code{\link[methods]{setOldClass}} both \code{"unit"} and 10 | \code{"viewport"} (which are S3 classes from the \pkg{grid} package), 11 | in order to be able to use those in slots of its own classes. 12 | } 13 | \section{Objects from the Class}{A virtual Class: No objects may be 14 | created from it.} 15 | \section{Extends}{ 16 | Class \code{"oldClass"}, directly. 17 | } 18 | \section{Methods}{ 19 | No methods defined with class "unit" in the signature. 20 | } 21 | % \seealso{ 22 | % add link to grid ?? 23 | % } 24 | \keyword{classes} 25 | -------------------------------------------------------------------------------- /src/reg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern void F77_NAME(hbin )(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *); 5 | extern void F77_NAME(herode)(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *); 6 | extern void F77_NAME(hsm )(void *, void *, void *, void *, void *, void *, void *); 7 | 8 | 9 | static const R_FortranMethodDef FortranEntries[] = { 10 | {"hbin", (DL_FUNC) &F77_NAME(hbin), 13}, 11 | {"herode", (DL_FUNC) &F77_NAME(herode), 10}, 12 | {"hsm", (DL_FUNC) &F77_NAME(hsm), 7}, 13 | {NULL, NULL, 0} 14 | }; 15 | 16 | void R_init_hexbin(DllInfo *info) { 17 | R_registerRoutines(info, NULL, NULL, FortranEntries, NULL); 18 | R_useDynamicSymbols(info, FALSE); 19 | R_forceSymbols(info, TRUE); 20 | } 21 | -------------------------------------------------------------------------------- /man/inout.hex.Rd: -------------------------------------------------------------------------------- 1 | \name{inout.hex} 2 | \alias{inout.hex} 3 | \title{Check points for inclusion} 4 | \description{ 5 | Check which points are in hexagons with \code{count} <= mincnt. 6 | } 7 | \usage{ 8 | inout.hex(hbin, mincnt) 9 | } 10 | 11 | \arguments{ 12 | \item{hbin}{an object of class \code{\link{hexbin}}.} 13 | \item{mincnt}{Cutoff, id's for counts less than mincnt are returned} 14 | } 15 | \details{ 16 | Check which points are in hexagons with \code{count} <= mincnt and 17 | returns the row ids for those points. One can use the ids to plot low 18 | ount hexagons as points instead. 19 | } 20 | \value{ 21 | A vector with the row ids of points which fall in hexagons with 22 | \code{count} less than or equal to mincnt 23 | } 24 | 25 | \author{Nicholas Lewin-Koh} 26 | 27 | 28 | \seealso{\code{\link{plotMAhex}}} 29 | 30 | \keyword{misc} 31 | 32 | -------------------------------------------------------------------------------- /man/getHMedian.Rd: -------------------------------------------------------------------------------- 1 | \name{getHMedian} 2 | \alias{getHMedian} 3 | \alias{getHMedian,erodebin-method} 4 | \title{Get coordiantes of the median cell after the erode operation} 5 | \description{ 6 | A method for a eroded hexbin object to extract the coordinates of the 7 | median cell. The median is simply the cell with the highest erosion 8 | number or the last cell to be eroded. 9 | } 10 | \usage{ 11 | getHMedian(ebin) 12 | } 13 | \arguments{ 14 | \item{ebin}{result of \code{\link{erode.hexbin}()}.} 15 | } 16 | \section{Methods}{ 17 | \describe{ 18 | \item{ebin = "erodebin"}{...} 19 | } 20 | } 21 | \seealso{\code{\link{erode.hexbin}} 22 | } 23 | \examples{ 24 | set.seed(153) 25 | x <- rnorm(10000) 26 | y <- rnorm(10000) 27 | bin <- hexbin(x,y) 28 | 29 | smbin <- smooth.hexbin(bin) 30 | erodebin <- erode.hexbin(smbin, cdfcut=.5) 31 | getHMedian(erodebin) 32 | } 33 | \keyword{methods} 34 | 35 | -------------------------------------------------------------------------------- /tests/large.R: -------------------------------------------------------------------------------- 1 | library(hexbin) 2 | 3 | if(FALSE) { ## the following is still quite a bit from working/useful : 4 | 5 | ## what should that do? set a palette? 6 | rgb <- matrix(c( 7 | 15,15,15, 8 | 9 | 0, 0, 0, 10 | 1, 9,15, 11 | 9,15, 9, 12 | 15, 9, 9, 13 | 14 | 0, 0, 0, 15 | 0, 0, 0, 16 | 0, 0, 0, 17 | 0, 0, 0, 18 | 0, 0, 0, 19 | 0, 0, 0, 20 | 21 | 9, 9, 9, 22 | 0, 2, 7, 23 | 0, 7, 1, 24 | 8, 1, 1, 25 | 26 | 15, 2, 2, 27 | 11, 1, 1, 28 | 8, 1, 1, 29 | 5, 1, 1, 30 | 5, 1, 1, 31 | 15,15,15), ncol = 3, byrow = TRUE) 32 | 33 | ##ps.options(rasters=600,color=rgb/15,background=2) 34 | ##ps.options(color=rgb/15,background=2) 35 | postscript("large.ps",width = 10,height = 7.5) 36 | 37 | plot.hexbin(ans.25mil, style = "nest", lcex = .9) 38 | 39 | }## FALSE, i.e. nothing done 40 | -------------------------------------------------------------------------------- /man/hsmooth-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{hsmooth-methods} 2 | \docType{methods}% + generic -- still use this doctype ? 3 | \alias{hsmooth}% generic 4 | \alias{hsmooth-methods} 5 | \alias{hsmooth,hexbin-method} 6 | \title{Hexagon Bin Smoothing: Generic hsmooth() and Methods} 7 | \description{ 8 | Methods for the generic function \code{hsmooth} in package 9 | \pkg{hexbin}: 10 | There is currently only the one for \code{\link{hexbin}} objects. 11 | } 12 | \usage{ 13 | \S4method{hsmooth}{hexbin}(bin, wts) 14 | } 15 | \arguments{ 16 | \item{bin}{a \code{\link{hexbin}} object, or an extension such as 17 | \code{\link{erodebin-class}}.} 18 | \item{wts}{weights vector, see \code{\link{smooth.hexbin}}} 19 | } 20 | \section{Methods}{ 21 | \describe{ 22 | \item{bin = "hexbin"}{is just the \code{\link{smooth.hexbin}} 23 | function (for back compatibility); see its documentation, also for 24 | examples.} 25 | } 26 | } 27 | \keyword{methods} 28 | 29 | -------------------------------------------------------------------------------- /man/pushHexport.Rd: -------------------------------------------------------------------------------- 1 | \name{pushHexport} 2 | \alias{pushHexport} 3 | \title{Push a Hexagon Viewport ("hexVP")} 4 | \description{ 5 | Push a Hexagon Viewport (\code{"hexVP"}, see \link{hexVP-class}) on to 6 | the tree of (grid) viewports, calling 7 | \code{\link[grid:viewports]{pushViewport}}. 8 | } 9 | \usage{ 10 | pushHexport(hvp, clip = "off") 11 | } 12 | \arguments{ 13 | \item{hvp}{a hexagon viewport, i.e., an object of class 14 | \code{"hexVP"}, see \link{hexVP-class}, typically produced by 15 | \code{\link{hexViewport}(..)}.} 16 | \item{clip}{which viewport to push, either 'on' or 'off' are the 17 | allowed arguments, see details.} 18 | } 19 | \seealso{the underlying \code{\link[grid:viewports]{pushViewport}} from the 20 | \pkg{grid} package. 21 | } 22 | \details{ 23 | A hexagon viewport (\code{"hexVP"}) object has slots for two replicate 24 | viewports one with clipping turned on and one with clipping off. This 25 | allows toggling the clipping option. 26 | } 27 | %\examples{ 28 | %} 29 | \keyword{dplot} 30 | -------------------------------------------------------------------------------- /tests/hdiffplot.R: -------------------------------------------------------------------------------- 1 | library(hexbin) 2 | 3 | if(R.version$major != "1" || as.numeric(R.version$minor) >= 7) 4 | RNGversion("1.6") 5 | set.seed(213) 6 | x1 <- rnorm(10000) 7 | y1 <- rnorm(10000) 8 | 9 | x2 <- rnorm(10000,mean = .3) 10 | y2 <- rnorm(10000,mean = .3) 11 | 12 | rx <- range(x1,x2) 13 | ry <- range(y1,y2) 14 | 15 | str(bin1 <- hexbin(x1,y1, xbnds = rx, ybnds = ry)) 16 | str(bin2 <- hexbin(x2,y2, xbnds = rx, ybnds = ry)) 17 | 18 | str(erode(bin1)) 19 | 20 | str(smbin1 <- smooth.hexbin(bin1)) 21 | (smbin2 <- smooth.hexbin(bin2)) 22 | 23 | str(erodebin1 <- erode.hexbin(smbin1)) 24 | (erodebin2 <- erode.hexbin(smbin2)) 25 | 26 | if(FALSE)## does not work -- what funny stuff is hdiffplot() doing??? 27 | par(mfrow = c(2,1)) 28 | 29 | if(exists("hdiffplot", mode="function")) { ## not yet in new hexbin 30 | hdiffplot(bin1,bin2, main = "Original N(0,*) Random bins") 31 | 32 | hdiffplot(smbin1,smbin2, main = "smooth.hexbin() smoothed bins") 33 | 34 | plot.new() 35 | hdiffplot(erodebin1,erodebin2, main = "erode.hexbin()d smoothed bins") 36 | }# not yet 37 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | -------------- 2 | Aug 10, 2014 3 | o get rid of functions in lattice.R, which were copied from lattice 4 | 5 | -------------- 6 | o The new 3D plots -- should rather make the new functions "internal" 7 | and choosable with *arguments* from given functions ! 8 | 9 | o hexbin *class* {as mentioned by man/hexbin.Rd } -- done 10 | 11 | o find the references (on paper) and read ! 12 | --> "References" in ./Biocore-notes 13 | 14 | ftp://www.galaxy.gmu.edu/pub/faculty/dcarr/eda/bin2d/ 15 | ftp://www.galaxy.gmu.edu/pub/faculty/dcarr/software/bin2d.rev/ 16 | 17 | 18 | o example(hmatplot) is still doing many pages instead of one 19 | 20 | 21 | ------------ 22 | March 5, 2005 23 | o Implement conversions between different hexagon coordinate systems 24 | 25 | o Smoothing on a hexagonal basis using tensor products 26 | + smoothing histograms 27 | + smoothing the intensity of a Poisson process 28 | 29 | o Family of hex apply functions 30 | 31 | o Hbin list class and constructors 32 | 33 | --- 34 | 35 | o Use standard convertColor() function more and 36 | and try to get rid of dependency on 'colorspace' 37 | -------------------------------------------------------------------------------- /tests/viewp-ex.Rout.save: -------------------------------------------------------------------------------- 1 | 2 | R version 3.1.1 (2014-07-10) -- "Sock it to Me" 3 | Copyright (C) 2014 The R Foundation for Statistical Computing 4 | Platform: x86_64-pc-linux-gnu (64-bit) 5 | 6 | R is free software and comes with ABSOLUTELY NO WARRANTY. 7 | You are welcome to redistribute it under certain conditions. 8 | Type 'license()' or 'licence()' for distribution details. 9 | 10 | R is a collaborative project with many contributors. 11 | Type 'contributors()' for more information and 12 | 'citation()' on how to cite R or R packages in publications. 13 | 14 | Type 'demo()' for some demos, 'help()' for on-line help, or 15 | 'help.start()' for an HTML browser interface to help. 16 | Type 'q()' to quit R. 17 | 18 | > library(hexbin) 19 | > 20 | > ## a variation on Nicholas' post to bioconductor & example(hexViewport) 21 | > set.seed(545) 22 | > x <- rnorm(2^15) 23 | > y <- 3*x - .2*x^2 + rnorm(2^15) 24 | > hbin <- hexbin(x,y) 25 | > 26 | > ## 27 | > hp <- hexViewport(hbin, newpage = TRUE) 28 | > pushHexport(hp) 29 | > library("grid") 30 | > grid.rect() 31 | > grid.xaxis() 32 | > grid.yaxis() 33 | > grid.hexagons(hbin, style = "centroid") 34 | > hloess <- loess(y ~ x, data = hcell2xy(hbin), weights = hbin @ count) 35 | > xx <- seq(hbin@xbnds[1], hbin@xbnds[2], length = 500) 36 | > grid.lines(xx, predict(hloess, xx), 37 | + gp = gpar(col = 'red', lwd = 2), default.units = "native") 38 | > popViewport() 39 | > 40 | > proc.time() 41 | user system elapsed 42 | 0.368 0.020 0.379 43 | -------------------------------------------------------------------------------- /R/smoothHexbin.R: -------------------------------------------------------------------------------- 1 | setClass("smoothbin", 2 | representation("hexbin", wts="numeric")) 3 | 4 | setGeneric("hsmooth", function(bin, wts) standardGeneric("hsmooth")) 5 | 6 | smooth.hexbin <- function(bin, wts = c(48, 4, 1)) 7 | { 8 | if(!is(bin,"hexbin")) 9 | stop("first argument must be a hexbin object") 10 | cell <- bin@cell - 1 11 | n <- as.integer(length(cell)) 12 | cnt <- bin@count 13 | xbins <- bin@xbins 14 | bdim <- bin@dimen 15 | row <- bdim[1] 16 | col <- bdim[2] 17 | ncol <- col + 4 18 | nrow <- row + 4 19 | nmax <- ncol * nrow 20 | sm <- rep.int(0:0, nmax) 21 | nr <- cell %/% col + 2 22 | nc <- cell %% col + 3 23 | pad <- rep.int(0:0, nmax - n) 24 | cell <- c(nr * ncol + nc, pad) 25 | cnt <- c(cnt, pad) 26 | ans <- .Fortran(`hsm`, 27 | cell = as.integer(cell), 28 | cnt = as.integer(cnt), 29 | n = n, 30 | nmax = as.integer(nmax), 31 | sm = as.integer(sm), 32 | ncol = as.integer(ncol), 33 | wts = as.integer(wts))[c("cell","cnt","n")] 34 | n <- ans$n 35 | length(ans$cell) <- length(ans$cnt) <- n 36 | bin@xbins <- xbins + 4 37 | bin@xbnds <- addBit(bin@xbnds, f = 2/xbins) 38 | bin@ybnds <- addBit(bin@ybnds, f = 2/xbins) 39 | bin@dimen <- c(nrow, ncol) 40 | bin@cell <- ans$cell 41 | bin@count <- ans$cnt 42 | new("smoothbin", bin, wts=wts) 43 | } 44 | 45 | setMethod("hsmooth", "hexbin", smooth.hexbin) 46 | -------------------------------------------------------------------------------- /tests/hray.Rout.save: -------------------------------------------------------------------------------- 1 | 2 | R version 3.1.1 (2014-07-10) -- "Sock it to Me" 3 | Copyright (C) 2014 The R Foundation for Statistical Computing 4 | Platform: x86_64-pc-linux-gnu (64-bit) 5 | 6 | R is free software and comes with ABSOLUTELY NO WARRANTY. 7 | You are welcome to redistribute it under certain conditions. 8 | Type 'license()' or 'licence()' for distribution details. 9 | 10 | R is a collaborative project with many contributors. 11 | Type 'contributors()' for more information and 12 | 'citation()' on how to cite R or R packages in publications. 13 | 14 | Type 'demo()' for some demos, 'help()' for on-line help, or 15 | 'help.start()' for an HTML browser interface to help. 16 | Type 'q()' to quit R. 17 | 18 | > library(hexbin) 19 | > 20 | > set.seed(572) 21 | > 22 | > x <- rnorm(100) 23 | > y <- rnorm(100) 24 | > val <- rnorm(100) 25 | > inc <- abs(rnorm(100,sd = .3)) 26 | > loB <- val-inc 27 | > hiB <- val+inc 28 | > 29 | > if(exists("hray", mode="function")) { # 'real soon now' 30 | + 31 | + ## no confidence bounds 32 | + plot(x,y,type = 'n') 33 | + hray(x,y,val) 34 | + 35 | + ## confidence bounds 36 | + plot(x,y,type = 'n') 37 | + hray(x,y,val, lo = loB, hi = hiB) 38 | + 39 | + ## clockwise orientation 40 | + plot(x,y,type = 'n') 41 | + hray(x,y,val, loB, hiB, clockwise = TRUE) 42 | + 43 | + ## no tics and small filled dots 44 | + plot(x,y,type = 'n') 45 | + hray(x,y,val, loB, hiB, ticlength = FALSE, 46 | + dotside = 20, dotlength = .025, dotden = -1) 47 | + 48 | + } 49 | > 50 | > proc.time() 51 | user system elapsed 52 | 0.252 0.012 0.258 53 | -------------------------------------------------------------------------------- /man/hcell2xyInt.Rd: -------------------------------------------------------------------------------- 1 | \name{hcell2xyInt} 2 | \alias{hcell2xyInt} 3 | %- Also NEED an '\alias' for EACH other topic documented here. 4 | \title{Change cell ids to 2d integer coordinate system} 5 | \description{ 6 | Transforms the cell representation of a a lattice into a 2d integer 7 | coordinate system. 8 | } 9 | \usage{ 10 | hcell2xyInt(hbin, xbins=NULL, xbnds=NULL, ybnds=NULL, shape=NULL) 11 | } 12 | 13 | \arguments{ 14 | \item{hbin}{a object of class \code{"hexbin"}, typically produced by 15 | \code{\link{hexbin}(*)}.} 16 | \item{xbins}{the number of bins partitioning the range of xbnds.} 17 | \item{xbnds, ybnds}{horizontal and vertical limits of the binning 18 | region in x or y units respectively; must be numeric vector of 19 | length 2.} 20 | \item{shape}{the \emph{shape} = yheight/xwidth of the plotting regions.} 21 | 22 | } 23 | \details{ 24 | Takes a grid defined by either the hexbin parameters or dimen in a 25 | hexbin object and translates the cell ids for the grid into 2d integer 26 | coordinates. 27 | } 28 | \value{ 29 | An integer matrix with two columns, i and j representing the integer 30 | xy coordinates of the hexagon grid. 31 | \item{i}{Integer coordiante of the rows, increases from bottom to top} 32 | \item{j}{Integer coordiante of the columns, increases from left to right} 33 | } 34 | 35 | \author{Nicholas Lewin-Koh } 36 | 37 | 38 | \seealso{\code{\link{hcell2xy}}} 39 | \examples{ 40 | x<-rnorm(10000) 41 | y<-rnorm(10000) 42 | hbin<-hexbin(x,y) 43 | ijInt<-hcell2xyInt(hbin) 44 | } 45 | 46 | \keyword{dplot} 47 | \keyword{misc} 48 | -------------------------------------------------------------------------------- /R/hexpanel.R: -------------------------------------------------------------------------------- 1 | panel.hexloess <- 2 | function(bin, w=NULL, span = 2/3, degree = 1, family = c("symmetric", 3 | "gaussian"), evaluation = 50, lwd = add.line$lwd, lty = add.line$lty, 4 | col, col.line = add.line$col, ...) 5 | { 6 | stop("panel.hexloess is no longer available") 7 | add.line <- trellis.par.get("add.line") 8 | 9 | ## x <- bin@xcm 10 | ## y <- bin@ycm 11 | ## if(is.null(w))w <- bin@count 12 | ## control <- loess.control(...) 13 | ## notna <- !(is.na(x) | is.na(y)) 14 | ## new.x <- seq(min(x[notna]), max(x[notna]), length = evaluation) 15 | ## family <- match.arg(family) 16 | ## iterations <- if (family == "gaussian") 1 else control$iterations 17 | ## fit <- stats:::simpleLoess(y, x, w, span, degree, FALSE, FALSE, 18 | ## normalize = FALSE, "none", "interpolate", 19 | ## control$cell, iterations, control$trace.hat) 20 | ## kd <- fit$kd 21 | ## z <- .C("loess_ifit", as.integer(kd$parameter), as.integer(kd$a), 22 | ## as.double(kd$xi), as.double(kd$vert), as.double(kd$vval), 23 | ## as.integer(evaluation), as.double(x), fit = double(evaluation), 24 | ## PACKAGE = "stats")$fit 25 | ## if (length(x) > 0) { 26 | ## if (!missing(col) && missing(col.line)) { 27 | ## col.line <- col 28 | ## } 29 | ## add.line <- trellis.par.get("add.line") 30 | ## panel.lines(new.x, z, col = col.line, lty = lty, lwd = lwd) 31 | ## } 32 | } 33 | 34 | panel.hexgrid <- function(h, border=grey(.85)) 35 | { 36 | hexGraphPaper(h,border=border) 37 | } 38 | -------------------------------------------------------------------------------- /src/hcell.f: -------------------------------------------------------------------------------- 1 | subroutine hcell(x,y,cell,n,size,shape,rx,ry,bnd) 2 | C Copyright 1991 3 | C Version Date: September 16, 1994 4 | C Programmer: Dan Carr 5 | C Indexing: Left to right, bottom to top 6 | C bnd(1) rows, bnd(2) columns 7 | C Output: cell ids for none empty cells, revised bnd(1) 8 | 9 | c implicit none 10 | integer n, cell(1), bnd(2) 11 | double precision x(1), y(1), rx(2), ry(2), size, shape 12 | integer i, i1, i2, iinc 13 | integer j1, j2, jinc 14 | integer L, lat, celmax 15 | double precision c1, c2, con1, con2, dist1 16 | double precision sx, sy, xmin, ymin, xr, yr 17 | 18 | C_______Constants for scaling the data_____________________________ 19 | 20 | xmin = rx(1) 21 | ymin = ry(1) 22 | xr = rx(2)-xmin 23 | yr = ry(2)-ymin 24 | c1 = size/xr 25 | c2 = size*shape/(yr*sqrt(3.)) 26 | 27 | jinc= bnd(2) 28 | lat=jinc+1 29 | iinc= 2*jinc 30 | con1=.25 31 | con2=1./3. 32 | celmax=0 33 | 34 | C_______Binning loop________________________________________ 35 | 36 | do i=1,n 37 | sx = c1 * (x(i) - xmin) 38 | sy = c2 * (y(i) - ymin) 39 | j1 = sx+.5 40 | i1 = sy+.5 41 | dist1=(sx-j1)**2 + 3.*(sy-i1)**2 42 | 43 | if(dist1.lt.con1)then 44 | L=i1*iinc+j1+1 45 | elseif(dist1.gt.con2)then 46 | L=int(sy)*iinc + int(sx)+lat 47 | else 48 | j2 = sx 49 | i2 = sy 50 | if( dist1.le.(sx-j2-.5)**2 + 3. * (sy - i2 -.5)**2) then 51 | L=i1*iinc+j1+1 52 | else 53 | L=i2*iinc+j2+lat 54 | endif 55 | endif 56 | 57 | cell(i)=L 58 | celmax = max(celmax,L) 59 | enddo 60 | bnd(1)=(celmax-1)/bnd(2)+1 61 | return 62 | end 63 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: hexbin 2 | Version: 1.28.4 3 | Title: Hexagonal Binning Routines 4 | Authors@R: c(person(given = "Dan", 5 | family = "Carr", 6 | role = "aut", 7 | email = "dcarr@voxel.galaxy.gmu.edu"), 8 | person(given = c("Nicholas"), 9 | family = "Lewin-Koh", 10 | role = "aut"), 11 | person(given = "Martin", 12 | family = "Maechler", 13 | role = "aut", 14 | email = "maechler@stat.math.ethz.ch"), 15 | person(given = "Deepayan", 16 | family = "Sarkar", 17 | role = "aut", 18 | email = "deepayan.sarkar@r-project.org"), 19 | person(given = "Edzer", 20 | family = "Pebesma", 21 | role = "cre", 22 | comment = c(ORCID = "0000-0001-8049-7069"), 23 | email = "edzer.pebesma@uni-muenster.de")) 24 | Depends: R (>= 2.0.1), methods 25 | Imports: lattice, grid, graphics, grDevices, stats, utils 26 | Suggests: marray, affy, Biobase, limma, knitr 27 | Description: Binning and plotting functions for hexagonal bins. 28 | Collate: lattice.R BTC.R BTY.R grid.hexagons.R grid.hexlegend.R hbox.R 29 | hdiffplot.R hexbinList.R hexbinplot.R hexbin.s4.R hexpanel.R 30 | hexplom.R hexPlotMA.R hexutil.R hexViewport.R HO.R LINGRAY.R 31 | LOCS.R MAG.R RB.R smoothHexbin.R 32 | License: GPL-2 33 | VignetteBuilder: knitr 34 | NeedsCompilation: yes 35 | URL: https://github.com/edzer/hexbin 36 | -------------------------------------------------------------------------------- /man/panel.hexloess.Rd: -------------------------------------------------------------------------------- 1 | \name{panel.hexloess} 2 | \alias{panel.hexloess} 3 | \title{Loess line for hexbin lattice plot} 4 | \description{ 5 | A panel function to add a loess line to a hexbin lattice plot. 6 | 7 | This function contravened CRAN policy and is no longer available. 8 | } 9 | \usage{ 10 | panel.hexloess(bin, w = NULL, span = 2/3, degree = 1, 11 | family = c("symmetric", "gaussian"), evaluation = 50, 12 | lwd = add.line$lwd, lty = add.line$lty, 13 | col, col.line = add.line$col, \dots) 14 | 15 | } 16 | \arguments{ 17 | \item{bin}{an object of class \code{hexbin}.} 18 | \item{w}{optional counts for object \code{bin}.} 19 | \item{span}{smoothness parameter for \code{loess}.} 20 | \item{degree}{degree of local polynomial used.} 21 | \item{family}{if \code{"gaussian"} fitting is by least-squares, and 22 | if \code{"symmetric"} a re-descending M-estimator is used.} 23 | \item{evaluation}{number of points at which to evaluate the smooth curve.} 24 | \item{lwd}{line weight graphical parameter.} 25 | \item{lty}{line type graphical parameter.} 26 | \item{col}{same as \code{col.line}.} 27 | \item{col.line}{line color graphical parameter.} 28 | \item{\dots}{optional arguments to \code{\link[stats]{loess.control}}.} 29 | } 30 | \value{ 31 | There is no return value from this function. The results are plotted on 32 | the current active device. 33 | } 34 | \author{Nicholas Lewin-Koh \email{nikko@hailmail.net}} 35 | \seealso{ 36 | \code{\link{hexbinplot}}, \code{\link{panel.hexgrid}}, 37 | \code{\link{loess.smooth}}, 38 | \code{\link{loess.control}}, 39 | \code{\link[lattice:panel.functions]{panel.loess}} 40 | } 41 | \keyword{hplot} 42 | -------------------------------------------------------------------------------- /man/hexVP.abline.Rd: -------------------------------------------------------------------------------- 1 | \name{hexVP.abline} 2 | \alias{hexVP.abline} 3 | \title{Add a Straight Line to a HexPlot} 4 | \description{ 5 | This function adds one or more straight lines through the current 6 | plot; it is the hexbin version of \code{\link[graphics]{abline}()}. 7 | } 8 | \usage{ 9 | hexVP.abline(hvp, a = NULL, b = NULL, h = numeric(0), v = numeric(0), 10 | col = "black", lty = 1, lwd = 2, \dots) 11 | } 12 | \arguments{ 13 | \item{hvp}{A hexViewport object that is currently on the active device} 14 | \item{a,b}{the intercept and slope or if \code{b} is \code{NULL}, 15 | an \code{lm} object or a vector of length 2 with 16 | \code{c(intercept,slope)}} 17 | \item{h}{the y-value for a horizontal line.} 18 | \item{v}{the x-value for a vertical line.} 19 | \item{col, lty, lwd}{line color, type and width.} 20 | \item{\dots}{further graphical parameters.} 21 | } 22 | \details{ 23 | The first form specifies the line in intercept/slope form 24 | (alternatively \code{a} can be specified on its own and is taken to 25 | contain the slope and intercept in vector form). 26 | 27 | The \code{h=} and \code{v=} forms draw horizontal and vertical 28 | lines at the specified coordinates. 29 | 30 | The \code{coef} form specifies the line by a vector containing the 31 | slope and intercept. 32 | 33 | \code{lm} is a regression object which contains \code{reg$coef}. If it is 34 | of length 1 then the value is taken to be the slope of a line 35 | through the origin, otherwise, the first 2 values are taken to be 36 | the intercept and slope. 37 | } 38 | \author{Nicholas Lewin-Koh} 39 | 40 | \seealso{\code{\link{gplot.hexbin}}, \code{\link{hexViewport}}, 41 | \code{\link{hexMA.loess}} 42 | } 43 | \keyword{aplot} 44 | 45 | -------------------------------------------------------------------------------- /man/hexList.Rd: -------------------------------------------------------------------------------- 1 | \name{hexList} 2 | \alias{hexList} 3 | \alias{hexbinList-class} 4 | \alias{coerce,list,hexbinList-method} 5 | \title{Conditional Bivariate Binning into Hexagon Cells } 6 | \description{ 7 | Creates a list of \code{\link{hexbin}} objects. Basic components are 8 | a cell id and a count of points falling in each occupied cell. 9 | Basic methods are \code{\link[methods]{show}()}, \code{plot()} %(\link{plot.hexbin}) 10 | and \code{\link{summary}()}, but also \code{\link{erode}}. 11 | % .. \code{\link{smooth.hexbin}} 12 | } 13 | \usage{ 14 | hexList(x, y = NULL, given = NULL, xbins = 30, shape = 1, 15 | xbnds = NULL, ybnds = NULL, xlab = NULL, ylab = NULL) 16 | } 17 | %- maybe also 'usage' for other objects documented here. 18 | \arguments{ 19 | \item{x}{ x coordinate to be binned } 20 | \item{y}{ y coordinate to be binned } 21 | \item{given}{ ..} 22 | \item{xbins}{ number of bins partitioning the range of xbnds} 23 | \item{shape}{ the \emph{shape} = yheight/xwidth of the plotting regions } 24 | \item{xbnds}{ horizontal limits of binning } 25 | \item{ybnds}{ vertical limits of binning } 26 | \item{xlab}{ character strings used as labels for \code{x} } 27 | \item{ylab}{ character strings used as labels for \code{y}} 28 | } 29 | \details{ 30 | There is also a \code{\link[methods:as]{coerce}} method to produce 31 | \code{hexbinList} objects from \code{\link{list}}s. 32 | %% i.e., \code{as(list(....), "hexbinList")} should work 33 | } 34 | \value{ 35 | 36 | If it is a LIST, use 37 | \item{comp1 }{Description of 'comp1'} 38 | \item{comp2 }{Description of 'comp2'} 39 | ... 40 | } 41 | 42 | \author{Nicholas Lewin-Koh} 43 | 44 | \seealso{\code{\link{hexbin}}, \code{\link{hdiffplot}} } 45 | \keyword{dplot} 46 | \keyword{misc} 47 | -------------------------------------------------------------------------------- /man/hexMA.loess.Rd: -------------------------------------------------------------------------------- 1 | \name{hexMA.loess} 2 | \alias{hexVP.loess} 3 | \alias{hexMA.loess} 4 | \title{Add Loess Fit to Hexplot } 5 | \description{ 6 | Fit a loess line using the hexagon centers of mass as the x and y 7 | coordinates and the cell counts as weights. 8 | } 9 | \usage{ 10 | hexMA.loess(pMA, span = 0.4, col = "red", n = 200, ...) 11 | hexVP.loess(hbin, hvp = NULL, span = 0.4, col = "red", n = 200, ...) 12 | } 13 | 14 | \arguments{ 15 | \item{hbin}{an object of class \code{hexbin}, see \code{\link{hexbin}}.} 16 | \item{hvp}{A \code{hexViewport} object.} 17 | \item{pMA}{the list returned by \code{\link{plotMAhex}}.} 18 | \item{span}{the parameter alpha which controls the degree of smoothing.} 19 | \item{col}{line color for the loess fit.} 20 | \item{n}{number of points at which the fit should be evaluated.} 21 | \item{...}{Additional graphical parameter settings for the \code{loess} line fit; see \code{\link[grid]{gpar}}.}} 22 | \value{ 23 | Returns invisibly the object associated with the loess fit. 24 | } 25 | 26 | \author{Nicholas Lewin-Koh } 27 | 28 | \seealso{ \code{\link{hexVP.abline}}, \code{\link{plotMAhex}}, 29 | \code{\link{gplot.hexbin}}, \code{\link{hexViewport}}; 30 | \code{\link{loess}} 31 | } 32 | \examples{ 33 | if(require(marray)){ 34 | data(swirl) 35 | %% the following had 'newpage = FALSE, ' -- why ?? 36 | hb <- plotMAhex(swirl[,1], main = "M vs A plot with hexagons", legend=0) 37 | hexVP.abline(hb$plot, h=0, col= gray(.6)) 38 | hexMA.loess(hb) 39 | } 40 | 41 | dat <- data.frame(x=rnorm(1000), y=rnorm(1000)) 42 | bin <- hexbin(dat$x, dat$y) 43 | hb <- plot(bin) 44 | hexVP.loess(bin, hvp = hb$plot.vp, span = 0.4, n = 200, col = "blue", lwd = 3, lty = "dashed") 45 | } 46 | \keyword{aplot} 47 | 48 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: R-CMD-check 10 | 11 | jobs: 12 | R-CMD-check: 13 | runs-on: ${{ matrix.config.os }} 14 | 15 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 16 | 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | config: 21 | - {os: macos-latest, r: 'release'} 22 | - {os: windows-latest, r: 'release'} 23 | - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} 24 | - {os: ubuntu-latest, r: 'release'} 25 | - {os: ubuntu-latest, r: 'oldrel-1'} 26 | 27 | env: 28 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 29 | R_KEEP_PKG_SOURCE: yes 30 | 31 | steps: 32 | - uses: actions/checkout@v4 33 | 34 | - uses: r-lib/actions/setup-tinytex@v2 35 | 36 | - name: Install additional LaTeX packages 37 | run: tlmgr install grfext 38 | 39 | - uses: r-lib/actions/setup-pandoc@v2 40 | 41 | - uses: r-lib/actions/setup-r@v2 42 | with: 43 | r-version: ${{ matrix.config.r }} 44 | http-user-agent: ${{ matrix.config.http-user-agent }} 45 | use-public-rspm: true 46 | 47 | - uses: r-lib/actions/setup-r-dependencies@v2 48 | with: 49 | extra-packages: any::rcmdcheck 50 | needs: check 51 | 52 | - uses: r-lib/actions/check-r-package@v2 53 | with: 54 | upload-snapshots: true 55 | build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")' 56 | -------------------------------------------------------------------------------- /tests/large.Rout.save: -------------------------------------------------------------------------------- 1 | 2 | R version 3.1.1 (2014-07-10) -- "Sock it to Me" 3 | Copyright (C) 2014 The R Foundation for Statistical Computing 4 | Platform: x86_64-pc-linux-gnu (64-bit) 5 | 6 | R is free software and comes with ABSOLUTELY NO WARRANTY. 7 | You are welcome to redistribute it under certain conditions. 8 | Type 'license()' or 'licence()' for distribution details. 9 | 10 | R is a collaborative project with many contributors. 11 | Type 'contributors()' for more information and 12 | 'citation()' on how to cite R or R packages in publications. 13 | 14 | Type 'demo()' for some demos, 'help()' for on-line help, or 15 | 'help.start()' for an HTML browser interface to help. 16 | Type 'q()' to quit R. 17 | 18 | > library(hexbin) 19 | > 20 | > if(FALSE) { ## the following is still quite a bit from working/useful : 21 | + 22 | + ## what should that do? set a palette? 23 | + rgb <- matrix(c( 24 | + 15,15,15, 25 | + 26 | + 0, 0, 0, 27 | + 1, 9,15, 28 | + 9,15, 9, 29 | + 15, 9, 9, 30 | + 31 | + 0, 0, 0, 32 | + 0, 0, 0, 33 | + 0, 0, 0, 34 | + 0, 0, 0, 35 | + 0, 0, 0, 36 | + 0, 0, 0, 37 | + 38 | + 9, 9, 9, 39 | + 0, 2, 7, 40 | + 0, 7, 1, 41 | + 8, 1, 1, 42 | + 43 | + 15, 2, 2, 44 | + 11, 1, 1, 45 | + 8, 1, 1, 46 | + 5, 1, 1, 47 | + 5, 1, 1, 48 | + 15,15,15), ncol = 3, byrow = TRUE) 49 | + 50 | + ##ps.options(rasters=600,color=rgb/15,background=2) 51 | + ##ps.options(color=rgb/15,background=2) 52 | + postscript("large.ps",width = 10,height = 7.5) 53 | + 54 | + plot.hexbin(ans.25mil, style = "nest", lcex = .9) 55 | + 56 | + }## FALSE, i.e. nothing done 57 | > 58 | > proc.time() 59 | user system elapsed 60 | 0.240 0.032 0.265 61 | -------------------------------------------------------------------------------- /man/optShape.Rd: -------------------------------------------------------------------------------- 1 | \name{optShape} 2 | \alias{optShape} 3 | %- Also NEED an '\alias' for EACH other topic documented here. 4 | \title{Optimal Shape Parameter for Hexbin Viewport} 5 | \description{ 6 | Takes a viewport or a given height and width and returns the shape 7 | parameter that will fill the specified plotting region with the 8 | appropriately shaped hexagons. If margins are specified the margins 9 | are subtracted from height and width before the shape parameter is 10 | specified. 11 | } 12 | \usage{ 13 | optShape(vp, height = NULL, width = NULL, mar = NULL) 14 | } 15 | \arguments{ 16 | \item{vp}{a \code{viewport} object, optional see details} 17 | \item{height}{the height of the plotting region, can be numeric or units} 18 | \item{width}{The width of the plotting region, can be numeric or units} 19 | \item{mar}{A four element numeric or units vector describing the 20 | margins in the order \code{c(bottom, left, top, right)}} 21 | } 22 | \value{ 23 | a scalar numeric value specifiyng \code{shape}. 24 | } 25 | \author{Nicholas Lewin-Koh} 26 | \section{Warning}{If a viewport is given as an argument it should 27 | already be pushed on the graphics device or it will have null units 28 | and a meaningless shape parameter will be returned. 29 | } 30 | \seealso{\code{\link{hexViewport}}, \code{\link{hexVP-class}}, 31 | \code{\link{hexbin}}} 32 | \examples{ 33 | x <- rgamma(10000,.9) 34 | m <- as.logical(rbinom(10000,1,.17)) 35 | x[m] <- -x[m] 36 | y <- rnorm(x,abs(x)) 37 | library("grid") 38 | vp <- plotViewport(xscale= range(x)+c(-.5,.5), 39 | yscale= range(y)+c(-.5,.5), 40 | default.units = "native") 41 | grid.newpage() 42 | pushViewport(vp) 43 | grid.rect() 44 | shape <- optShape(vp) 45 | shape 46 | hb <- hexbin(x,y,xbins=40,shape=shape) 47 | grid.hexagons(hb,colramp=BTY) 48 | } 49 | \keyword{dplot} 50 | 51 | -------------------------------------------------------------------------------- /man/colramp.Rd: -------------------------------------------------------------------------------- 1 | \name{ColorRamps} 2 | \title{Color Ramps on Perceptually Linear Scales} 3 | \alias{ColorRamps} 4 | \alias{LinGray} 5 | \alias{BTC} 6 | \alias{BTY} 7 | \alias{LinOCS} 8 | \alias{heat.ob} 9 | \alias{magent} 10 | \alias{plinrain} 11 | \description{ 12 | Functions for returning colors on perceptually linear scales, 13 | where steps correspond to \sQuote{just detectable differences}. 14 | } 15 | \usage{ 16 | LinGray (n, beg=1, end=92) 17 | BTC (n, beg=1, end=256) 18 | LinOCS (n, beg=1, end=256) 19 | heat.ob (n, beg=1, end=256) 20 | magent (n, beg=1, end=256) 21 | plinrain(n, beg=1, end=256) 22 | } 23 | \arguments{ 24 | \item{n}{number of colors to return from the ramp} 25 | \item{beg}{begining of ramp, integer from 1-255} 26 | \item{end}{end of ramp, integer from 1-255} 27 | } 28 | \value{ 29 | returns an array of colors 30 | } 31 | \details{ 32 | Several precalulated color ramps, that are on a perceptually linear 33 | color scale. A perceptually linear color scale is a scale where each 34 | jump corresponds to a \dQuote{just detectable difference} in color and the 35 | scale is percieved as linear by the human eye (emprically determined). 36 | 37 | When using the ramps, if \code{beg} is less than \code{end} the ramp 38 | will be reversed. 39 | } 40 | \references{ 41 | Haim Levkowitz (1997) 42 | \emph{Color Theory and Modeling for Computer Graphics, 43 | Visualization, and Multimedia Applications}. 44 | Kluwer Academic Publishers, Boston/London/Dordrecht. 45 | } 46 | \seealso{ 47 | \code{\link[grDevices:palettes]{rainbow}}, 48 | \code{\link[grDevices:palettes]{terrain.colors}}, 49 | \code{\link[grDevices]{rgb}}, 50 | \code{\link[grDevices]{hsv}} 51 | } 52 | \examples{ 53 | h <- hexbin(rnorm(10000),rnorm(10000)) 54 | plot(h, colramp= BTY) 55 | ## looks better if you shave the tails: 56 | plot(h, colramp= function(n){LinOCS(n,beg=15,end=225)}) 57 | } 58 | \author{Nicholas Lewin-Koh} 59 | \keyword{color} 60 | -------------------------------------------------------------------------------- /man/hexViewport.Rd: -------------------------------------------------------------------------------- 1 | \name{hexViewport} 2 | \alias{hexViewport} 3 | \title{Compute a Grid Viewport for Hexagon / Hexbin Graphics} 4 | \description{ 5 | Builds a \code{grid} viewport for hexagon or \code{\link{hexbin}} 6 | graphics. This builds on the concepts of the \pkg{grid} package, 7 | see \code{\link[grid]{viewport}}. 8 | } 9 | \usage{% see ../R/hexViewport.R 10 | hexViewport(x, offset = unit(0,"inches"), mar = NULL, 11 | xbnds = NULL, ybnds = NULL, newpage = FALSE, 12 | clip = "off", vp.name = NULL) 13 | } 14 | \arguments{ 15 | \item{x}{a \code{\link{hexbin}} object.} 16 | \item{offset}{a \code{\link[grid]{unit}} object.} 17 | \item{mar}{margins as \code{\link[grid]{unit}}s, of length 4 or 1.} 18 | \item{xbnds, ybnds}{bounds for x- and y- plotting range; these default 19 | to the corresponding slots of \code{x}.} 20 | \item{newpage}{logical indicating if a new graphics page should be 21 | openend, i.e., \code{\link[grid]{grid.newpage}()}.} 22 | \item{clip}{simply passed to \code{\link[grid]{viewport}()}.} 23 | \item{vp.name}{name of viewport; defaults to random name.} 24 | } 25 | \value{ 26 | an S4 object of class \code{"hexVP"}, see \link{hexVP-class} for more, 27 | with its main slot \code{hexVp} a \code{\link[grid]{viewport}} for 28 | grid graphics. 29 | } 30 | 31 | \seealso{\code{\link[grid]{viewport}} and the main 32 | \emph{\dQuote{handlers}} \code{\link{pushHexport}} and 33 | \code{\link[grid:viewports]{popViewport}}; further 34 | \code{\link{gplot.hexbin}} and \code{\link{hboxplot}} which build on 35 | \code{hexViewport}. 36 | } 37 | \examples{ 38 | set.seed(131) 39 | x <- rnorm(7777) 40 | y <- rt (7777, df=3) 41 | 42 | ## lower resolution binning and overplotting with counts 43 | bin <- hexbin(x,y,xbins=25) 44 | P <- plot(bin) 45 | xy <- hcell2xy(bin) 46 | pushHexport(P$plot.vp) 47 | i <- bin@count <= 3 48 | library("grid") 49 | grid.text(as.character(bin@count[i]), xy$x[i], xy$y[i], 50 | default.units = "native") 51 | grid.points(x[1:20],y[1:20]) # to show some points rather than counts 52 | popViewport() 53 | } 54 | \keyword{hplot}% ? 55 | \keyword{aplot} 56 | -------------------------------------------------------------------------------- /man/NHANES.Rd: -------------------------------------------------------------------------------- 1 | \name{NHANES} 2 | \alias{NHANES} 3 | \docType{data} 4 | \title{NHANES Data : National Health and Nutrition Examination Survey} 5 | \usage{data(NHANES)} 6 | \description{ 7 | This is a somewhat large interesting dataset, a data frame of 15 8 | variables (columns) on 9575 persons (rows). 9 | } 10 | \format{ 11 | This data frame contains the following columns: 12 | \describe{ 13 | \item{Cancer.Incidence}{binary factor with levels \code{No} and \code{Yes}.} 14 | \item{Cancer.Death}{binary factor with levels \code{No} and \code{Yes}.} 15 | \item{Age}{numeric vector giving age of the person in years.} 16 | \item{Smoke}{a factor with levels \code{Current}, \code{Past}, 17 | \code{Nonsmoker}, and \code{Unknown}.} 18 | \item{Ed}{numeric vector of \eqn{\{0,1\}} codes giving the education level.} 19 | \item{Race}{numeric vector of \eqn{\{0,1\}} codes giving the 20 | person's race.%% FIXME : 0 = ? 1 = ? 21 | } 22 | \item{Weight}{numeric vector giving the weight in kilograms} 23 | \item{BMI}{numeric vector giving Body Mass Index, i.e., 24 | \code{Weight/Height^2} where Height is in meters, and missings 25 | (61\% !) are coded as \code{0} originally.}%% rather FIXME? 26 | \item{Diet.Iron}{numeric giving Dietary iron.} 27 | \item{Albumin}{numeric giving albumin level in g/l.} 28 | \item{Serum.Iron}{numeric giving Serum iron in \eqn{\mu}{u}g/l.} 29 | \item{TIBC}{numeric giving Total Iron Binding Capacity in \eqn{\mu}{u}g/l.} 30 | \item{Transferin}{numeric giving Transferin Saturation which is just 31 | \code{100*serum.iron/TIBC}.} 32 | \item{Hemoglobin}{numeric giving Hemoglobin level.} 33 | \item{Sex}{a factor with levels \code{F} (female) and \code{M} (male).} 34 | } 35 | } 36 | \examples{ 37 | data(NHANES) 38 | summary(NHANES) 39 | ## Missing Data overview : 40 | nNA <- sapply(NHANES, function(x)sum(is.na(x))) 41 | cbind(nNA[nNA > 0]) 42 | # Which are just these 6 : 43 | \dontrun{ 44 | Diet.Iron 141 45 | Albumin 252 46 | Serum.Iron 1008 47 | TIBC 853 48 | Transferin 1019 49 | Hemoglobin 759 50 | }%dont 51 | } 52 | \keyword{datasets} 53 | -------------------------------------------------------------------------------- /man/hcell2xy.Rd: -------------------------------------------------------------------------------- 1 | \name{hcell2xy} 2 | \alias{hcell2xy} 3 | \alias{hcell2xy,hexbin-method} 4 | 5 | \title{Compute X and Y Coordinates for Hexagon Cells} 6 | 7 | \description{ 8 | Computes x and y coordinates from hexagon cell id's. 9 | } 10 | \usage{ 11 | hcell2xy(hbin, check.erosion = TRUE) 12 | } 13 | \arguments{ 14 | \item{hbin}{a object of class \code{"hexbin"}, typically produced by 15 | \code{\link{hexbin}(*)}.} 16 | \item{check.erosion}{logical indicating if only the eroded points 17 | should be returned in the case where \code{hbin} inherits from 18 | \code{"erodebin"} (see \code{\link{erodebin-class}}); is \code{TRUE} 19 | by default.} 20 | } 21 | \value{ 22 | A list with two components of the same length as \code{bin$cell}, 23 | \item{x}{} 24 | \item{y}{} 25 | } 26 | 27 | %%FIXME \references{see in \code{\link{hcell}}.} 28 | 29 | \details{ 30 | The hexbin object \code{hbin} contains all the needed information. 31 | The purpose of this function is to reduce storage. The cost is 32 | additional calculation. 33 | } 34 | 35 | \seealso{%%FIXME \code{\link{hcell}}, \code{\link{hray}}, 36 | \code{\link{hexbin}}. 37 | } 38 | 39 | \examples{ 40 | x <- rnorm(10000) 41 | y <- rnorm(10000) 42 | plot(x,y, pch=".") 43 | hbin <- hexbin(x,y) 44 | str(xys <- hcell2xy(hbin)) 45 | points(xys, cex=1.5, col=2) ; title("hcell2xy( hexbin(..) )", col.main=2) 46 | 47 | %% __________ FIXME ________ 48 | \dontshow{ 49 | ## Temporal trends with confidence bounds plotted on a map: 50 | ## Illustration only pending access to user functions 51 | ## mtapply() # like tapply but for matrices 52 | ## sens.season.slope() # computes sen's seasonal slope 53 | 54 | ## This part does not work and commented out 55 | #hbin <- hcell(dat$x,dat$y) # x and y are in map projection units 56 | #newdat < dat[,c('month','year','value')] # extract columns 57 | #stats <- mtapply(newdat,bin$cell,sens.season.slope,season=12) 58 | #plot(mymap,type='l') # map boundaries in map projection units 59 | #xy <- hcell2xy(hbin) # x and y coordinates for hexagon cell centers 60 | #hray(xy$x, xy$y,val=stat[,1],lower= stat[,2],upper=stat[,3]) 61 | } 62 | } 63 | \keyword{manip} 64 | -------------------------------------------------------------------------------- /man/hexTapply.Rd: -------------------------------------------------------------------------------- 1 | \name{hexTapply} 2 | \alias{hexTapply} 3 | 4 | \title{Apply function to data from each hexagon bin.} 5 | 6 | \description{ 7 | A wrapper for tapply except that it operates with each hexagon bin 8 | being the category. The function operates on the data associated on 9 | the points from each bin. 10 | } 11 | 12 | \usage{ 13 | hexTapply(hbin, dat, FUN = sum, ..., simplify=TRUE) 14 | } 15 | \arguments{ 16 | \item{hbin}{a object of class \code{"hexbin"}, typically produced by 17 | \code{\link{hexbin}(*)}.} 18 | \item{dat}{A vector of data the same length as \code{hbin@cID}} 19 | \item{FUN}{the function to be applied. In the case of functions like 20 | \code{+}, \code{\%*\%}, etc., the function name must be quoted. If 21 | \code{FUN} is \code{NULL}, tapply returns a vector which can be used 22 | to subscript the multi-way array \code{tapply} normally produces.} 23 | \item{\dots}{optional arguments to \code{FUN}.} 24 | \item{simplify}{If \code{FALSE}, \code{tapply} always returns an array 25 | of mode \code{"list"}. If \code{TRUE} (the default), then if 26 | \code{FUN} always returns a scalar, \code{tapply} returns an array 27 | with the mode of the scalar.} 28 | } 29 | \details{ 30 | This function is a wrapper for tapply, except that the cell id is 31 | always the categorical variable. This function is specifically good for 32 | adding variables to the cAtt slot of a hexbin object or for plotting 33 | a third variable in a hexagon plot. See below for examples. 34 | } 35 | 36 | \value{ 37 | Returns a vector of the result of 'FUN' as in 38 | \code{\link{tapply}}. See \code{\link{tapply}} for detailed 39 | description of output. 40 | } 41 | 42 | \author{Nicholas Lewin-Koh} 43 | \seealso{ \code{\link{tapply}},\code{\link{hexbin}} } 44 | \examples{ 45 | data(NHANES) 46 | hbin<-hexbin(log(NHANES$Diet.Iron+1),log(NHANES$BMI),xbins=25,IDs=TRUE) 47 | hvp<-plot(hbin) 48 | mtrans<-hexTapply(hbin,NHANES$Transferin,median,na.rm=TRUE) 49 | pushHexport(hvp$plot.vp) 50 | grid.hexagons(hbin,style='lattice',pen=0,border='red',use.count=FALSE, 51 | cell.at=mtrans) 52 | 53 | 54 | 55 | } 56 | \keyword{dplot} 57 | \keyword{utilities}% at least one, from doc/KEYWORDS 58 | 59 | -------------------------------------------------------------------------------- /man/panel.hexboxplot.Rd: -------------------------------------------------------------------------------- 1 | \name{panel.hexboxplot} 2 | \alias{panel.hexboxplot} 3 | \title{Boxplot for hexbin lattice plot} 4 | \description{ 5 | A panel function to add a boxplot to a hexbin lattice plot. 6 | } 7 | \usage{ 8 | panel.hexboxplot(x, y, xbins = 30, 9 | xbnds = c("data", "panel"), ybnds = c("data", "panel"), 10 | .prelim = FALSE, .cpl = current.panel.limits(), 11 | .xlim = .cpl$xlim, .ylim = .cpl$ylim, 12 | .aspect.ratio, type = character(0), cdfcut = 0.25, 13 | shadow = 0.05, ..., check.erosion = TRUE) 14 | } 15 | \arguments{ 16 | \item{x, y}{numeric vector or factor.} 17 | \item{xbins}{the number of bins partitioning the range of xbnds.} 18 | \item{xbnds, ybnds}{horizontal and vertical limits of the binning 19 | region in x or y units respectively; must be numeric vector of 20 | length 2.} 21 | \item{.prelim, .cpl, .xlim, .ylim, .aspect.ratio}{for internal use.} 22 | \item{type}{character vector controlling additional augmentation of 23 | the display. A \code{"g"} in \code{type} adds a reference grid, an 24 | \code{"hg"} adds a hexagonal grid.} 25 | \item{cdfcut}{number in (0,1) indicating the confidence level for the 26 | erosion limits. See \code{\link{erode.hexbin}} for more information.} 27 | \item{shadow}{number in (0,1) indicating the confidence level for the 28 | erosion limits of a boxplot shadow. See \code{\link{erode.hexbin}} 29 | for more information.} 30 | \item{\dots}{potential further arguments passed on.} 31 | \item{check.erosion}{logical indicating only eroded points should be 32 | used for \code{"erodebin"} objects; simply passed to 33 | \code{\link{hcell2xy}}, see its documentation.} 34 | } 35 | \value{ 36 | There is no return value from this function. The results are plotted on 37 | the current active device. 38 | } 39 | \author{Nicholas Lewin-Koh \email{nikko@hailmail.net}} 40 | \seealso{\code{\link{hexbinplot}}, \code{\link{panel.hexgrid}}, 41 | \code{\link[lattice]{panel.bwplot}} 42 | } 43 | \examples{ 44 | mixdata <- 45 | data.frame(x = c(rnorm(5000),rnorm(5000,4,1.5)), 46 | y = rep(1:2, 5000)) 47 | hexbinplot(y ~ x, mixdata, panel = panel.hexboxplot) 48 | } 49 | \keyword{hplot} 50 | -------------------------------------------------------------------------------- /src/hbin.f: -------------------------------------------------------------------------------- 1 | subroutine hbin(x,y,cell,cnt,xcm,ycm, size, shape, 2 | * rx,ry, bnd, n, cellid) 3 | 4 | C Copyright 1991 5 | C Version Date: September 16, 1994 6 | C Programmer: Dan Carr 7 | C Indexing: Left to right, bottom to top 8 | C bnd(1) rows, bnd(2) columns 9 | C Output: cell ids for non empty cells, revised bnd(1) 10 | 11 | c optionally also return cellid(1:n) 12 | c Copyright (2004) Nicholas Lewin-Koh and Martin Maechler 13 | 14 | implicit none 15 | 16 | integer n, nc, cell(*), cnt(*), bnd(2), cellid(*) 17 | c cellid(*): length 1 or n 18 | double precision x(n), y(n), xcm(*),ycm(*), rx(2),ry(2), size 19 | double precision shape 20 | integer i, i1, i2, iinc 21 | integer j1, j2, jinc 22 | integer L, lmax, lat 23 | double precision c1, c2, con1, con2, dist1 24 | double precision sx, sy, xmin, ymin, xr, yr 25 | logical keepID 26 | 27 | keepID = (cellid(1) .eq. 0) 28 | C_______Constants for scaling the data_____________________________ 29 | 30 | xmin = rx(1) 31 | ymin = ry(1) 32 | xr = rx(2)-xmin 33 | yr = ry(2)-ymin 34 | c1 = size/xr 35 | c2 = size*shape/(yr*sqrt(3.)) 36 | 37 | jinc= bnd(2) 38 | lat=jinc+1 39 | iinc= 2*jinc 40 | lmax=bnd(1)*bnd(2) 41 | con1=.25 42 | con2=1.0/3.0 43 | 44 | C_______Binning loop________________________________________ 45 | 46 | do i=1,n 47 | sx = c1 * (x(i) - xmin) 48 | sy = c2 * (y(i) - ymin) 49 | j1 = sx+.5 50 | i1 = sy+.5 51 | dist1=(sx-j1)**2 + 3.*(sy-i1)**2 52 | 53 | if(dist1 .lt. con1) then 54 | L=i1*iinc + j1+1 55 | elseif(dist1 .gt. con2) then 56 | L=int(sy)*iinc + int(sx)+lat 57 | else 58 | j2 = sx 59 | i2 = sy 60 | if(dist1 .le. (sx-j2 -.5)**2 + 3.*(sy-i2 -.5)**2) then 61 | L=i1*iinc+ j1+1 62 | else 63 | L=i2*iinc+ j2+lat 64 | endif 65 | endif 66 | 67 | cnt(L)=cnt(L)+1 68 | if (keepID) cellid(i)=L 69 | xcm(L)=xcm(L)+ (x(i)-xcm(L))/cnt(L) 70 | ycm(L)=ycm(L)+ (y(i)-ycm(L))/cnt(L) 71 | enddo 72 | 73 | C_______Compression of output________________________________________ 74 | 75 | nc=0 76 | do L=1,lmax 77 | if(cnt(L) .gt. 0) then 78 | nc=nc+1 79 | cell(nc)=L 80 | cnt(nc)=cnt(L) 81 | xcm(nc)=xcm(L) 82 | ycm(nc)=ycm(L) 83 | endif 84 | enddo 85 | n=nc 86 | bnd(1)=(cell(nc)-1)/bnd(2)+1 87 | return 88 | end 89 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 2009-08-09 Nicholas Lewin-Koh 2 | * R/grid.hexagons.R removed break statements, switch fails. Cow 3 | lattice and centroid can ramp colors. Bug submitted by Ricardo 4 | DeLemos" 5 | * man/hexVP-class.Rd Shape slot was not documented 6 | 7 | 2009-02-26 Nicholas Lewin-Koh 8 | * R/grid.hexagons.R fixed bad if construct 9 | * R/hexbin.s4.R changed xlab and ylab signatures in class hexbin 10 | from character to vector, patch submitted by Don Armstrong 11 | 12 | 2008-04-28 Martin Maechler 13 | 14 | * DESCRIPTION (Version): 1.13.4 15 | 16 | * man/*.Rd: fixes to several man pages; 17 | note that gplot.hexbin() now is *deprecated* ! 18 | 19 | 2008-03-18 Patrick Aboyoun 20 | 21 | * man/panel.hexboxplot, man/panel.hexgrid, man/panel.hexloess: Added more information to man files. 22 | * R/hexbinplot.R (panel.hexboxplot): removed unused singles argument. 23 | 24 | 2008-03-12 Patrick Aboyoun 25 | 26 | * R/hexViewport.R (hexVP.abline): Fixed the handling of non-model objects as input. 27 | 28 | 2008-02-28 Patrick Aboyoun 29 | 30 | * R/hexPlotMA.R (plotMAhex): Require users to specify status explicitly 31 | rather than as the component MA$genes$Status 32 | * R/hexPlotMA.R (plotMAhex): Replaced support of Biobase class exprSet with 33 | ExpressionSet 34 | 35 | 2006-09-28 Martin Maechler 36 | 37 | * NAMESPACE: add full list of colorspace dependencies 38 | 39 | 2005-07-26 Martin Maechler 40 | 41 | * R/hexViewport.R (smartBnds): some rationalization 42 | * R/hexViewport.R (rname): dito 43 | 44 | 2005-07-19 Martin Maechler 45 | 46 | * DESCRIPTION (Version): 1.3.1 (not to confuse with the previous one). 47 | 48 | * man/gplot.hexbin.Rd: fix typo and usage for S4method 49 | 50 | * R/hexPlotMA.R (hexMA.loess): add argument 'n' 51 | 52 | * R/hexViewport.R (hexVP.loess): add argument 'n'; other "white 53 | space cosmetic" in file 54 | 55 | 2005-10-21 Nicholas Lewin-Koh 56 | 57 | * added Deepayan Sarkar's hexbinplot.R function for lattice hexbin 58 | plots 59 | 60 | * Added my Hexplom function based on Deepayan's code 61 | 62 | 2005-10-27 Nicholas Lewin-Koh 63 | 64 | * Added more panel functions, for hexboxplots and hdiffplots. 65 | 66 | -------------------------------------------------------------------------------- /src/hsm.f: -------------------------------------------------------------------------------- 1 | C File: hsm.f 2 | C Programmer: Daniel B. Carr 3 | C Version Date: January 3, 1994 4 | C 5 | C This program is an hexagon cell smoother. It smooths into 6 | C neighboring cells and hence expands. 7 | 8 | C The kernal is a crude integer kernel. 9 | C The boundary hexagons get weight 1, the center hexagon 10 | C gets weight, wt, which by default is set to six. 11 | C 12 | C 13 | 14 | subroutine hsm(cell,cnt,n,nmax,sm,ncol,wt) 15 | 16 | implicit none 17 | 18 | integer n, nmax, ncol 19 | integer cell(*), cnt(*), sm(*), wt(*) 20 | integer ind, ind1(6), ind2(12),ind3(6), ind4(12), loc 21 | integer row, cnt1, cnt2, wta, wtb, wtc 22 | integer i, j 23 | 24 | C__________Constants___________________________________________ 25 | 26 | ind1(1)=-1 27 | ind1(2)=ncol-1 28 | ind1(3)=ncol 29 | ind1(4)=+1 30 | ind1(5)=-ncol 31 | ind1(6)=-ncol-1 32 | 33 | ind2(1)=-2 34 | ind2(2)=ncol-2 35 | ind2(3)=2*ncol-1 36 | ind2(4)=2*ncol 37 | ind2(5)=2*ncol+1 38 | ind2(6)=ncol+1 39 | ind2(7)=2 40 | ind2(8)=-ncol+1 41 | ind2(9)=-2*ncol+1 42 | ind2(10)=-2*ncol 43 | ind2(11)=-2*ncol-1 44 | ind2(12)=-ncol-2 45 | 46 | ind3(1)=-1 47 | ind3(2)=ncol 48 | ind3(3)=ncol+1 49 | ind3(4)=+1 50 | ind3(5)=-ncol+1 51 | ind3(6)=-ncol 52 | 53 | ind4(1)=-2 54 | ind4(2)=ncol-1 55 | ind4(3)=2*ncol-1 56 | ind4(4)=2*ncol 57 | ind4(5)=2*ncol+1 58 | ind4(6)=ncol+2 59 | ind4(7)=2 60 | ind4(8)=-ncol+2 61 | ind4(9)=-2*ncol+1 62 | ind4(10)=-2*ncol 63 | ind4(11)=-2*ncol-1 64 | ind4(12)=-ncol-1 65 | 66 | wta = wt(1) 67 | wtb = wt(2) 68 | wtc = wt(3) 69 | 70 | C_________Smoothing_____________________________________ 71 | 72 | do i=1,n 73 | sm(cell(i))=wta*cnt(i) 74 | enddo 75 | 76 | do i=1,n 77 | loc=cell(i) 78 | row=(loc-1)/ncol + 1 79 | cnt1=wtb*cnt(i) 80 | cnt2=wtc*cnt(i) 81 | 82 | if(mod(row,2).eq.1)then 83 | do j=1,6 84 | ind=loc+ind1(j) 85 | sm(ind)=sm(ind)+cnt1 86 | enddo 87 | do j=1,12 88 | ind=loc+ind2(j) 89 | sm(ind)=sm(ind)+cnt2 90 | enddo 91 | else 92 | do j=1,6 93 | ind=loc+ind3(j) 94 | sm(ind)=sm(ind)+cnt1 95 | enddo 96 | do j=1,12 97 | ind=loc+ind4(j) 98 | sm(ind)=sm(ind)+cnt2 99 | enddo 100 | endif 101 | enddo 102 | 103 | n=0 104 | do i=1,nmax 105 | if(sm(i).gt.0)then 106 | n=n+1 107 | cell(n)=i 108 | cnt(n)=sm(i) 109 | endif 110 | enddo 111 | return 112 | end 113 | 114 | 115 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | useDynLib(hexbin, .registration=TRUE) 2 | 3 | 4 | import(methods) 5 | import(grid) ## too many things from grid 6 | import(lattice) ## too many things from lattice 7 | importFrom(graphics, plot, polygon) 8 | importFrom(grDevices, col2rgb, gray, grey, hsv, rgb, rgb2hsv, 9 | xy.coords) 10 | importFrom(stats, coef, density, IQR, loess, loess.control, median, 11 | predict, update) 12 | importFrom(utils, modifyList, str) 13 | 14 | 15 | ## Generics and functions defined in this package 16 | export( 17 | "erode", 18 | "erode.hexbin", 19 | "getHMedian", 20 | ## document those; the method aliases are there: 21 | ## "getFig", "getMargins", "getPlt", "getXscale", "getYscale", 22 | "gplot.hexbin", 23 | "grid.hexagons", 24 | "grid.hexlegend", 25 | "hboxplot", 26 | "hcell2xy", 27 | "hexbin", 28 | "hexcoords", 29 | "hexList", 30 | "hexpolygon", 31 | "hexViewport", 32 | "hexVP.abline", 33 | "plotMAhex", 34 | "hexVP.loess", 35 | "hexMA.loess", 36 | "hsmooth", 37 | "list2hexList", 38 | "pushHexport", 39 | "smooth.hexbin", 40 | "hdiffplot", # but not all the helpers in ./R/hdiffplot.R 41 | ## Stuff in hexutils 42 | "hcell2xyInt", 43 | "hgridcent", 44 | "hexGraphPaper", 45 | "hexTapply", 46 | "optShape", 47 | "inout.hex", 48 | ## color stuff 49 | "BTC", "BTY", "LinGray", "LinOCS", "heat.ob", "magent","plinrain", 50 | 51 | ## Lattice stuff: 52 | 53 | ## high-level functions 54 | "hexbinplot", "hexplom", 55 | 56 | ## panel functions 57 | "panel.hexbinplot", "panel.hexplom", "panel.hexboxplot", 58 | "panel.hexgrid","panel.hexloess", 59 | 60 | ## utilities 61 | "hexlegendGrob") 62 | 63 | 64 | ## S3 methods for lattice-type functions 65 | 66 | S3method("hexbinplot", "formula") 67 | S3method("hexplom", "formula") 68 | S3method("hexplom", "matrix") 69 | S3method("hexplom", "data.frame") 70 | 71 | 72 | 73 | exportClasses("hexbin", 74 | "erodebin", 75 | "smoothbin", 76 | "hexVP", 77 | # could/should we keep this 'private' (?) : 78 | "integer or NULL", 79 | "hexbinList" 80 | ) 81 | 82 | exportMethods( 83 | "erode", 84 | ## undocumented: "getFig", "getMargins", "getPlt", "getXscale", "getYscale", 85 | "hsmooth", 86 | 87 | "plot", 88 | "summary", 89 | "show", 90 | "coerce" 91 | ) 92 | 93 | 94 | -------------------------------------------------------------------------------- /man/hexGraphPaper.Rd: -------------------------------------------------------------------------------- 1 | \name{hexGraphPaper} 2 | \alias{hexGraphPaper} 3 | \alias{hgridcent} 4 | \title{Create a Hexgon Grid} 5 | \description{ 6 | Creates a hexagon grid that can be added to a plot created with grid 7 | graphics. 8 | } 9 | \usage{ 10 | hexGraphPaper(hb, xbnds = NULL, ybnds = NULL, xbins = 30, shape = 1, 11 | add = TRUE, fill.edges = 1, fill = 0, border = 1) 12 | 13 | hgridcent(xbins, xbnds, ybnds, shape, edge.add = 0) 14 | } 15 | \arguments{ 16 | \item{hb}{a object of class \code{"hexbin"}, typically produced by 17 | \code{\link{hexbin}(*)}.} 18 | \item{xbnds, ybnds}{horizontal and vertical limits of the binning 19 | region in x or y units respectively; must be numeric vector of 20 | length 2.} 21 | \item{xbins}{the number of bins partitioning the range of xbnds.} 22 | \item{shape}{the \emph{shape} = yheight/xwidth of the plotting regions.} 23 | \item{add}{a logical value indicating whether or not to add the grid 24 | to the current plot.} 25 | \item{fill.edges}{integer number of hexagons to add around the border} 26 | \item{fill}{the fill color for the hexagons} 27 | \item{border}{the color of the border of the hexagons} 28 | \item{edge.add}{offset (typically \code{fill.edges} above) used in 29 | \code{hgridcent}.} 30 | } 31 | \details{ 32 | If a hexbin object is given then the parameters xbins and shape are 33 | ignored. Different bounds can still be specified. The \code{fill.edges} 34 | parameter should be an integer. \code{fill.edges} takes the current 35 | grid and adds a layer of hexagons around the grid for each level of 36 | fill. So for example if \code{fill.edges= 2} than the dimensions of 37 | the grid would be \code{(i,j)+4}. 38 | 39 | \code{hgridcent()} is the utility function computing the resulting 40 | list (see section \dQuote{Value}). 41 | 42 | \strong{WARNING! If using a hexVP be sure to set clip to "on", otherwise the 43 | hexagon grid will bleed over the plot edges.} 44 | } 45 | \value{ 46 | Invisibly returns a list with th following components 47 | \item{x}{The x coordinates of the grid} 48 | \item{y}{the y coordinates of the grid} 49 | \item{dimen}{a vector of length 2 gining the rows and columns of the grid} 50 | \item{dx}{the horizontal diameter of the hexagons} 51 | \item{dy}{the vertical diameter of the hexagons} 52 | } 53 | \author{Nicholas Lewin-Koh} 54 | \seealso{\code{\link{hcell2xy}}, \code{\link{hexpolygon}}, 55 | \code{\link{grid.hexagons}}} 56 | \examples{ 57 | x <- rnorm(10000) 58 | y <- rnorm(10000,x,x) 59 | hbin <- hexbin(x,y) 60 | hvp <- plot(hbin,type="n") 61 | pushHexport(hvp$plot,clip="on") 62 | hexGraphPaper(hbin,border=grey(.8)) 63 | grid.hexagons(hbin) 64 | } 65 | \keyword{aplot} 66 | \keyword{dplot} 67 | -------------------------------------------------------------------------------- /R/hbox.R: -------------------------------------------------------------------------------- 1 | hboxplot <- function(bin, xbnds = NULL, ybnds = NULL, 2 | density, border = c(0,grey(.7)), 3 | pen = c(2, 3), unzoom = 1.1, clip="off", reshape = FALSE, 4 | xlab = NULL, ylab = NULL, main = "") 5 | { 6 | 7 | ##_______________ Collect computing constants______________ 8 | 9 | if(!is(bin,"hexbin")) 10 | stop("first argument must be a hexbin object") 11 | h.xy <- hcell2xy(bin,check.erosion=TRUE) 12 | ##___zoom in scaling with expanding to avoid hexagons outside plot frame___ 13 | 14 | if(is(bin,"erodebin")) { 15 | h.xy$x <- h.xy$x 16 | h.xy$y <- h.xy$y 17 | nxbnds <- if(is.null(xbnds)) range(h.xy$x) else xbnds 18 | nybnds <- if(is.null(ybnds)) range(h.xy$y) else ybnds 19 | ratiox <- diff(nxbnds)/diff(bin@xbnds) 20 | ratioy <- diff(nybnds)/diff(bin@ybnds) 21 | 22 | ratio <- max(ratioy, ratiox) 23 | nxbnds <- mean(nxbnds) + c(-1,1)*(unzoom * ratio * diff(bin@xbnds))/2 24 | nybnds <- mean(nybnds) + c(-1,1)*(unzoom * ratio * diff(bin@ybnds))/2 25 | } 26 | else { 27 | nxbnds <- if(is.null(xbnds)) bin@xbnds else xbnds 28 | nybnds <- if(is.null(ybnds)) bin@ybnds else ybnds 29 | } 30 | margins <- unit(0.1 + c(5,4,4,3),"lines") 31 | plot.vp <- hexViewport(bin, xbnds = nxbnds, ybnds = nybnds, 32 | mar=margins, newpage = TRUE) 33 | pushHexport(plot.vp) 34 | grid.rect() 35 | grid.xaxis() 36 | grid.yaxis() 37 | ## xlab, ylab, main : 38 | if(is.null(xlab)) xlab <- bin@xlab 39 | if(is.null(ylab)) ylab <- bin@ylab 40 | if(nchar(xlab) > 0) 41 | grid.text(xlab, y = unit(-2, "lines"), gp= gpar(fontsize= 16)) 42 | if(nchar(ylab) > 0) 43 | grid.text(ylab, x = unit(-2, "lines"), gp= gpar(fontsize= 16), rot = 90) 44 | if(nchar(main) > 0) 45 | grid.text(main, y = unit(1, "npc") + unit(1.5, "lines"), 46 | gp = gpar(fontsize = 18)) 47 | if(clip=="on") { 48 | popViewport() 49 | pushHexport(plot.vp, clip="on") 50 | } 51 | 52 | cnt <- if(is(bin,"erodebin")) bin@count[bin@eroded] else bin@count 53 | 54 | xbins <- bin@xbins 55 | shape <- bin@shape 56 | xnew <- h.xy$x 57 | ynew <- h.xy$y 58 | 59 | ##__________________ Construct a hexagon___________________ 60 | dx <- (0.5 * diff(bin@xbnds))/xbins 61 | dy <- (0.5 * diff(bin@ybnds))/(xbins * shape * sqrt(3)) 62 | hexC <- hexcoords(dx, dy, sep = NULL) 63 | 64 | ##_______________ Full Cell Plotting_____________________ 65 | hexpolygon(xnew, ynew, hexC, density = density, 66 | fill = pen[2], border = border[2]) 67 | 68 | ##______________Plotting median___________________________ 69 | 70 | if(!is(bin,"erodebin")) { 71 | ## No warning here, allow non-erode above! warning("No erode component") 72 | } 73 | else { 74 | med <- which.max(bin@erode) 75 | xnew <- xnew[med] 76 | ynew <- ynew[med] 77 | hexpolygon(xnew, ynew, hexC, density = density, 78 | fill = pen[1], border = border[1]) 79 | } 80 | popViewport() 81 | invisible(plot.vp) 82 | 83 | }# hboxplot() 84 | -------------------------------------------------------------------------------- /R/LINGRAY.R: -------------------------------------------------------------------------------- 1 | LinGray <- function(n,beg = 1,end = 92) 2 | { 3 | if(beg < 1 || end < 1 || beg > 256 || end > 256) 4 | stop("`beg' and `end' must be numbers in the interval [1,256]") 5 | 6 | M <- rbind(c(0,0,0), 7 | c(0,0,0), 8 | c(1,1,1), 9 | c(1,1,1), 10 | c(2,2,2), 11 | c(3,3,3), 12 | c(4,4,4), 13 | c(5,5,5), 14 | c(6,6,6), 15 | c(7,7,7), 16 | c(8,8,8), 17 | c(9,9,9), 18 | c(10,10,10), 19 | c(11,11,11), 20 | c(12,12,12), 21 | c(13,13,13), 22 | c(14,14,14), 23 | c(15,15,15), 24 | c(16,16,16), 25 | c(17,17,17), 26 | c(18,18,18), 27 | c(19,19,19), 28 | c(20,20,20), 29 | c(21,21,21), 30 | c(22,22,22), 31 | c(23,23,23), 32 | c(24,24,24), 33 | c(25,25,25), 34 | c(26,26,26), 35 | c(27,27,27), 36 | c(28,28,28), 37 | c(29,29,29), 38 | c(30,30,30), 39 | c(32,32,32), 40 | c(34,34,34), 41 | c(35,35,35), 42 | c(37,37,37), 43 | c(39,39,39), 44 | c(41,41,41), 45 | c(43,43,43), 46 | c(45,45,45), 47 | c(46,46,46), 48 | c(47,47,47), 49 | c(49,49,49), 50 | c(51,51,51), 51 | c(52,52,52), 52 | c(54,54,54), 53 | c(56,56,56), 54 | c(59,59,59), 55 | c(61,61,61), 56 | c(64,64,64), 57 | c(67,67,67), 58 | c(69,69,69), 59 | c(72,72,72), 60 | c(75,75,75), 61 | c(76,76,76), 62 | c(78,78,78), 63 | c(81,81,81), 64 | c(84,84,84), 65 | c(87,87,87), 66 | c(91,91,91), 67 | c(94,94,94), 68 | c(97,97,97), 69 | c(101,101,101), 70 | c(104,104,104), 71 | c(107,107,107), 72 | c(108,108,108), 73 | c(112,112,112), 74 | c(116,116,116), 75 | c(120,120,120), 76 | c(124,124,124), 77 | c(128,128,128), 78 | c(132,132,132), 79 | c(136,136,136), 80 | c(141,141,141), 81 | c(145,145,145), 82 | c(147,147,147), 83 | c(150,150,150), 84 | c(154,154,154), 85 | c(159,159,159), 86 | c(164,164,164), 87 | c(169,169,169), 88 | c(174,174,174), 89 | c(179,179,179), 90 | c(185,185,185), 91 | c(190,190,190), 92 | c(195,195,195), 93 | c(201,201,201), 94 | c(207,207,207), 95 | c(212,212,212), 96 | c(216,216,216), 97 | c(218,218,218), 98 | c(224,224,224), 99 | c(226,226,226), 100 | c(230,230,230), 101 | c(237,237,237), 102 | c(243,243,243), 103 | c(245,245,245), 104 | c(252,252,252), 105 | c(255,255,255), 106 | c(255,255,255))[round(seq(beg,end,length = n)), ] 107 | 108 | rgb(M[,1]/255, 109 | M[,2]/255, 110 | M[,3]/255) 111 | } 112 | -------------------------------------------------------------------------------- /man/erode.hexbin.Rd: -------------------------------------------------------------------------------- 1 | \name{erode.hexbin} 2 | \alias{erode} 3 | \alias{erode.hexbin} 4 | \alias{erode,hexbin-method} 5 | \alias{erodebin-class} 6 | 7 | \title{Erosion of a Hexagon Count Image} 8 | \description{ 9 | This erosion algorithm removes counts from hexagon cells at a rate 10 | proportional to the cells' exposed surface area. When a cell becomes 11 | empty, algorithm removes the emptied cell and notes the removal 12 | order. Cell removal increases the exposure of any neighboring cells. 13 | The last cell removed is a type of bivariate median. 14 | } 15 | 16 | \usage{ 17 | erode(hbin, cdfcut = 0.5) 18 | erode.hexbin(hbin, cdfcut = 0.5) 19 | } 20 | 21 | \arguments{ 22 | \item{hbin}{an object of class \code{\link{hexbin}}.} 23 | \item{cdfcut}{number in (0,1) indicating the confidence level for the 24 | limits.} 25 | } 26 | 27 | \value{ 28 | An \code{"erodebin"} object (with all the slots from \code{hbin}) and 29 | additionally with 30 | high count cells and a component \code{erode} that gives the erosion order. 31 | } 32 | 33 | \details{ 34 | The algorithm extracts high count cells with containing a given 35 | fraction (cdfcut) of the total counts. The algorithm extracts all 36 | cells if cdfcut=0. The algorithm performs gray-level erosion on the 37 | extracted cells. Each erosion cycle removes counts from cells. The 38 | counts removed for each cell are a multiple of the cell's exposed-face 39 | count. The algorithm choses the multiple so at least one cell will be 40 | empty or have a count deficit on each erosion cycle. The erode vector 41 | contain an erosion number for each cell. The value of erode is 42 | 43 | 6*erosion_cycle_at_cell_removal - cell_deficit_at_removal 44 | 45 | Cells with low values are eroded first. The cell with the highest 46 | erosion number is a candidate bivariate median. A few ties in erode 47 | are common. 48 | } 49 | 50 | \seealso{ 51 | \code{\link{hexbin}}, \code{\link{smooth.hexbin}}, 52 | \code{\link{hcell2xy}}, %%FIXME\code{\link{hcell}}, 53 | %% \code{\link{hboxplot}}, \code{\link{hdiffplot}}, 54 | %% \code{\link{hmatplot}}, 55 | \code{\link{gplot.hexbin}}, 56 | \code{\link{grid.hexagons}}, \code{\link{grid.hexlegend}} 57 | } 58 | 59 | \examples{ 60 | set.seed(153) 61 | x <- rnorm(10000) 62 | y <- rnorm(10000) 63 | bin <- hexbin(x,y) 64 | 65 | smbin <- smooth.hexbin(bin) 66 | erodebin <- erode.hexbin(smbin, cdfcut=.5) 67 | plot(erodebin) 68 | 69 | ## bivariate boxplot 70 | hboxplot(erodebin, main = "hboxplot(erodebin)") 71 | 72 | 73 | %% MM: This looks wrong -- both the graphic and the logic in "par" here : 74 | # show erosion order 75 | plot(bin,style= "lat", minarea=1, maxarea=1, 76 | legend=FALSE, border=gray(.7)) 77 | 78 | %% FIXME: {compare with example in "hexbin0"} 79 | grid.hexagons(erodebin,style= "lat", minarea=1, maxarea=1,pen="green") 80 | xy <- hcell2xy(erodebin) 81 | library("grid") 82 | grid.text(lab = as.character(erodebin@erode), xy$x, xy$y, 83 | gp = gpar(col="white", cex=0.65)) 84 | 85 | } 86 | \keyword{hplot} 87 | -------------------------------------------------------------------------------- /man/hexpolygon.Rd: -------------------------------------------------------------------------------- 1 | \name{hexpolygon} 2 | \alias{hexpolygon} 3 | \alias{hexcoords} 4 | \title{Hexagon Coordinates and Polygon Drawing} 5 | \description{ 6 | Simple \sQuote{low-level} function for computing and drawing hexagons. 7 | Can be used for \sQuote{grid} (package \pkg{grid}) or 8 | \sQuote{traditional} (package \pkg{graphics}) graphics. 9 | } 10 | \usage{ 11 | hexcoords(dx, dy = NULL, n = 1, sep = NULL) 12 | 13 | hexpolygon(x, y, hexC = hexcoords(dx, dy, n = 1), dx, dy = NULL, 14 | fill = 1, border = 0, hUnit = "native", ...) 15 | } 16 | \arguments{ 17 | \item{dx,dy}{horizontal and vertical width of the hexagon(s).} 18 | \item{n}{number of hexagon \dQuote{repeats}.} 19 | \item{sep}{separator value to be put between coordinates of different 20 | hexagons. The default, \code{NULL} doesn't use a separator.} 21 | \item{x,y}{numeric vectors of the same length specifying the hexagon 22 | \emph{centers} around which to draw.} 23 | \item{hexC}{a list as returned from \code{hexcoords()}. 24 | Its component \code{no.sep} determines if grid or traditional 25 | graphics are used. The default (via default of \code{hexcoords}) is 26 | now to use grid graphics.} 27 | \item{fill,border}{passed to \code{\link[grid]{grid.polygon}} (for \pkg{grid}).} 28 | \item{hUnit}{string or \code{NULL} determining in which units (x,y) 29 | values are.} 30 | \item{\dots}{further arguments passed to \code{\link{polygon}} (for 31 | \pkg{graphics}).} 32 | } 33 | \value{ 34 | \code{hexcoords()} returns a list with components 35 | \item{x,y}{numeric vectors of length \eqn{n \times 6}{n * 6} (or 36 | \eqn{n \times 7}{n * 7} if \code{sep} is not NULL) 37 | specifying the hexagon polygon coordinates (with \code{sep} appended 38 | to each 6-tuple).} 39 | \item{no.sep}{a logical indicating if \code{sep} was \code{NULL}.} 40 | 41 | \code{hexpolygon} returns what its last \code{\link[grid]{grid.polygon}(.)} 42 | or \code{\link{polygon}(.)} call returns. 43 | } 44 | \author{Martin Maechler, originally.} 45 | \seealso{\code{\link{grid.hexagons}} which builds on these.} 46 | \examples{ 47 | str(hexcoords(1, sep = NA)) # multiple of (6 + 1) 48 | str(hexcoords(1, sep = NULL))# no separator -> multiple of 6 49 | \dontshow{ 50 | stopifnot(3 * (6+1) == sapply(hexcoords(2, n = 3, sep = NA)[1:2], length), 51 | 6 == sapply(hexcoords(1)[1:2], length)) 52 | set.seed(1001) 53 | } 54 | 55 | ## hexpolygon()s: 56 | x <- runif(20, -2, 2) 57 | y <- x + rnorm(20) 58 | 59 | ## 1) traditional 'graphics' 60 | plot(x,y, asp = 1, "plot() + hexpolygon()") 61 | hexpolygon(x,y, dx = 0.1, density = 25, col = 2, lwd = 1.5) 62 | 63 | ## 2) "grid" : 64 | 65 | addBit <- function(bnds, f = 0.05) bnds + c(-f, f) * diff(bnds) 66 | sc <- addBit(rxy <- range(x,y))# same extents (cheating asp=1) 67 | library("grid") 68 | grid.newpage() 69 | pushViewport(plotViewport(.1+c(4,4,2,1), xscale = sc, yscale = sc)) 70 | grid.rect() 71 | grid.xaxis() 72 | grid.yaxis() 73 | grid.points(x,y) 74 | hexpolygon(x,y, hexcoords(dx = 0.1, sep=NULL), border = "blue", fill=NA) 75 | popViewport() 76 | } 77 | \keyword{dplot} 78 | \keyword{aplot} 79 | -------------------------------------------------------------------------------- /man/hexplom.Rd: -------------------------------------------------------------------------------- 1 | \name{hexplom} 2 | \title{Hexbin Plot Matrices} 3 | \alias{hexplom} 4 | \alias{hexplom.formula} 5 | \alias{hexplom.data.frame} 6 | \alias{hexplom.matrix} 7 | \alias{panel.hexplom} 8 | 9 | \usage{ 10 | hexplom(x, data, \dots) 11 | 12 | \method{hexplom}{formula}(x, data = NULL, \dots) 13 | 14 | \method{hexplom}{data.frame}(x, data = NULL, \dots, groups = NULL, 15 | subset = TRUE) 16 | 17 | \method{hexplom}{matrix}(x, data = NULL, \dots, groups = NULL, subset = TRUE) 18 | 19 | panel.hexplom(\dots) 20 | } 21 | 22 | \description{ 23 | \code{hexplom} draws Conditional Hexbin Plot Matrices. It is similar 24 | to \code{splom}, expect that the default display is different. 25 | Specifically, the default display is created using 26 | \code{panel.hexplom}, which is an alias for \code{panel.hexbinplot}. 27 | } 28 | 29 | \arguments{ 30 | \item{x}{ 31 | The object on which method dispatch is carried out. 32 | 33 | For the \code{"formula"} method, a formula describing the structure 34 | of the plot, which should be of the form \code{~ x | g1 * g2 * 35 | \dots}, where \code{x} is a data frame or matrix. Each of \code{g1, 36 | g2, \dots} must be either factors or shingles. The conditioning 37 | variables \code{g1, g2, \dots} may be omitted. 38 | 39 | For the \code{data.frame} and \code{matrix} methods, a data frame or 40 | matrix as appropriate. 41 | } 42 | \item{data}{ 43 | For the \code{formula} method, an optional data frame in which 44 | variables in the formula (as well as \code{groups} and 45 | \code{subset}, if any) are to be evaluated. By default, the 46 | environment where the function was called from is used. 47 | } 48 | 49 | \item{groups, subset, \dots}{ see \code{\link[lattice]{splom}}. The 50 | non-standard evaluation of \code{groups} and \code{subset} only 51 | applies in the \code{formula} method. Apart from arguments that 52 | apply to \code{splom} (many of which are only documented in 53 | \code{\link[lattice]{xyplot}}), additional arguments meant for 54 | \code{panel.hexplom} (which is an alias for 55 | \code{\link{panel.hexbinplot}}) may also be supplied. Such 56 | arguments may include ones that control details of the hexbin 57 | calculations, documented in \code{\link{gplot.hexbin}}} 58 | 59 | } 60 | \value{ 61 | 62 | An object of class \code{"trellis"}. The 63 | \code{\link[lattice:update.trellis]{update}} method can be used to 64 | update components of the object and the 65 | \code{\link[lattice:print.trellis]{print}} method (usually called by 66 | default) will plot it on an appropriate plotting device. 67 | 68 | } 69 | 70 | \seealso{ 71 | \code{\link[lattice]{splom}}, \code{\link[lattice]{xyplot}}, 72 | \code{\link[hexbin]{hexbinplot}}, 73 | \code{\link[lattice]{Lattice}}, \code{\link[lattice]{panel.pairs}} 74 | } 75 | 76 | \author{ Deepayan Sarkar \email{Deepayan.Sarkar@R-project.org}, 77 | Nicholas Lewin-Koh \email{nikko@hailmail.net}} 78 | 79 | \examples{ 80 | 81 | ## Simple hexplom 82 | data(NHANES) 83 | hexplom(~NHANES[,7:14], xbins=15) 84 | 85 | ## With colors and conditioning 86 | hexplom(~NHANES[,9:13] | Sex, data = NHANES, 87 | xbins = 15, colramp = magent) 88 | 89 | ## With custom panel function 90 | hexplom(NHANES[,9:13], xbins = 20,colramp = BTY, 91 | upper.panel = panel.hexboxplot) 92 | 93 | 94 | } 95 | \keyword{hplot} 96 | 97 | -------------------------------------------------------------------------------- /man/hexVP-class.Rd: -------------------------------------------------------------------------------- 1 | \name{hexVP-class} 2 | \docType{class} 3 | \alias{hexVP-class} 4 | \alias{getFig,hexVP-method} 5 | \alias{getMargins,hexVP-method} 6 | \alias{getPlt,hexVP-method} 7 | \alias{getXscale,hexVP-method} 8 | \alias{getYscale,hexVP-method} 9 | 10 | \title{Formal class "hexVP" of a Hexagon Viewport} 11 | \description{ 12 | Hexagon Viewports are \dQuote{value-added} grid viewports (see 13 | \code{\link[grid]{viewport}}) where the extra slots contain scaling and 14 | \dQuote{embedding} information. A hexViewport is created my taking the 15 | available area in the cuurent viewport on the graphics device and 16 | maximizing the amount of area with a fied aspect ratio. The default 17 | when the shape parameter is 1, is a 1:1 aspect ratio in terms of the 18 | size of the viewport, not the scale of the x and y axis. The plotting 19 | area is centered within the existing margins and the maximum size 20 | determined. Extra area is then allocated to the margins. This viewport 21 | is replicated twice, once with clipping set to "on" and once with 22 | clipping "off". This feature can be used for toggling clipping on and 23 | off while editing the plot. 24 | } 25 | \section{Objects from the Class}{ 26 | Objects are typically created by calls to \code{\link{hexViewport}()} 27 | or by low level calls of the form \code{new("hexVP", ...)}. 28 | } 29 | \section{Slots}{ 30 | \describe{ 31 | \item{\code{hexVp.off}:}{Object of class \code{"viewport"} with 32 | clipping set to off, see \code{\link[grid]{viewport}}.} 33 | \item{\code{hexVp.on}:}{Object of class \code{"viewport"}, with the same 34 | dimensions and parameters as hexVp.off, but with 35 | clipping set to on, see \code{\link[grid]{viewport}}.} 36 | \item{\code{hp.name}:}{The name of the viewport for searching a vptree}. 37 | \item{\code{mar}:}{\code{\link[grid]{unit}} vector of four margins 38 | (typically in \code{"lines"}).} 39 | \item{\code{fig}:}{\code{\link[grid]{unit}} vector of two figure sizes 40 | (typically in \code{"npc"}).} 41 | \item{\code{plt}:}{\code{\link[grid]{unit}} vector of two figure sizes 42 | (typically in \code{"npc"}).} 43 | %% MM {FIXME?}: Is n't this simply ``xlim'' - then call it so! 44 | %% NL, yes it is, but xscale and yscale is the parameters used by 45 | %% grid. 46 | \item{\code{shape}:}{The shape parameter from the plotted 47 | \code{\link[hexbin]{hexbin}} object.} 48 | \item{\code{xscale}:}{numeric of length two specifying x-range.} 49 | \item{\code{yscale}:}{numeric of length two specifying y-range.} 50 | } 51 | } 52 | \section{Methods}{ 53 | These are methods accessing the slots of corresponding name. 54 | \describe{ 55 | \item{getFig}{\code{signature(hvp = "hexVP")}: ... } 56 | \item{getMargins}{\code{signature(hvp = "hexVP")}: ... } 57 | \item{getPlt}{\code{signature(hvp = "hexVP")}: ... } 58 | \item{getXscale}{\code{signature(hvp = "hexVP")}: ... } 59 | \item{getYscale}{\code{signature(hvp = "hexVP")}: ... } 60 | } 61 | } 62 | \author{ 63 | Nicholas Lewin-Koh \email{kohnicho@comp.nus.edu.sg}. 64 | } 65 | \seealso{ 66 | The constructor function \code{\link{hexViewport}}. 67 | \code{\link{hexbin}}, and its S4 plotting method, 68 | \code{\link{gplot.hexbin}}. 69 | } 70 | \examples{ 71 | library("grid") 72 | example(hexViewport, echo=FALSE) 73 | ## continued: 74 | str(P$plot.vp) 75 | } 76 | \keyword{classes} 77 | -------------------------------------------------------------------------------- /man/grid.hexlegend.Rd: -------------------------------------------------------------------------------- 1 | \name{grid.hexlegend} 2 | \alias{grid.hexlegend} 3 | \title{Add a Legend to a Hexbin Plot} 4 | \description{ 5 | Plots the legend for the \code{plot} method of \code{\link{hexbin}}. 6 | Provides a legend indicating the count representations. 7 | } 8 | \usage{ 9 | grid.hexlegend(legend, ysize, lcex, inner, style = , 10 | minarea = 0.05, maxarea = 0.8, mincnt = 1, maxcnt, trans = NULL, 11 | inv = NULL, colorcut, density = NULL, border = NULL, pen = NULL, 12 | colramp = function(n) { LinGray(n,beg = 90,end = 15) }, 13 | leg.unit = "native") 14 | } 15 | \arguments{ 16 | \item{legend}{positive number giving width of the legend in inches.} 17 | \item{ysize}{height of legend in inches} 18 | \item{lcex}{the characters expansion size for the text in the legend, 19 | see \code{\link{par}(cex=)}.} 20 | \item{inner}{the inner diameter of a hexagon in inches.} 21 | \item{style}{the hexagon style; see \code{\link{grid.hexagons}}.} 22 | \item{minarea, maxarea}{fraction of the cell area for the lowest and largest 23 | count, respectively.} 24 | \item{mincnt, maxcnt}{minimum and maximum count accepted in \code{plot}.} 25 | \item{trans}{a transformation function for the counts such as 26 | \code{\link{sqrt}}.} 27 | \item{inv}{the inverse transformation function.} 28 | \item{colorcut}{numeric vector of values covering [0, 1] the determine 29 | hexagon color classes boundaries and hexagon legend size boundaries.} 30 | \item{border}{argument for \code{\link{polygon}()}. Draw the border 31 | for each hexagon.} 32 | \item{density}{argument for \code{\link{polygon}()} filling. A 33 | \code{0} causes the polygon not to be filled.} 34 | \item{pen}{color argument used for \code{\link{polygon}(col = .)}. 35 | Determines the color with which the polygon will be filled.} 36 | \item{colramp}{function accepting an integer \code{n} as an argument and 37 | returning n colors.} 38 | \item{leg.unit}{unit to use}%FIXME 39 | 40 | } 41 | \details{ 42 | The \code{plot} method for \code{\link{hexbin}} objects calls this function 43 | to produce a legend 44 | by setting the graphics parameters, so \code{hex.legend} itself is not a 45 | standalone function. 46 | 47 | The legend function is \bold{preliminary}. Later version will include 48 | refinements and handle extreme cases (small and large) for cell size 49 | and counts. 50 | 51 | See the \bold{Details} section of \code{\link{grid.hexagons}}'s help page. 52 | } 53 | \value{ 54 | This function does not return any value. 55 | } 56 | \references{ see in \code{\link{grid.hexagons}}.} 57 | \author{ 58 | Dan Carr 59 | 60 | ported by Nicholas Lewin-Koh 61 | } 62 | 63 | \seealso{\code{\link{hexbin}}, \code{\link{grid.hexagons}}, 64 | % FIXME 65 | \code{\link{smooth.hexbin}}, \code{\link{erode.hexbin}}, 66 | % \code{\link{hcell}}, 67 | \code{\link{hcell2xy}}, 68 | \code{\link{gplot.hexbin}},% \code{\link{hboxplot}},% \code{\link{hdiffplot}}, 69 | % \code{\link{hmatplot}} 70 | } 71 | 72 | \examples{ 73 | ## Not a stand alone function; typically only called from plot.hexbin() 74 | %% Hence we should not run it here! 75 | %% FIXME: Improve hex.legend() such that it *can* be added to plots !!! 76 | \dontrun{ 77 | grid.hexlegend(legend = 2, ysize = 1,lcex=8,inner=0.2, 78 | maxcnt = 100, colorcut = c(0.5,0.5)) 79 | } 80 | } 81 | \keyword{aplot} 82 | -------------------------------------------------------------------------------- /man/smooth.hexbin.Rd: -------------------------------------------------------------------------------- 1 | \name{smooth.hexbin} 2 | \alias{smooth.hexbin} 3 | \alias{smoothbin-class} 4 | \title{Hexagon Bin Smoothing} 5 | \description{ 6 | Given a \code{"hexbin"} (hexagon bin) object, compute a discrete 7 | kernel smoother that covers seven cells, namely a center cell and its 8 | six neighbors. With two iterations the kernel effectively covers 9 | 1+6+12=19 cells. 10 | } 11 | \usage{ 12 | smooth.hexbin(bin, wts=c(48,4,1)) 13 | } 14 | \arguments{ 15 | \item{bin}{object of class \code{"hexbin"}, typically resulting from 16 | \code{\link{hexbin}()} or \code{\link{erode,hexbin-method}}.} 17 | \item{wts}{numeric vector of length 3 for relative weights of the 18 | center, the six neighbor cells, and twelve second neighbors.} 19 | } 20 | \value{ 21 | an object of class \code{"smoothbin"}, extending class 22 | \code{"hexbin"}, see \code{\link{hexbin}}. 23 | The object includes the additional slot \code{wts}. 24 | } 25 | \references{see \code{\link{grid.hexagons}} and \code{\link{hexbin}}.} 26 | 27 | \details{ 28 | This discrete kernel smoother uses the center cell, immediate 29 | neighbors and second neighbors to smooth the counts. The counts for 30 | each resulting cell is a linear combination of previous cell counts 31 | and weights. The weights are 32 | \tabular{ll}{ 33 | 1 center cell, \tab weight = wts[1]\cr 34 | 6 immediate neighbors\tab weight = wts[2]\cr 35 | 12 second neighbors \tab weight =wts[3]\cr 36 | } 37 | If a cell, its immediate and second neighbors all have a value of 38 | \code{max(cnt)}, the new maximum count would be 39 | \code{max(cnt)*sum(wts)}. It is possible for the counts to overflow. 40 | 41 | The domain for cells with positive counts increases. The hexbin 42 | slots \code{xbins}, \code{xbnds}, \code{ybnds}, and \code{dimen} all 43 | reflect this increase. 44 | Note that usually \code{dimen[2] = xbins+1}. 45 | 46 | The intent was to provide a fast, iterated, immediate neighbor smoother. 47 | However, the current hexbin plotting routines only support shifting 48 | even numbered rows to the right. Future work can 49 | 50 | (1) add a shift indicator to hexbin objects that indicates left or 51 | right shifting.\cr 52 | (2) generalize plot.hexbin() and hexagons()\cr 53 | (3) provide an iterated kernel.\cr 54 | 55 | With \code{wts[3]=0}, the smoother only uses the immediate neighbors. 56 | With a shift indicator the domain could increase by 2 rows (one bottom 57 | and on top) and 2 columns (one left and one right). However the current 58 | implementation increases the domain by 4 rows and 4 columns, thus 59 | reducing plotting resolution. 60 | } 61 | 62 | \seealso{ 63 | \code{\link{hexbin}}, \code{\link{erode.hexbin}}, %MISSING \code{\link{hthin}}, 64 | \code{\link{hcell2xy}},% \code{\link{hcell}}, 65 | \code{\link{gplot.hexbin}}, \code{\link{hboxplot}}, 66 | %\code{\link{hdiffplot}}, \code{\link{hmatplot}}, 67 | \code{\link{grid.hexagons}}, \code{\link{grid.hexlegend}}. 68 | } 69 | \examples{ 70 | x <- rnorm(10000) 71 | y <- rnorm(10000) 72 | bin <- hexbin(x,y) 73 | # show the smooth counts in gray level 74 | smbin <- smooth.hexbin(bin) 75 | plot(smbin, main = "smooth.hexbin(.)") 76 | 77 | # Compare the smooth and the origin 78 | smbin1 <- smbin 79 | smbin1@count <- as.integer(ceiling(smbin@count/sum(smbin@wts))) 80 | plot(smbin1) 81 | smbin2 <- smooth.hexbin(bin,wts=c(1,0,0)) # expand the domain for comparability 82 | plot(smbin2) 83 | } 84 | \keyword{misc} 85 | -------------------------------------------------------------------------------- /R/hexbinList.R: -------------------------------------------------------------------------------- 1 | hexList <- function(x,y=NULL,given=NULL,xbins=30,shape=1, 2 | xbnds = NULL, ybnds = NULL, 3 | xlab = NULL, ylab = NULL) 4 | { 5 | xl <- if (!missing(x)) deparse(substitute(x)) 6 | yl <- if (!missing(y)) deparse(substitute(y)) 7 | xy <- xy.coords(x, y, xl, yl) 8 | if(length(given)!=length(xy$x) | is.null(given)) 9 | stop("Given is is different length from x and y") 10 | if(is.factor(given)) 11 | given <- as.character(given) 12 | clss <- unique(given) 13 | if(is.null(xbnds)) 14 | xbnds <- range(xy$x) 15 | if(is.null(ybnds)) 16 | ybnds <- range(xy$y) 17 | hbins <- vector(mode = "list",length=length(clss)) 18 | i <- 1 19 | for(g in clss){ 20 | hbins[[i]] <- hexbin(xy$x[given==g],xy$y[given==g], 21 | xbins=xbins,shape=shape,xbnds=xbnds,ybnds=ybnds) 22 | i <- i+1 23 | } 24 | mx <- max(unlist(lapply(hbins,function(h)max(h@count)))) 25 | mn <- min(unlist(lapply(hbins,function(h)min(h@count)))) 26 | hl <- new("hexbinList",n=length(hbins),hbins=hbins, Xbnds=xbnds, 27 | Ybnds=ybnds, Xbins=integer(xbins), Shape=shape, Bnames=clss, 28 | CntBnds=c(mn,mx)) 29 | hl 30 | } 31 | 32 | 33 | setClass("hexbinList", 34 | representation(n="integer", hbins="vector", 35 | Xbnds="numeric", Ybnds="numeric", 36 | Xbins="numeric", Shape="numeric", 37 | Bnames="character", CntBnds="numeric") 38 | 39 | ) 40 | 41 | 42 | bnds.check <- function(binlst, xb = TRUE, yb = TRUE) 43 | { 44 | xb <- 45 | if(xb) { 46 | b <- binlst[[1]]@xbnds 47 | all(unlist(lapply(binlst, function(x, bnd) all(x@xbnds == bnd), b))) 48 | } else TRUE 49 | yb <- 50 | if(yb) { 51 | b <- binlst[[1]]@ybnds 52 | all(unlist(lapply(binlst, function(y, bnd) all(y@ybnds == bnd), b))) 53 | } else TRUE 54 | xb & yb 55 | } 56 | 57 | xbins.check <- function(binlst) 58 | { 59 | xb <- binlst[[1]]@xbins 60 | all(unlist(lapply(binlst, function(y, xbin)all(y@xbins == xbin), xb))) 61 | } 62 | 63 | shape.check <- function(binlst) 64 | { 65 | xs <- binlst[[1]]@shape 66 | all(unlist(lapply(binlst, function(y, xsh)all(y@shape == xsh), xs))) 67 | } 68 | 69 | list2hexList <- function(binlst) 70 | { 71 | if(length(binlst) < 2) 72 | stop(" need at least 2 hex bin objects") 73 | if(!all(unlist(lapply(binlst, is, "hexbin")))) 74 | stop("All Elements of list must be hexbin objects") 75 | if(!bnds.check(binlst)) 76 | stop("All bin objects in list need the same xbnds and ybnds") 77 | if(!xbins.check(binlst)) 78 | stop("All bin objects in list need the same number of bins") 79 | if(!shape.check(binlst)) 80 | stop("All bin objects in list need the same shape parameter") 81 | mx <- max(unlist(lapply(binlst,function(h)max(h@count)))) 82 | mn <- min(unlist(lapply(binlst,function(h)min(h@count)))) 83 | xbins <- binlst[[1]]@xbins 84 | xbnds <- binlst[[1]]@xbnds 85 | ybnds <- binlst[[1]]@ybnds 86 | shape <- binlst[[1]]@shape 87 | hl <- new("hexbinList",n=length(binlst),hbins=binlst, Xbnds=xbnds, 88 | Ybnds=ybnds, Xbins=xbins, Shape=shape, 89 | Bnames=names(binlst), CntBnds=c(mn,mx)) 90 | hl 91 | } 92 | 93 | setAs("list","hexbinList",function(from)list2hexList(from)) 94 | 95 | #setMethod("[", "hexbinList", function(hbl,i,...) 96 | #{ 97 | # if( length(list(...)) > 0 ) 98 | # stop("extra subscripts cannot be handled") 99 | # if(missing(i)) hbl 100 | # hbl@hbins[i] 101 | #}) 102 | 103 | ##setMethod("[[", "hexbinList", function(hbl) 104 | ##{ 105 | 106 | ##}) 107 | -------------------------------------------------------------------------------- /man/hboxplot.Rd: -------------------------------------------------------------------------------- 1 | \name{hboxplot} 2 | \alias{hboxplot} 3 | \title{2-D Generalization of Boxplot} 4 | 5 | \description{ 6 | If \code{bin} is an \emph{eroded} \code{\link{hexbin}} object, i.e., 7 | an \code{erodebin} object, \code{hboxplot()} plots the high counts cells 8 | selected by \code{\link{erode}()}. By default, the high counts 9 | cells contain 50 percent of the counts so analagous to the 10 | interquartile \dQuote{range}. The function distinguishes the last 11 | cells eroded using color. These cells correspond to one definition of the 12 | bivariate median. 13 | %% FIXME ^^ (bad style, content +- ok) 14 | } 15 | 16 | \usage{ 17 | hboxplot(bin, xbnds = NULL, ybnds = NULL, 18 | density, border = c(0, grey(0.7)), pen = c(2, 3), 19 | unzoom = 1.1, clip ="off", reshape = FALSE, 20 | xlab = NULL, ylab = NULL, main = "") 21 | } 22 | \arguments{ 23 | \item{bin}{an object of class \code{\link{hexbin}}.} 24 | \item{xbnds,ybnds}{global x- and y-axis plotting limits for multiple 25 | plots.} 26 | \item{density, border}{arguments for \code{\link{polygon}()} each of 27 | length two, the first for the median, the second for the other cells.} 28 | \item{pen}{colors (\dQuote{pen numbers}) for \code{polygon()}.} 29 | \item{unzoom}{plot limit expansion factor when \code{xbnds} is 30 | missing.} 31 | \item{clip}{either 'on' or 'off' are the allowed arguments, when on 32 | everything is clipped to the plotting region.} 33 | \item{reshape}{logical value to reshape the plot although \code{xbnds} 34 | and \code{ybnds} are present.} 35 | \item{xlab, ylab, main}{x- and y- axis labels and main title} 36 | } 37 | 38 | \value{ 39 | invisibly, the \code{\link{hexViewport}()} used internally. 40 | Used to add to the plot afterwards. 41 | } 42 | 43 | \references{ see in \code{\link{grid.hexagons}}.} 44 | 45 | \details{ 46 | The \code{density}, \code{border}, and \code{pen} arguments correspond 47 | to the \code{\link{polygon}} function calls for plotting two types of 48 | cells. The cell types, pen numbers and suggested colors are\cr 49 | \tabular{lll}{ 50 | TYPE \tab PEN \tab COLOR \cr 51 | cells of bin \tab 2 \tab light gray \cr 52 | last eroded cells of bin (median cells)\tab 1 \tab black \cr 53 | } 54 | 55 | The erode components of the hexbin objects must be present for the 56 | medians cells to plot. 57 | 58 | When \code{xbnds} is missing or \code{reshape} is true, the plot 59 | changes graphics parameters and resets them. When \code{xbnds} is 60 | missing the function also zooms in based on the available data to 61 | provide increased resolution. 62 | 63 | The zoom used the hexagon cell centers. The unzoom argument backs off 64 | a bit so the whole hexagon will fit in the plot. 65 | 66 | \code{Hboxplot()} is used as a stand alone function, for producing separate 67 | legends .....%%FIXME for \code{\link{hmatplot}()} and for panels in 68 | %% \code{\link{hmatplot}()}. 69 | } 70 | 71 | \seealso{ 72 | \code{\link{hexbin}}, \code{\link{erode}}, 73 | %\code{\link{smooth.hexbin}}, 74 | \code{\link{hcell2xy}},% \code{\link{hcell}}, 75 | \code{\link{gplot.hexbin}}, 76 | % \code{\link{hmatplot}}, 77 | \code{\link{grid.hexagons}}, \code{\link{grid.hexlegend}} 78 | } 79 | 80 | \examples{ 81 | \dontshow{set.seed(753)} 82 | ## boxplot of smoothed counts 83 | x <- rnorm(10000) 84 | y <- rnorm(10000) 85 | 86 | bin <- hexbin(x,y) 87 | erodebin <- erode(smooth.hexbin(bin)) 88 | 89 | hboxplot(erodebin) 90 | hboxplot(erodebin, density = c(32,7), border = c(2,4)) 91 | hp <- hboxplot(erodebin, density = c(-1,17), 92 | main = "hboxplot(erode*(smooth*(.)))") 93 | pushHexport(hp) 94 | library("grid") 95 | grid.points(x[1:10], y[1:10])# just non-sense to show the principle 96 | popViewport() 97 | } 98 | \keyword{hplot} 99 | -------------------------------------------------------------------------------- /R/hexutil.R: -------------------------------------------------------------------------------- 1 | hcell2xyInt <- function(hbin, xbins=NULL, xbnds=NULL, ybnds=NULL, shape=NULL) 2 | { 3 | if(missing(hbin) && (is.null(xbnds) || is.null(ybnds))) 4 | stop("Need a hexbin object or boundaries to make lattice") 5 | if(missing(hbin) && (is.null(xbins) || is.null(shape))) 6 | stop("Need xbins and shape to make a lattice") 7 | if(!missing(hbin)) { 8 | xbins <- hbin@xbins 9 | shape <- hbin@shape 10 | xbnds <- if(is.null(xbnds)) hbin@xbnds else xbnds 11 | ybnds <- if(is.null(ybnds)) hbin@ybnds else ybnds 12 | dimen <- hbin@dimen 13 | 14 | } 15 | if(missing(hbin)) { 16 | jmax <- floor(xbins + 1.5001) 17 | imax <- 2 * floor((xbins *shape)/sqrt(3) + 1.5001) 18 | dimen <- c(imax, jmax) 19 | } 20 | cell <- 1:(dimen[1]*dimen[2])-1 21 | i <- cell %/% dimen[2] 22 | j <- cell %% dimen[2] 23 | list(i=i+1, j=j+1) 24 | } 25 | 26 | hgridcent <- function(xbins, xbnds, ybnds, shape, edge.add=0) 27 | { 28 | ## auxiliary for hexGraphPaper(): 29 | jmax <- floor(xbins + 1.5001) 30 | c1 <- 2 * floor((xbins *shape)/sqrt(3) + 1.5001) 31 | imax <- (jmax*c1 -1)/jmax + 1 32 | dimen <- c(imax, jmax) 33 | c3 <- diff(xbnds)/xbins 34 | c4 <- (diff(ybnds) * sqrt(3))/(2 * shape * xbins) 35 | if(edge.add > 0) { 36 | xbnds <- xbnds + 1.5*c(-edge.add*c3, edge.add*c3) 37 | ybnds <- ybnds + c(-edge.add*c4, edge.add*c4) 38 | dimen <- dimen + rep.int(2*edge.add, 2) 39 | } 40 | jmax <- dimen[2] 41 | cell <- 1:(dimen[1]*dimen[2]) 42 | i <- cell %/% jmax 43 | j <- cell %% jmax 44 | y <- c4 * i + ybnds[1] 45 | x <- c3 * ifelse(i %% 2 == 0, j, j + 0.5) + xbnds[1] 46 | list(x = x, y = y, dimen = dimen, dx=c3, dy=c4) 47 | } 48 | 49 | hexGraphPaper <- 50 | function(hb, xbnds=NULL, ybnds=NULL, xbins=30, shape=1, 51 | add=TRUE, fill.edges=1, fill=0, border=1) 52 | { 53 | if(missing(hb) && (is.null(xbnds) || is.null(ybnds))) 54 | stop("Need a hexbin object or boundaries to make lattice") 55 | if(!missing(hb)) { 56 | xbins <- hb@xbins 57 | shape <- hb@shape 58 | xbnds <- if(is.null(xbnds)) hb@xbnds else xbnds 59 | ybnds <- if(is.null(ybnds)) hb@ybnds else ybnds 60 | dimen <- hb@dimen 61 | } 62 | xy <- hgridcent(xbins, xbnds, ybnds, shape, edge.add=fill.edges) 63 | if(add){ 64 | sx <- xbins/diff(xbnds) 65 | sy <- (xbins * shape)/diff(ybnds) 66 | inner <- 0.5 67 | outer <- (2 * inner)/sqrt(3) 68 | dx <- inner/sx 69 | dy <- outer/(2 * sy) 70 | if(add){ 71 | hexC <- hexcoords(dx, dy, sep=NULL) 72 | hexpolygon (xy$x, xy$y, hexC, dx, dy, 73 | fill = fill, border = border, hUnit = "native") 74 | } 75 | } 76 | invisible(xy) 77 | } 78 | 79 | hexTapply <- function(hbin,dat,FUN=sum,...,simplify=TRUE) 80 | { 81 | if(is.null(hbin@cID)) 82 | stop("Must have cell ID's to do this operation \n 83 | please re-bin data using IDs = TRUE") 84 | if((length(dat)> 0) && (length(dat) != length(hbin@cID))) 85 | stop("Length of IDs does not match the length of the data") 86 | tapply(dat,hbin@cID,FUN,...,simplify=simplify) 87 | } 88 | 89 | optShape <- function(vp, height=NULL, width=NULL, mar=NULL) 90 | { 91 | if(missing(vp) && (is.null(height) || is.null(width))) 92 | stop("Need a viewport object or height and width of the plotting region.") 93 | if(!missing(vp)) { 94 | if("hexVP" %in% class(vp)) { 95 | height <- vp@plt[2] 96 | width <- vp@plt[1] 97 | } 98 | else if("viewport"%in%class(vp)) { 99 | #height <- convertHeight(unit(1,"npc"),"inches") 100 | #width <- convertWidth (unit(1,"npc"),"inches") 101 | height <- convertUnit(vp$height,"inches") 102 | width <- convertUnit(vp$width,"inches") 103 | } 104 | else 105 | stop("need valid viewport or hexViewport") 106 | } 107 | if(!is.null(mar)){ 108 | height <- height - mar[1] - mar[3] 109 | width <- width - mar[2] - mar[4] 110 | } 111 | 112 | shape <- as.numeric(height)/as.numeric(width) 113 | shape 114 | } 115 | 116 | inout.hex <- function(hbin,mincnt) 117 | { 118 | if(is.null(hbin@cID)) 119 | stop("bin object must have a cID slot, \n try re-binning with ID = TRUE") 120 | tI <- table(hbin@cID) 121 | which(hbin@cID%in%(names(tI)[tI> ./hexagons.Rd 92 | 93 | \seealso{ 94 | \code{\link{hexbin}}, \code{\link{smooth.hexbin}}, \code{\link{erode.hexbin}}, 95 | % MISSING: hthin, 96 | \code{\link{hcell2xy}}, % \code{\link{hcell}}, 97 | \code{\link{gplot.hexbin}}, 98 | \code{\link{hboxplot}}, % \code{\link{hmatplot}}, 99 | \code{\link{grid.hexagons}}, \code{\link{grid.hexlegend}}. 100 | } 101 | \examples{ 102 | ## Comparison of two bivariate boxplots 103 | x1 <- rnorm(10000) 104 | y1 <- rnorm(10000) 105 | x2 <- rnorm(10000,mean=.5) 106 | y2 <- rnorm(10000,mean=.5) 107 | xbnds <- range(x1,x2) 108 | ybnds <- range(y1,y2) 109 | 110 | bin1 <- hexbin(x1,y1,xbnds=xbnds,ybnds=ybnds) 111 | bin2 <- hexbin(x2,y2,xbnds=xbnds,ybnds=ybnds) 112 | erodebin1 <- erode.hexbin(smooth.hexbin(bin1)) 113 | erodebin2 <- erode.hexbin(smooth.hexbin(bin2)) 114 | 115 | hdiffplot(erodebin1,erodebin2) 116 | 117 | ## Compare *three* of them: -------------------- 118 | 119 | x3 <- rnorm(10000,mean=-1) 120 | y3 <- rnorm(10000,mean=-.5) 121 | xbnds <- range(x1,x2,x3) 122 | ybnds <- range(y1,y2,y3) 123 | 124 | bin1 <- hexbin(x1,y1,xbnds=xbnds,ybnds=ybnds) 125 | bin2 <- hexbin(x2,y2,xbnds=xbnds,ybnds=ybnds) 126 | bin3 <- hexbin(x3,y3,xbnds=xbnds,ybnds=ybnds) 127 | erodebin1 <- erode.hexbin(smooth.hexbin(bin1)) 128 | erodebin2 <- erode.hexbin(smooth.hexbin(bin2)) 129 | erodebin3 <- erode.hexbin(smooth.hexbin(bin3)) 130 | 131 | bnlst <- list(b1=erodebin1, b2=erodebin2, b3=erodebin3) 132 | hdiffplot(bnlst) 133 | } 134 | \keyword{hplot} 135 | -------------------------------------------------------------------------------- /man/gplot.hexbin.Rd: -------------------------------------------------------------------------------- 1 | \name{gplot.hexbin} 2 | \alias{gplot.hexbin} 3 | \alias{plot,hexbin,missing-method} 4 | \title{Plotting Hexagon Cells with a Legend} 5 | \description{ 6 | Plots Hexagons visualizing the counts in an hexbin object. Different 7 | styles are availables. Provides a legend indicating the count 8 | representations. 9 | } 10 | \usage{ 11 | %% In future: No longer export gplot.hexbin() ! 12 | gplot.hexbin(x, style = "colorscale", legend = 1.2, lcex = 1, 13 | minarea = 0.04, maxarea = 0.8, mincnt = 1, maxcnt = max(x@count), 14 | trans = NULL, inv = NULL, colorcut = seq(0, 1, length = min(17, maxcnt)), 15 | border = NULL, density = NULL, pen = NULL, 16 | colramp = function(n) LinGray(n,beg = 90,end = 15), 17 | xlab = "", ylab = "", main = "", newpage = TRUE, 18 | type = c("p", "l", "n"), xaxt = c("s", "n"), yaxt = c("s", "n"), 19 | clip = "on", verbose = getOption("verbose")) 20 | %% FIXME: This is the S4 plot method for 'hexbin' 21 | %% currently also exported "standalone" - for testing,debugging.. 22 | %% we'd really don't want to repeat the argument list; use \synopsis{.} ? 23 | \S4method{plot}{hexbin,missing}(x, style = "colorscale", legend = 1.2, lcex = 1, 24 | minarea = 0.04, maxarea = 0.8, mincnt = 1, maxcnt = max(x@count), 25 | trans = NULL, inv = NULL, colorcut = seq(0, 1, length = min(17, maxcnt)), 26 | border = NULL, density = NULL, pen = NULL, 27 | colramp = function(n) LinGray(n,beg = 90,end = 15), 28 | xlab = "", ylab = "", main = "", newpage = TRUE, 29 | type = c("p", "l", "n"), xaxt = c("s", "n"), yaxt = c("s", "n"), 30 | clip = "on", verbose = getOption("verbose")) 31 | } 32 | \arguments{ 33 | \item{x}{an object of class \code{\link{hexbin}}.} 34 | % \item{y}{(required by the S4 method for \code{\link{plot}} but unused 35 | % here; must be missing)} 36 | \item{style}{string specifying the style of hexagon plot, 37 | see \code{\link{grid.hexagons}} for the possibilities.} 38 | \item{legend}{numeric width of the legend in inches of \code{FALSE}. 39 | In the latter case, or when \code{0}, no legend is not produced.} 40 | \item{lcex}{characters expansion size for the text in the legend} 41 | \item{minarea}{fraction of cell area for the lowest count} 42 | \item{maxarea}{fraction of the cell area for the largest count} 43 | \item{mincnt}{cells with fewer counts are ignored.} 44 | \item{maxcnt}{cells with more counts are ignored.} 45 | \item{trans}{\code{\link{function}} specifying a transformation for 46 | the counts such as \code{sqrt}.} 47 | % FIXME: use better description of these in hexagons() -- or use same 48 | % ---- help page ?! 49 | \item{inv}{the inverse transformation of \code{trans}.} 50 | \item{colorcut}{vector of values covering [0, 1] that determine 51 | hexagon color class boundaries and hexagon legend size boundaries. 52 | Alternatively, an integer (\code{<= maxcnt}) specifying the 53 | \emph{number} of equispaced colorcut values in [0,1].} 54 | \item{border, density, pen}{color for polygon borders and filling of 55 | each hexagon drawn, passed to \code{\link{grid.hexagons}}.} 56 | \item{colramp}{function accepting an integer \code{n} as an argument and 57 | returning n colors.} 58 | \item{xlab, ylab}{x- and y-axis label.} 59 | \item{main}{main title.} 60 | \item{newpage}{should a new page start?.} 61 | \item{type, xaxt, yaxt}{strings to be used (when set to \code{"n"}) for 62 | suppressing the plotting of hexagon symbols, or the x- or y-axis, 63 | respectively.} 64 | \item{clip}{either 'on' or 'off' are the allowed arguments, when on 65 | everything is clipped to the plotting region.} 66 | \item{verbose}{logical indicating if some diagnostic output should happen.} 67 | } 68 | \details{ 69 | This is the (S4) \code{\link{plot}} method for \code{\link{hexbin}} (and 70 | \code{erodebin}) objects (\link{erodebin-class}). 71 | 72 | To use the standalone function 73 | \code{gplot.hexbin()} is \bold{\emph{deprecated}}. 74 | For \code{style}, \code{minarea} etc, see the \bold{Details} section of 75 | \code{\link{grid.hexagons}}'s help page. 76 | 77 | The legend functionality is somewhat preliminary. Later versions may 78 | include refinements and handle extreme cases (small and large) for 79 | cell size and counts. 80 | 81 | All arguments of \code{gplot.hexbin} can also be used for the S4 82 | \code{\link{plot}} method. 83 | } 84 | \value{ 85 | invisibly, a list with components 86 | \item{plot.vp}{the \code{\link{hexViewport}} constructed and used.} 87 | \item{legend.vp}{if a legend has been produced, its 88 | \code{\link[grid]{viewport}}.} 89 | } 90 | 91 | \references{ see in \code{\link{grid.hexagons}}.} 92 | \author{ 93 | Dan Carr \email{dcarr@voxel.galaxy.gmu.edu}, 94 | ported by Nicholas Lewin-Koh \email{kohnicho@comp.nus.edu.sg} and 95 | Martin Maechler. 96 | } 97 | \seealso{\code{\link{hexbin}}, \code{\link{hexViewport}}, 98 | \code{\link{smooth.hexbin}}, 99 | \code{\link{erode.hexbin}}, 100 | \code{\link{hcell2xy}}, \code{\link{hboxplot}}, 101 | \code{\link{hdiffplot}}. %%, \code{\link{hmatplot}}. 102 | } 103 | \examples{ 104 | ## 1) simple binning of spherical normal: 105 | x <- rnorm(10000) 106 | y <- rnorm(10000) 107 | bin <- hexbin(x,y) 108 | 109 | ## Plot method for hexbin ! 110 | ## ---- ------ -------- 111 | plot(bin) 112 | # nested lattice 113 | plot(bin, style= "nested.lattice") 114 | 115 | # controlling the colorscheme 116 | plot(bin, colramp=BTY, colorcut=c(0,.1,.2,.3,.4,.6,1)) 117 | 118 | ## 2) A mixture distribution 119 | x <- c(rnorm(5000),rnorm(5000,4,1.5)) 120 | y <- c(rnorm(5000),rnorm(5000,2,3)) 121 | bin <- hexbin(x,y) 122 | 123 | pens <- cbind(c("#ECE2F0","#A6BDDB","#1C9099"), 124 | c("#FFF7BC","#FEC44F","#D95F0E")) 125 | plot(bin, style = "nested.lattice", pen=pens) 126 | # now really crazy 127 | plot(bin, style = "nested.lattice", pen=pens,border=2,density=35) 128 | 129 | # lower resolution binning and overplotting with counts 130 | bin <- hexbin(x,y,xbins=25) 131 | P <- plot(bin, style="lattice", legend=FALSE, 132 | minarea=1, maxarea=1, border="white") 133 | ## 134 | %% FIXME! 135 | library("grid") 136 | pushHexport(P$plot.vp) 137 | xy <- hcell2xy(bin) 138 | # to show points rather than counts : 139 | grid.points(x,y,pch=18,gp=gpar(cex=.3,col="green")) 140 | grid.text(as.character(bin@count), xy$x,xy$y, 141 | gp=gpar(cex=0.3, col="red"),default.units="native") 142 | popViewport() 143 | 144 | # Be creative, have fun! 145 | } 146 | \keyword{hplot} 147 | -------------------------------------------------------------------------------- /tests/hdiffplot.Rout.save: -------------------------------------------------------------------------------- 1 | 2 | R version 4.4.1 (2024-06-14) -- "Race for Your Life" 3 | Copyright (C) 2024 The R Foundation for Statistical Computing 4 | Platform: x86_64-pc-linux-gnu 5 | 6 | R is free software and comes with ABSOLUTELY NO WARRANTY. 7 | You are welcome to redistribute it under certain conditions. 8 | Type 'license()' or 'licence()' for distribution details. 9 | 10 | R is a collaborative project with many contributors. 11 | Type 'contributors()' for more information and 12 | 'citation()' on how to cite R or R packages in publications. 13 | 14 | Type 'demo()' for some demos, 'help()' for on-line help, or 15 | 'help.start()' for an HTML browser interface to help. 16 | Type 'q()' to quit R. 17 | 18 | > library(hexbin) 19 | > 20 | > if(R.version$major != "1" || as.numeric(R.version$minor) >= 7) 21 | + RNGversion("1.6") 22 | Warning messages: 23 | 1: In RNGkind("Marsaglia-Multicarry", "Buggy Kinderman-Ramage", "Rounding") : 24 | buggy version of Kinderman-Ramage generator used 25 | 2: In RNGkind("Marsaglia-Multicarry", "Buggy Kinderman-Ramage", "Rounding") : 26 | non-uniform 'Rounding' sampler used 27 | 3: In RNGkind("Marsaglia-Multicarry", "Buggy Kinderman-Ramage", "Rounding") : 28 | RNGkind: Marsaglia-Multicarry has poor statistical properties 29 | > set.seed(213) 30 | > x1 <- rnorm(10000) 31 | > y1 <- rnorm(10000) 32 | > 33 | > x2 <- rnorm(10000,mean = .3) 34 | > y2 <- rnorm(10000,mean = .3) 35 | > 36 | > rx <- range(x1,x2) 37 | > ry <- range(y1,y2) 38 | > 39 | > str(bin1 <- hexbin(x1,y1, xbnds = rx, ybnds = ry)) 40 | Formal class 'hexbin' [package "hexbin"] with 16 slots 41 | ..@ cell : int [1:535] 16 20 48 70 74 75 76 80 99 101 ... 42 | ..@ count : int [1:535] 1 1 1 1 1 1 1 1 1 1 ... 43 | ..@ xcm : num [1:535] 0.37 1.338 0.721 -1.846 -0.965 ... 44 | ..@ ycm : num [1:535] -3.66 -3.71 -3.54 -3.2 -3.24 ... 45 | ..@ xbins : num 30 46 | ..@ shape : num 1 47 | ..@ xbnds : num [1:2] -3.8 4.3 48 | ..@ ybnds : num [1:2] -3.71 4.17 49 | ..@ dimen : num [1:2] 36 31 50 | ..@ n : int 10000 51 | ..@ ncells: int 535 52 | ..@ call : language hexbin(x = x1, y = y1, xbnds = rx, ybnds = ry) 53 | ..@ xlab : chr "x1" 54 | ..@ ylab : chr "y1" 55 | ..@ cID : NULL 56 | ..@ cAtt : int(0) 57 | > str(bin2 <- hexbin(x2,y2, xbnds = rx, ybnds = ry)) 58 | Formal class 'hexbin' [package "hexbin"] with 16 slots 59 | ..@ cell : int [1:545] 41 51 75 76 104 107 110 114 136 138 ... 60 | ..@ count : int [1:545] 1 1 1 1 1 2 1 1 2 1 ... 61 | ..@ xcm : num [1:545] -1.141 1.445 -0.493 -0.324 -0.995 ... 62 | ..@ ycm : num [1:545] -3.42 -3.45 -3.24 -3.35 -2.9 ... 63 | ..@ xbins : num 30 64 | ..@ shape : num 1 65 | ..@ xbnds : num [1:2] -3.8 4.3 66 | ..@ ybnds : num [1:2] -3.71 4.17 67 | ..@ dimen : num [1:2] 36 31 68 | ..@ n : int 10000 69 | ..@ ncells: int 545 70 | ..@ call : language hexbin(x = x2, y = y2, xbnds = rx, ybnds = ry) 71 | ..@ xlab : chr "x2" 72 | ..@ ylab : chr "y2" 73 | ..@ cID : NULL 74 | ..@ cAtt : int(0) 75 | > 76 | > str(erode(bin1)) 77 | Formal class 'erodebin' [package "hexbin"] with 19 slots 78 | ..@ eroded: logi [1:535] FALSE FALSE FALSE FALSE FALSE FALSE ... 79 | ..@ cdfcut: num 0.5 80 | ..@ erode : int [1:71] 12 35 34 57 52 4 30 101 138 150 ... 81 | ..@ cell : int [1:535] 16 20 48 70 74 75 76 80 99 101 ... 82 | ..@ count : int [1:535] 1 1 1 1 1 1 1 1 1 1 ... 83 | ..@ xcm : num [1:535] 0.37 1.338 0.721 -1.846 -0.965 ... 84 | ..@ ycm : num [1:535] -3.66 -3.71 -3.54 -3.2 -3.24 ... 85 | ..@ xbins : num 30 86 | ..@ shape : num 1 87 | ..@ xbnds : num [1:2] -3.8 4.3 88 | ..@ ybnds : num [1:2] -3.71 4.17 89 | ..@ dimen : num [1:2] 36 31 90 | ..@ n : int 10000 91 | ..@ ncells: int 535 92 | ..@ call : language hexbin(x = x1, y = y1, xbnds = rx, ybnds = ry) 93 | ..@ xlab : chr "x1" 94 | ..@ ylab : chr "y1" 95 | ..@ cID : NULL 96 | ..@ cAtt : int(0) 97 | > 98 | > str(smbin1 <- smooth.hexbin(bin1)) 99 | Formal class 'smoothbin' [package "hexbin"] with 17 slots 100 | ..@ wts : num [1:3] 48 4 1 101 | ..@ cell : int [1:906] 17 18 19 21 22 23 51 52 53 54 ... 102 | ..@ count : int [1:906] 1 1 1 1 1 1 1 4 5 2 ... 103 | ..@ xcm : num [1:535] 0.37 1.338 0.721 -1.846 -0.965 ... 104 | ..@ ycm : num [1:535] -3.66 -3.71 -3.54 -3.2 -3.24 ... 105 | ..@ xbins : num 34 106 | ..@ shape : num 1 107 | ..@ xbnds : num [1:2] -4.34 4.84 108 | ..@ ybnds : num [1:2] -4.23 4.7 109 | ..@ dimen : num [1:2] 40 35 110 | ..@ n : int 10000 111 | ..@ ncells: int 535 112 | ..@ call : language hexbin(x = x1, y = y1, xbnds = rx, ybnds = ry) 113 | ..@ xlab : chr "x1" 114 | ..@ ylab : chr "y1" 115 | ..@ cID : NULL 116 | ..@ cAtt : int(0) 117 | > (smbin2 <- smooth.hexbin(bin2)) 118 | 'hexbin' object from call: hexbin(x = x2, y = y2, xbnds = rx, ybnds = ry) 119 | n = 10000 points in nc = 545 hexagon cells in grid dimensions 40 by 35 120 | > 121 | > str(erodebin1 <- erode.hexbin(smbin1)) 122 | Formal class 'erodebin' [package "hexbin"] with 19 slots 123 | ..@ eroded: logi [1:906] FALSE FALSE FALSE FALSE FALSE FALSE ... 124 | ..@ cdfcut: num 0.5 125 | ..@ erode : int [1:73] 11 35 95 100 117 88 6 39 167 232 ... 126 | ..@ cell : int [1:906] 17 18 19 21 22 23 51 52 53 54 ... 127 | ..@ count : int [1:906] 1 1 1 1 1 1 1 4 5 2 ... 128 | ..@ xcm : num [1:535] 0.37 1.338 0.721 -1.846 -0.965 ... 129 | ..@ ycm : num [1:535] -3.66 -3.71 -3.54 -3.2 -3.24 ... 130 | ..@ xbins : num 34 131 | ..@ shape : num 1 132 | ..@ xbnds : num [1:2] -4.34 4.84 133 | ..@ ybnds : num [1:2] -4.23 4.7 134 | ..@ dimen : num [1:2] 40 35 135 | ..@ n : int 10000 136 | ..@ ncells: int 535 137 | ..@ call : language hexbin(x = x1, y = y1, xbnds = rx, ybnds = ry) 138 | ..@ xlab : chr "x1" 139 | ..@ ylab : chr "y1" 140 | ..@ cID : NULL 141 | ..@ cAtt : int(0) 142 | > (erodebin2 <- erode.hexbin(smbin2)) 143 | 'hexbin' object from call: hexbin(x = x2, y = y2, xbnds = rx, ybnds = ry) 144 | n = 10000 points in nc = 545 hexagon cells in grid dimensions 40 by 35 145 | > 146 | > if(FALSE)## does not work -- what funny stuff is hdiffplot() doing??? 147 | + par(mfrow = c(2,1)) 148 | > 149 | > if(exists("hdiffplot", mode="function")) { ## not yet in new hexbin 150 | + hdiffplot(bin1,bin2, main = "Original N(0,*) Random bins") 151 | + 152 | + hdiffplot(smbin1,smbin2, main = "smooth.hexbin() smoothed bins") 153 | + 154 | + plot.new() 155 | + hdiffplot(erodebin1,erodebin2, main = "erode.hexbin()d smoothed bins") 156 | + }# not yet 157 | > 158 | > proc.time() 159 | user system elapsed 160 | 0.486 1.388 0.369 161 | -------------------------------------------------------------------------------- /R/grid.hexlegend.R: -------------------------------------------------------------------------------- 1 | grid.hexlegend <- 2 | function(legend, ysize, lcex, inner, 3 | style = "colorscale", 4 | minarea = 0.05, maxarea = 0.8, mincnt = 1, maxcnt, trans = NULL, 5 | inv = NULL, colorcut, density = NULL, border = NULL, pen = NULL, 6 | colramp = function(n) { LinGray(n,beg = 90,end = 15) }, 7 | leg.unit="native") 8 | { 9 | ## the formal arg matching should happen 10 | style <- match.arg(style, eval(formals(grid.hexagons)[["style"]])) 11 | 12 | if (style %in% c("centroids", "lattice", "colorscale")) { 13 | ## _______________tranformations_______________________ 14 | if(is.null(trans)) { 15 | sc <- maxcnt - mincnt 16 | bnds <- round(mincnt + sc * colorcut) 17 | } 18 | 19 | else { 20 | if(!is.function(trans) && !is.function(inv)) 21 | stop("'trans' and 'inv' must both be functions if 'trans' is not NULL") 22 | con <- trans(mincnt) 23 | sc <- trans(maxcnt) - con 24 | bnds <- round(inv(con + sc * colorcut)) 25 | } 26 | } 27 | 28 | if(style == "colorscale") { ## use own 'inner' 29 | n <- length(bnds) 30 | spacing <- ysize/(n + 3) 31 | inner <- min(legend/3.5, (sqrt(3) * spacing)/2) 32 | } 33 | dx <- inner/2 34 | dy <- dx/sqrt(3) 35 | hexC <- hexcoords(dx, dy, n = 1,sep=NULL) 36 | 37 | ## _______________Plotting______________________________ 38 | switch(style, 39 | "colorscale" = { 40 | midx <- legend/3 41 | textx <- (2 * legend)/3 42 | tx <- hexC$x + midx 43 | pen <- colramp(n) 44 | for(i in seq(length = n-1)) { 45 | grid.polygon(tx,hexC$y + i * spacing, 46 | default.units=leg.unit,id=NULL,id.lengths=6, 47 | gp=gpar(fill = pen[i], col = border)) 48 | grid.text(as.character(bnds[i]), textx, (i - 0.5) * spacing, 49 | default.units=leg.unit, gp=gpar(cex = lcex)) 50 | } 51 | grid.text(as.character(bnds[n]), textx, (n - 0.5) * spacing, 52 | default.units=leg.unit, gp=gpar(cex = lcex)) 53 | grid.text("Counts", legend/2, (n + 1.5) * spacing, 54 | default.units=leg.unit, gp=gpar(cex = 1.7 * lcex)) 55 | }, 56 | 57 | "centroids" = , 58 | "lattice" = { 59 | ## NL Solved hex overlap problem on legend 60 | ## Need to tackle too many categories 61 | radius <- sqrt(minarea + (maxarea - minarea) * colorcut) 62 | n <- length(radius) 63 | shift <- c(0, 2*dy*radius) 64 | shift <- shift[1:n] + shift[2:(n+1)] 65 | #labht <- max(strheight(as.character(bnds), cex = lcex)) 66 | labht <- convertY(unit(get.gpar(names = "fontsize")[[1]]*lcex, 67 | "points"),"native",valueOnly = TRUE) 68 | shift <- pmax(labht, shift) 69 | six <- rep.int(6:6, n) 70 | xmid <- legend/3 71 | inc <- ysize/(n+3) 72 | if(inc > max(shift)) y <- inc * 1:n 73 | else { 74 | y <- cumsum(shift) 75 | extra.slop <- (n * inc) - y[n] # FIXME? y[n] == sum(shift) 76 | shift[-1] <- shift[-1] + extra.slop/(n-1) 77 | y <- cumsum(shift) 78 | ## (y+(1/n)*extra.slop)-y[1] 79 | ## delta <- max(log(shift))-min(log(shift)) 80 | ## fudge <- extra.slop*(diff(log(shift))/delta) 81 | ## y<- c(y[1], y[-1]+ fudge ) 82 | } 83 | textx <- rep.int((2 * legend)/3, n) 84 | ## ____________________plotting______________________ 85 | if(is.null(pen)) pen <- 1 86 | if(is.null(border)) border <- pen 87 | grid.polygon(x = rep.int(hexC$x,n)* 88 | rep.int(radius, six) + rep.int(xmid, 6 * n), 89 | y = rep.int(hexC$y,n)* 90 | rep.int(radius, six) + rep.int(y, six), 91 | default.units=leg.unit, id=NULL, 92 | id.lengths=rep.int(6,n), 93 | gp=gpar(fill = pen, col = border)) 94 | 95 | grid.text(as.character(bnds), textx, y, 96 | default.units=leg.unit, gp=gpar(cex = lcex)) 97 | grid.text("Counts", legend/2, (n + 2) * inc, 98 | default.units=leg.unit, gp=gpar(cex =1.7 * lcex)) 99 | }, 100 | 101 | "nested.lattice" = , 102 | "nested.centroids" = { 103 | ## _____________x scaling_____________________________ 104 | numb <- cut(floor(legend/inner), breaks = c(-1, 0, 2,4)) 105 | ## Note: In old code 106 | ## top breaks=c(-1,0,2,4,8), numb<- 5 and size=1:9 107 | if(is.na(numb)) 108 | numb <- 4 109 | switch(numb, 110 | {warning("not enough space for legend"); return()}, 111 | size <- 5, 112 | size <- c(1, 5, 9), 113 | size <- c(1, 3, 5, 7, 9)) 114 | xmax <- length(size) 115 | radius <- sqrt(minarea + (maxarea - minarea) * (size - 1)/9) 116 | txt <- as.character(size) 117 | ##___________________y scaling_____________________ 118 | lab <- c("Ones", "Tens", "Hundreds", 119 | "Thousands", "10 Thousands", "100 Thousands", 120 | "Millions", "10 Millions", 121 | "100 Millions", "Billions") 122 | power <- floor(log10(maxcnt)) + 1 123 | yinc <- 16 * dy 124 | if(ysize/power < yinc) 125 | {warning("Not enough height for legend"); return()} 126 | xmid <- legend/10 127 | x <- inner * (1:xmax - (1 + xmax)/2) + xmid 128 | n <- length(x) 129 | tx <- rep.int(hexC$x, n) 130 | ty <- rep.int(hexC$y, n) 131 | six <- rep.int(6:6, n) 132 | y <- rep.int(3 * dy - yinc, xmax) 133 | ## ____________________plotting______________________ 134 | if(is.null(pen)) { 135 | pen <- 1:power +1 136 | pen <- cbind(pen, pen +10) 137 | } 138 | if(is.null(border)) border <- FALSE 139 | for(i in 1:power) { 140 | y <- y + yinc 141 | hexpolygon(x, y, hexC, 142 | col = pen[i,1], border = border) 143 | grid.polygon(x= tx * rep.int(radius, six) + rep.int(x, six), 144 | y= ty * rep.int(radius, six) + rep.int(y, six), 145 | default.units=leg.unit, id=NULL, 146 | id.lengths=rep(6,n), 147 | gp=gpar(fill = pen[i,2], col = border)) 148 | 149 | grid.text(txt, x, y - 4.5 * dy, 150 | default.units=leg.unit, gp=gpar(cex = lcex)) 151 | ##adj= 0.5, cex = lcex) 152 | grid.text(lab[i], xmid, y[1] + 4.5 * dy, 153 | default.units=leg.unit, gp=gpar(cex = 1.7*lcex)) 154 | ##adj= 0.5, cex = 1.7*lcex) 155 | } 156 | })## switch(style = *) 157 | }## hex.legend() 158 | -------------------------------------------------------------------------------- /src/herode.f: -------------------------------------------------------------------------------- 1 | C File: herode.f 2 | C Version date: Jan 4, 1994 3 | C Programmer: Daniel B. Carr 4 | C 5 | C The vector erode returns the gray-level erosion order for hexagon cells. 6 | C The erosion cycle is: 7 | C cycle = (erode-1)/6 + 1 8 | C Many cells may be eroded in the same cycle 9 | C A tie break is the cell count deficit at erosion time: 10 | C deficit=erode - 6*cycle 11 | C The last eroded cell might be considered a bivariate median 12 | C 13 | C The algorithm: 14 | C Repeat until no cells are left in the list. 15 | C Process list 16 | C Reduce the cell counts by the a multiple of exposed sides 17 | C If a cell count is zero or less after an erosion cycle 18 | C let order=order + 6 19 | C report erode = order + cell count (count is <= 0) 20 | C remove the cell from consideration 21 | C update exposed side counts for existing neighbor cells 22 | C if exposed sides was zero, temporarily store id's 23 | C else 24 | C compress list 25 | C endif 26 | C Add temporarily stored id's to list 27 | C End Repeat 28 | 29 | subroutine herode(cell,cnt,n,bdim, 30 | * erode,ncnt,ncell,sides,neib,exist) 31 | 32 | C 33 | C 34 | implicit none 35 | 36 | integer cell(*), cnt(*) ! cell id and count 37 | integer n, bdim(2) ! number of cells and 2-D array bounds 38 | integer erode(*) ! erosion status 39 | integer ncell(*),ncnt(*) ! extracted id's and expanded counts 40 | integer sides(*) ! number of exposed sides 41 | integer neib(6,*) ! pointers to the neighbors 42 | logical exist(0:*) ! cell existence 43 | 44 | integer nrow, ncol, Lmax ! dimensions 45 | integer inc1(6), inc2(6) ! increments to get neighbors 46 | integer i, icell, j, k, L ! subscripts 47 | integer nc, nnc, nb, ninc, r, c !more subscripts 48 | integer loop, order, maxcnt 49 | 50 | 51 | C_______Zero cell ordering numbers________________________________ 52 | 53 | order=0 54 | 55 | C_______Load the increment arrays and constants 56 | 57 | nrow = bdim(1) 58 | ncol = bdim(2) 59 | Lmax = nrow * ncol 60 | nnc = n 61 | 62 | C______Load increment arrays to neigbors______________ 63 | C 64 | C order=right, up left, down left, up right, left, down right 65 | 66 | inc1(1)= 1 67 | inc1(2)= ncol-1 68 | inc1(3)= -ncol-1 69 | inc1(4)= ncol 70 | inc1(5)=-1 71 | inc1(6)= -ncol 72 | 73 | inc2(1)= 1 74 | inc2(2)= ncol 75 | inc2(3)= -ncol 76 | inc2(4)= ncol+1 77 | inc2(5)=-1 78 | inc2(6)= -ncol+1 79 | 80 | 81 | c_______load working arrays_______________________________________________ 82 | 83 | do i=0,Lmax 84 | exist(i)=.false. 85 | enddo 86 | 87 | maxcnt=0 88 | do i=1,n 89 | icell=cell(i) 90 | ncnt(icell)=cnt(i) 91 | exist(icell)=.true. 92 | maxcnt=max(maxcnt,cnt(i)) 93 | enddo 94 | 95 | C_______Store pointers to cell neighbors_________________________ 96 | C 97 | C A pointer of 0 means the neigbor in out of bounds 98 | C Also find the max count 99 | C Speed: Can avoid adding 1's to r and c 100 | C but this code is easier to follow 101 | do i=1,n 102 | L=cell(i) 103 | k = L -1 104 | r=k/ncol+1 105 | c=mod(k,ncol)+1 106 | if(mod(r,2).eq.1)then 107 | do j = 1,6 108 | neib(j,L) = L + inc1(j) 109 | enddo 110 | 111 | if (c .eq. 1) then 112 | neib(2,L) = 0 113 | neib(3,L) = 0 114 | neib(5,L) = 0 115 | else if (c .eq. ncol) then 116 | neib(1,L) = 0 117 | endif 118 | 119 | if (r .eq. 1) then 120 | neib(3,L) = 0 121 | neib(6,L) = 0 122 | else if(r.eq.nrow)then 123 | neib(2,L) = 0 124 | neib(4,L) =0 125 | endif 126 | 127 | else 128 | do j= 1,6 129 | neib(j,L) = L + inc2(j) 130 | enddo 131 | 132 | if (c .eq. 1) then 133 | neib(5,L) = 0 134 | else if (c .eq. ncol) then 135 | neib(1,L) = 0 136 | neib(4,L) = 0 137 | neib(6,L) = 0 138 | endif 139 | 140 | if (r .eq. nrow) then 141 | neib(2,L) = 0 142 | neib(4,L) = 0 143 | endif 144 | 145 | endif 146 | enddo 147 | 148 | 149 | C_______Count exposed sides for cells in the contour_________________ 150 | 151 | do i=1,n 152 | icell=cell(i) 153 | sides(icell)=0 154 | do j=1,6 155 | if(.not. exist( neib(j,icell) ) )then 156 | sides(icell)=sides(icell)+ 1 157 | endif 158 | enddo 159 | enddo 160 | 161 | C________Grab surface cells___________________________________________ 162 | 163 | nc=0 164 | do i=1,n 165 | if(sides(cell(i)).gt.0)then 166 | nc=nc+1 167 | ncell(nc)=cell(i) 168 | endif 169 | enddo 170 | n=nc !n is now the number of exposed, non-empty cells 171 | 172 | C_______The outer loop________________________________________________ 173 | C 174 | C temporary indices 175 | C nc: index for cells remaining on the list 176 | C ninc: index for newly exposed cells added to back of list 177 | 178 | do while(n.gt.0) 179 | 180 | C Subtract exposed-side counts from the surface cell counts 181 | C until at least one cell is empty. 182 | 183 | loop=maxcnt 184 | do i=1,n 185 | icell=ncell(i) 186 | loop=min( (ncnt(icell)-1)/sides(icell) , loop) 187 | enddo 188 | loop=loop+1 !all loop values are 1 too small 189 | 190 | C update the counts, rank and remove eroded cells 191 | 192 | nc=0 193 | order=order+6 194 | ninc=n 195 | do i=1,n 196 | icell=ncell(i) 197 | ncnt(icell)=ncnt(icell)-sides(icell)*loop 198 | if(ncnt(icell).le.0)then 199 | 200 | C Remove the empty cell and store it's order 201 | exist(icell)=.false. 202 | erode(icell)=order+ncnt(icell) 203 | 204 | C Update the neighbors of the empty cell 205 | do j=1,6 206 | nb=neib(j,icell) 207 | if(exist(nb))then 208 | 209 | C Store cells for addition to surface list 210 | if(sides(nb).eq.0)then 211 | ninc=ninc+1 212 | ncell(ninc)=nb 213 | endif 214 | 215 | C Update sides for the neighbors 216 | sides(nb)=sides(nb)+1 217 | endif 218 | enddo 219 | else 220 | 221 | C Save remaining cells 222 | nc=nc+1 223 | ncell(nc)=ncell(i) 224 | endif 225 | enddo 226 | 227 | C Add new surface cells if any 228 | 229 | do i=n+1,ninc,1 230 | nc=nc+1 231 | ncell(nc)=ncell(i) 232 | enddo 233 | n=nc 234 | enddo 235 | 236 | C_______compress result___________________________________________ 237 | 238 | 239 | do i=1,nnc 240 | erode(i)=erode(cell(i)) 241 | enddo 242 | n=nnc 243 | 244 | return 245 | end 246 | -------------------------------------------------------------------------------- /R/LOCS.R: -------------------------------------------------------------------------------- 1 | LinOCS <- function(n,beg = 1,end = 256) 2 | { 3 | if(beg < 1 || end < 1 || beg > 256 || end > 256) 4 | stop("`beg' and `end' must be numbers in the interval [1,256]") 5 | 6 | M <- rbind(c(0,0,0), 7 | c(0,0,0), 8 | c(0,0,0), 9 | c(1,0,0), 10 | c(2,0,0), 11 | c(2,0,0), 12 | c(3,0,0), 13 | c(3,0,0), 14 | c(4,0,0), 15 | c(5,0,0), 16 | c(5,0,0), 17 | c(6,0,0), 18 | c(7,0,0), 19 | c(7,0,0), 20 | c(8,0,0), 21 | c(9,0,0), 22 | c(9,0,0), 23 | c(10,0,0), 24 | c(11,0,0), 25 | c(12,0,0), 26 | c(13,0,0), 27 | c(14,0,0), 28 | c(15,0,0), 29 | c(16,0,0), 30 | c(17,0,0), 31 | c(18,0,0), 32 | c(19,0,0), 33 | c(20,0,0), 34 | c(21,0,0), 35 | c(22,0,0), 36 | c(23,0,0), 37 | c(25,0,0), 38 | c(26,0,0), 39 | c(27,0,0), 40 | c(28,0,0), 41 | c(30,0,0), 42 | c(31,0,0), 43 | c(33,0,0), 44 | c(34,0,0), 45 | c(35,0,0), 46 | c(37,0,0), 47 | c(39,0,0), 48 | c(40,0,0), 49 | c(43,0,0), 50 | c(45,0,0), 51 | c(46,0,0), 52 | c(49,0,0), 53 | c(51,0,0), 54 | c(53,0,0), 55 | c(54,0,0), 56 | c(56,0,0), 57 | c(58,0,0), 58 | c(60,0,0), 59 | c(62,0,0), 60 | c(64,0,0), 61 | c(67,0,0), 62 | c(69,0,0), 63 | c(71,0,0), 64 | c(74,0,0), 65 | c(76,0,0), 66 | c(80,0,0), 67 | c(81,0,0), 68 | c(84,0,0), 69 | c(86,0,0), 70 | c(89,0,0), 71 | c(92,0,0), 72 | c(94,0,0), 73 | c(97,0,0), 74 | c(100,0,0), 75 | c(103,0,0), 76 | c(106,0,0), 77 | c(109,0,0), 78 | c(112,0,0), 79 | c(115,0,0), 80 | c(117,0,0), 81 | c(122,0,0), 82 | c(126,0,0), 83 | c(128,0,0), 84 | c(131,0,0), 85 | c(135,0,0), 86 | c(135,0,0), 87 | c(135,1,0), 88 | c(135,2,0), 89 | c(135,3,0), 90 | c(135,4,0), 91 | c(135,6,0), 92 | c(135,6,0), 93 | c(135,8,0), 94 | c(135,9,0), 95 | c(135,10,0), 96 | c(135,11,0), 97 | c(135,13,0), 98 | c(135,13,0), 99 | c(135,15,0), 100 | c(135,17,0), 101 | c(135,17,0), 102 | c(135,19,0), 103 | c(135,21,0), 104 | c(135,22,0), 105 | c(135,23,0), 106 | c(135,25,0), 107 | c(135,26,0), 108 | c(135,27,0), 109 | c(135,29,0), 110 | c(135,31,0), 111 | c(135,32,0), 112 | c(135,33,0), 113 | c(135,35,0), 114 | c(135,36,0), 115 | c(135,38,0), 116 | c(135,40,0), 117 | c(135,42,0), 118 | c(135,44,0), 119 | c(135,46,0), 120 | c(135,47,0), 121 | c(135,49,0), 122 | c(135,51,0), 123 | c(135,52,0), 124 | c(135,54,0), 125 | c(135,56,0), 126 | c(135,57,0), 127 | c(135,59,0), 128 | c(135,62,0), 129 | c(135,63,0), 130 | c(135,65,0), 131 | c(135,67,0), 132 | c(135,69,0), 133 | c(135,72,0), 134 | c(135,73,0), 135 | c(135,76,0), 136 | c(135,78,0), 137 | c(135,80,0), 138 | c(135,82,0), 139 | c(135,84,0), 140 | c(135,87,0), 141 | c(135,88,0), 142 | c(135,90,0), 143 | c(135,93,0), 144 | c(135,95,0), 145 | c(135,98,0), 146 | c(135,101,0), 147 | c(135,103,0), 148 | c(135,106,0), 149 | c(135,107,0), 150 | c(135,110,0), 151 | c(135,113,0), 152 | c(135,115,0), 153 | c(135,118,0), 154 | c(135,121,0), 155 | c(135,124,0), 156 | c(135,127,0), 157 | c(135,129,0), 158 | c(135,133,0), 159 | c(135,135,0), 160 | c(135,138,0), 161 | c(135,141,0), 162 | c(135,144,0), 163 | c(135,148,0), 164 | c(135,150,0), 165 | c(135,155,0), 166 | c(135,157,0), 167 | c(135,160,0), 168 | c(135,163,0), 169 | c(135,166,0), 170 | c(135,170,0), 171 | c(135,174,0), 172 | c(135,177,0), 173 | c(135,180,0), 174 | c(135,184,0), 175 | c(135,188,0), 176 | c(135,192,0), 177 | c(135,195,0), 178 | c(135,200,0), 179 | c(135,203,0), 180 | c(135,205,0), 181 | c(135,210,0), 182 | c(135,214,0), 183 | c(135,218,0), 184 | c(135,222,0), 185 | c(135,226,0), 186 | c(135,231,0), 187 | c(135,236,0), 188 | c(135,239,0), 189 | c(135,244,0), 190 | c(135,249,0), 191 | c(135,254,0), 192 | c(135,255,1), 193 | c(135,255,5), 194 | c(135,255,10), 195 | c(135,255,15), 196 | c(135,255,20), 197 | c(135,255,23), 198 | c(135,255,28), 199 | c(135,255,33), 200 | c(135,255,38), 201 | c(135,255,43), 202 | c(135,255,45), 203 | c(135,255,49), 204 | c(135,255,54), 205 | c(135,255,59), 206 | c(135,255,65), 207 | c(135,255,70), 208 | c(135,255,74), 209 | c(135,255,80), 210 | c(135,255,84), 211 | c(135,255,90), 212 | c(135,255,95), 213 | c(135,255,98), 214 | c(135,255,104), 215 | c(135,255,110), 216 | c(135,255,116), 217 | c(135,255,120), 218 | c(135,255,125), 219 | c(135,255,131), 220 | c(135,255,137), 221 | c(135,255,144), 222 | c(135,255,149), 223 | c(135,255,154), 224 | c(135,255,158), 225 | c(135,255,165), 226 | c(135,255,172), 227 | c(135,255,179), 228 | c(135,255,186), 229 | c(135,255,191), 230 | c(135,255,198), 231 | c(135,255,203), 232 | c(135,255,211), 233 | c(135,255,216), 234 | c(135,255,224), 235 | c(135,255,232), 236 | c(135,255,240), 237 | c(135,255,248), 238 | c(135,255,254), 239 | c(135,255,255), 240 | c(140,255,255), 241 | c(146,255,255), 242 | c(153,255,255), 243 | c(156,255,255), 244 | c(161,255,255), 245 | c(168,255,255), 246 | c(172,255,255), 247 | c(177,255,255), 248 | c(182,255,255), 249 | c(189,255,255), 250 | c(192,255,255), 251 | c(199,255,255), 252 | c(204,255,255), 253 | c(210,255,255), 254 | c(215,255,255), 255 | c(220,255,255), 256 | c(225,255,255), 257 | c(232,255,255), 258 | c(236,255,255), 259 | c(240,255,255), 260 | c(248,255,255), 261 | c(255,255,255))[ round(seq(beg,end,length = n)), ] 262 | 263 | rgb(M[,1]/255, 264 | M[,2]/255, 265 | M[,3]/255) 266 | } 267 | -------------------------------------------------------------------------------- /man/plotMAhex.Rd: -------------------------------------------------------------------------------- 1 | \name{plotMAhex} 2 | \alias{plotMAhex} 3 | \title{MA-plot using hexagon bins} 4 | \description{ 5 | Creates an MA-plot using hexagons with color/glyph coding for control spots. 6 | } 7 | \usage{ 8 | plotMAhex(MA, array = 1, xlab = "A", ylab = "M", 9 | main = colnames(MA)[array], xlim = NULL, ylim = NULL, 10 | status = NULL, values, pch, col, cex, nbin = 40, 11 | zero.weights = FALSE, style = "colorscale", legend = 1.2, 12 | lcex = 1, minarea = 0.04, maxarea = 0.8, mincnt = 2, 13 | maxcnt = NULL, trans = NULL, inv = NULL, colorcut = NULL, 14 | border = NULL, density = NULL, pen = NULL, 15 | colramp = function(n) { LinGray(n, beg = 90, end = 15) }, 16 | newpage = TRUE, type = c("p", "l", "n"), 17 | xaxt = c("s", "n"), yaxt = c("s", "n"), 18 | verbose = getOption("verbose")) 19 | } 20 | \arguments{ 21 | \item{MA}{an \code{RGList}, \code{MAList} or \code{MArrayLM} object, 22 | or any list with components \code{M} containing log-ratios and 23 | \code{A} containing average intensities. Alternatively a 24 | \code{matrix}, \code{Affybatch} or \code{ExpressionSet} object.} 25 | \item{array}{integer giving the array to be plotted. Corresponds to 26 | columns of \code{M} and \code{A}.} 27 | \item{xlab, ylab, main}{character strings giving label for x-axis, 28 | y-axis or main tile of the plot.} 29 | \item{xlim, ylim}{numeric vectors of length 2 giving limits for x-axis 30 | (or y-axis respectively), defaulting to min and max of the data.} 31 | \item{status}{character vector giving the control status of each spot 32 | on the array, of same length as the number of rows of \code{MA$M}. 33 | If omitted, all points are plotted in the default color, symbol and size.} 34 | \item{values}{character vector giving values of \code{status} to be 35 | highlighted on the plot. Defaults to unique values of \code{status}. 36 | Ignored if there is no \code{status} vector.} 37 | \item{pch}{vector or list of plotting characters. Default to integer code 16. 38 | Ignored is there is no \code{status} vector.} 39 | \item{col}{numeric or character vector of colors, of the same length 40 | as \code{values}. Defaults to \code{1:length(values)}. Ignored if 41 | there is no \code{status} vector.} 42 | \item{cex}{numeric vector of plot symbol expansions, of the the same 43 | length as \code{values}. Defaults to 0.2 for the most common status 44 | value and 1 for the others. Ignored if there is no \code{status} vector.} 45 | \item{nbin}{ Number of bins } %% << FIXME 46 | \item{zero.weights}{logical, should spots with zero or negative 47 | weights be plotted?} 48 | \item{style}{string specifying the style of hexagon plot, 49 | see \code{\link{grid.hexagons}} for the possibilities.} 50 | \item{legend}{numeric width of the legend in inches of \code{FALSE}. 51 | In the latter case, or when \code{0}, no legend is not produced.} 52 | \item{lcex}{characters expansion size for the text in the legend.} 53 | \item{minarea}{fraction of cell area for the lowest count.} 54 | \item{maxarea}{fraction of the cell area for the largest count.} 55 | \item{mincnt}{cells with fewer counts are ignored.} 56 | \item{maxcnt}{cells with more counts are ignored.} 57 | \item{trans}{\code{\link{function}} specifying a transformation for 58 | the counts such as \code{sqrt}.} 59 | \item{inv}{the inverse transformation of \code{trans}.} 60 | \item{colorcut}{vector of values covering [0, 1] that determine 61 | hexagon color class boundaries and hexagon legend size boundaries. 62 | Alternatively, an integer (\code{<= maxcnt}) specifying the 63 | \emph{number} of equispaced colorcut values in [0,1].} 64 | \item{border, density, pen}{color for polygon borders and filling of 65 | each hexagon drawn, passed to \code{\link{grid.hexagons}}.} 66 | \item{colramp}{function accepting an integer \code{n} as an argument and 67 | returning n colors.} 68 | \item{newpage}{should a new page start?} 69 | \item{type, xaxt, yaxt}{strings to be used (when set to \code{"n"}) for 70 | suppressing the plotting of hexagon symbols, or the x- or y-axis, 71 | respectively.} 72 | \item{verbose}{logical indicating if some diagnostic output should happen.} 73 | } 74 | 75 | \details{ 76 | An MA-plot is a plot of log-intensity ratios (M-values) versus 77 | log-intensity averages (A-values). If \code{MA} is an \code{RGList} or 78 | \code{MAList} then this function produces an ordinary within-array 79 | MA-plot. If \code{MA} is an \code{MArrayLM} object, then the plot is an 80 | fitted model MA-plot in which the estimated coefficient is on the y-axis 81 | and the average A-value is on the x-axis. 82 | 83 | If \code{MA} is a \code{matrix} or \code{ExpressionSet} object, then this 84 | function produces a between-array MA-plot. In this case the A-values in 85 | the plot are the average log-intensities across the arrays and the 86 | M-values are the deviations of the log-intensities for the specified 87 | array from the average. If there are more than five arrays, then the 88 | average is computed robustly using medians. With five or fewer arrays, 89 | it is computed by means. 90 | 91 | The \code{status} vector is intended to specify the control status of 92 | each spot, for example "gene", "ratio control", "house keeping gene", 93 | "buffer" and so on. The vector is usually computed using the function 94 | \code{\link[limma]{controlStatus}} from package \pkg{limma} and a 95 | spot-types file. However the function may be used to highlight any 96 | subset of spots. 97 | 98 | The arguments \code{values}, \code{pch}, \code{col} and \code{cex} 99 | can be included as attributes to \code{status} instead of being 100 | passed as arguments to \code{plotMA}. 101 | 102 | See \code{\link[graphics]{points}} for possible values for \code{pch}, 103 | \code{col} and \code{cex}. 104 | } 105 | 106 | \value{ 107 | A plot is created on the current graphics device. 108 | and a list with the following items is returned invisibly: 109 | \item{plot.vp}{the \code{\link{hexViewport}} constructed and used.} 110 | \item{legend.vp}{if a legend has been produced, its 111 | \code{\link[grid]{viewport}}.} 112 | \item{hbin}{a \code{hexbin} object built with A as the x coordinate 113 | and M as the y coordinate.} 114 | } 115 | 116 | \references{See \url{http://www.statsci.org/micrarra/refs/maplots.html}} 117 | 118 | \author{Nicholas Lewin-Koh, adapted from code by Gordon Smyth} 119 | 120 | \seealso{\code{\link[limma:plotma]{plotMA}} from package \pkg{limma}, 121 | and \code{\link{gplot.hexbin}}. 122 | } 123 | 124 | \examples{ 125 | if(require(marray)){ %% for the data only --> data(swirl, package="marray") 126 | data(swirl) 127 | hb <- plotMAhex(swirl[,1],newpage=FALSE, 128 | main = "M vs A plot with hexagons", legend=0) 129 | hexVP.abline(hb$plot.vp,h=0,col=gray(.6)) 130 | hexMA.loess(hb) 131 | } 132 | } 133 | \keyword{hplot} 134 | -------------------------------------------------------------------------------- /R/HO.R: -------------------------------------------------------------------------------- 1 | heat.ob <- function(n,beg = 1,end = 256) 2 | { 3 | if(beg < 1 || end < 1 || beg > 256 || end > 256) 4 | stop("`beg' and `end' must be numbers in the interval [1,256]") 5 | 6 | M <- rbind(c(0, 0, 0), 7 | c(35, 0, 0), 8 | c(52, 0, 0), 9 | c(60, 0, 0), 10 | c(63, 1, 0), 11 | c(64, 2, 0), 12 | c(68, 5, 0), 13 | c(69, 6, 0), 14 | c(72, 8, 0), 15 | c(74,10, 0), 16 | c(77,12, 0), 17 | c(78,14, 0), 18 | c(81,16, 0), 19 | c(83,17, 0), 20 | c(85,19, 0), 21 | c(86,20, 0), 22 | c(89,22, 0), 23 | c(91,24, 0), 24 | c(92,25, 0), 25 | c(94,26, 0), 26 | c(95,28, 0), 27 | c(98,30, 0), 28 | c(100,31, 0), 29 | c(102,33, 0), 30 | c(103,34, 0), 31 | c(105,35, 0), 32 | c(106,36, 0), 33 | c(108,38, 0), 34 | c(109,39, 0), 35 | c(111,40, 0), 36 | c(112,42, 0), 37 | c(114,43, 0), 38 | c(115,44, 0), 39 | c(117,45, 0), 40 | c(119,47, 0), 41 | c(119,47, 0), 42 | c(120,48, 0), 43 | c(122,49, 0), 44 | c(123,51, 0), 45 | c(125,52, 0), 46 | c(125,52, 0), 47 | c(126,53, 0), 48 | c(128,54, 0), 49 | c(129,56, 0), 50 | c(129,56, 0), 51 | c(131,57, 0), 52 | c(132,58, 0), 53 | c(134,59, 0), 54 | c(134,59, 0), 55 | c(136,61, 0), 56 | c(137,62, 0), 57 | c(137,62, 0), 58 | c(139,63, 0), 59 | c(139,63, 0), 60 | c(140,65, 0), 61 | c(142,66, 0), 62 | c(142,66, 0), 63 | c(143,67, 0), 64 | c(143,67, 0), 65 | c(145,68, 0), 66 | c(145,68, 0), 67 | c(146,70, 0), 68 | c(146,70, 0), 69 | c(148,71, 0), 70 | c(148,71, 0), 71 | c(149,72, 0), 72 | c(149,72, 0), 73 | c(151,73, 0), 74 | c(151,73, 0), 75 | c(153,75, 0), 76 | c(153,75, 0), 77 | c(154,76, 0), 78 | c(154,76, 0), 79 | c(154,76, 0), 80 | c(156,77, 0), 81 | c(156,77, 0), 82 | c(157,79, 0), 83 | c(157,79, 0), 84 | c(159,80, 0), 85 | c(159,80, 0), 86 | c(159,80, 0), 87 | c(160,81, 0), 88 | c(160,81, 0), 89 | c(162,82, 0), 90 | c(162,82, 0), 91 | c(163,84, 0), 92 | c(163,84, 0), 93 | c(165,85, 0), 94 | c(165,85, 0), 95 | c(166,86, 0), 96 | c(166,86, 0), 97 | c(166,86, 0), 98 | c(168,87, 0), 99 | c(168,87, 0), 100 | c(170,89, 0), 101 | c(170,89, 0), 102 | c(171,90, 0), 103 | c(171,90, 0), 104 | c(173,91, 0), 105 | c(173,91, 0), 106 | c(174,93, 0), 107 | c(174,93, 0), 108 | c(176,94, 0), 109 | c(176,94, 0), 110 | c(177,95, 0), 111 | c(177,95, 0), 112 | c(179,96, 0), 113 | c(179,96, 0), 114 | c(180,98, 0), 115 | c(182,99, 0), 116 | c(182,99, 0), 117 | c(183,100, 0), 118 | c(183,100, 0), 119 | c(185,102, 0), 120 | c(185,102, 0), 121 | c(187,103, 0), 122 | c(187,103, 0), 123 | c(188,104, 0), 124 | c(188,104, 0), 125 | c(190,105, 0), 126 | c(191,107, 0), 127 | c(191,107, 0), 128 | c(193,108, 0), 129 | c(193,108, 0), 130 | c(194,109, 0), 131 | c(196,110, 0), 132 | c(196,110, 0), 133 | c(197,112, 0), 134 | c(197,112, 0), 135 | c(199,113, 0), 136 | c(200,114, 0), 137 | c(200,114, 0), 138 | c(202,116, 0), 139 | c(202,116, 0), 140 | c(204,117, 0), 141 | c(205,118, 0), 142 | c(205,118, 0), 143 | c(207,119, 0), 144 | c(208,121, 0), 145 | c(208,121, 0), 146 | c(210,122, 0), 147 | c(211,123, 0), 148 | c(211,123, 0), 149 | c(213,124, 0), 150 | c(214,126, 0), 151 | c(214,126, 0), 152 | c(216,127, 0), 153 | c(217,128, 0), 154 | c(217,128, 0), 155 | c(219,130, 0), 156 | c(221,131, 0), 157 | c(221,131, 0), 158 | c(222,132, 0), 159 | c(224,133, 0), 160 | c(224,133, 0), 161 | c(225,135, 0), 162 | c(227,136, 0), 163 | c(227,136, 0), 164 | c(228,137, 0), 165 | c(230,138, 0), 166 | c(230,138, 0), 167 | c(231,140, 0), 168 | c(233,141, 0), 169 | c(233,141, 0), 170 | c(234,142, 0), 171 | c(236,144, 0), 172 | c(236,144, 0), 173 | c(238,145, 0), 174 | c(239,146, 0), 175 | c(241,147, 0), 176 | c(241,147, 0), 177 | c(242,149, 0), 178 | c(244,150, 0), 179 | c(244,150, 0), 180 | c(245,151, 0), 181 | c(247,153, 0), 182 | c(247,153, 0), 183 | c(248,154, 0), 184 | c(250,155, 0), 185 | c(251,156, 0), 186 | c(251,156, 0), 187 | c(253,158, 0), 188 | c(255,159, 0), 189 | c(255,159, 0), 190 | c(255,160, 0), 191 | c(255,161, 0), 192 | c(255,163, 0), 193 | c(255,163, 0), 194 | c(255,164, 0), 195 | c(255,165, 0), 196 | c(255,167, 0), 197 | c(255,167, 0), 198 | c(255,168, 0), 199 | c(255,169, 0), 200 | c(255,169, 0), 201 | c(255,170, 0), 202 | c(255,172, 0), 203 | c(255,173, 0), 204 | c(255,173, 0), 205 | c(255,174, 0), 206 | c(255,175, 0), 207 | c(255,177, 0), 208 | c(255,178, 0), 209 | c(255,179, 0), 210 | c(255,181, 0), 211 | c(255,181, 0), 212 | c(255,182, 0), 213 | c(255,183, 0), 214 | c(255,184, 0), 215 | c(255,187, 7), 216 | c(255,188,10), 217 | c(255,189,14), 218 | c(255,191,18), 219 | c(255,192,21), 220 | c(255,193,25), 221 | c(255,195,29), 222 | c(255,197,36), 223 | c(255,198,40), 224 | c(255,200,43), 225 | c(255,202,51), 226 | c(255,204,54), 227 | c(255,206,61), 228 | c(255,207,65), 229 | c(255,210,72), 230 | c(255,211,76), 231 | c(255,214,83), 232 | c(255,216,91), 233 | c(255,219,98), 234 | c(255,221,105), 235 | c(255,223,109), 236 | c(255,225,116), 237 | c(255,228,123), 238 | c(255,232,134), 239 | c(255,234,142), 240 | c(255,237,149), 241 | c(255,239,156), 242 | c(255,240,160), 243 | c(255,243,167), 244 | c(255,246,174), 245 | c(255,248,182), 246 | c(255,249,185), 247 | c(255,252,193), 248 | c(255,253,196), 249 | c(255,255,204), 250 | c(255,255,207), 251 | c(255,255,211), 252 | c(255,255,218), 253 | c(255,255,222), 254 | c(255,255,225), 255 | c(255,255,229), 256 | c(255,255,233), 257 | c(255,255,236), 258 | c(255,255,240), 259 | c(255,255,244), 260 | c(255,255,247), 261 | c(255,255,255))[ round(seq(beg,end,length = n)), ] 262 | 263 | rgb(M[,1]/255, 264 | M[,2]/255, 265 | M[,3]/255) 266 | 267 | } 268 | -------------------------------------------------------------------------------- /R/BTC.R: -------------------------------------------------------------------------------- 1 | BTC <- function(n, beg = 1, end = 256) 2 | { 3 | if(beg < 1 || end < 1 || beg > 256 || end > 256) 4 | stop("`beg' and `end' must be numbers in the interval [1,256]") 5 | 6 | M <- rbind(c(0,0,0), 7 | c(0,0,40), 8 | c(0,4,56), 9 | c(0,9,61), 10 | c(0,12,64), 11 | c(0,14,66), 12 | c(0,17,69), 13 | c(0,20,73), 14 | c(0,22,74), 15 | c(0,25,78), 16 | c(0,27,79), 17 | c(0,30,83), 18 | c(0,31,85), 19 | c(0,33,86), 20 | c(0,36,90), 21 | c(0,38,91), 22 | c(0,39,93), 23 | c(0,41,95), 24 | c(0,43,96), 25 | c(0,46,100), 26 | c(0,47,102), 27 | c(0,49,103), 28 | c(0,51,105), 29 | c(0,52,107), 30 | c(0,54,108), 31 | c(0,55,110), 32 | c(0,57,112), 33 | c(0,57,112), 34 | c(0,58,113), 35 | c(0,60,115), 36 | c(0,62,117), 37 | c(0,63,119), 38 | c(0,65,120), 39 | c(0,66,122), 40 | c(0,68,124), 41 | c(0,70,125), 42 | c(0,71,127), 43 | c(0,73,129), 44 | c(0,73,129), 45 | c(0,74,130), 46 | c(0,76,132), 47 | c(0,78,134), 48 | c(0,79,136), 49 | c(0,81,137), 50 | c(0,82,139), 51 | c(0,84,141), 52 | c(0,86,142), 53 | c(0,87,144), 54 | c(0,89,146), 55 | c(0,90,147), 56 | c(0,92,149), 57 | c(0,94,151), 58 | c(0,94,151), 59 | c(0,95,153), 60 | c(0,97,154), 61 | c(0,98,156), 62 | c(0,100,158), 63 | c(0,102,159), 64 | c(0,103,161), 65 | c(0,105,163), 66 | c(0,106,164), 67 | c(0,108,166), 68 | c(0,109,168), 69 | c(0,111,170), 70 | c(0,113,171), 71 | c(0,114,173), 72 | c(0,116,175), 73 | c(0,117,176), 74 | c(0,119,178), 75 | c(0,121,180), 76 | c(0,121,180), 77 | c(0,122,181), 78 | c(0,124,183), 79 | c(0,125,185), 80 | c(0,127,187), 81 | c(0,129,188), 82 | c(0,130,190), 83 | c(0,132,192), 84 | c(0,133,193), 85 | c(0,135,195), 86 | c(0,137,197), 87 | c(0,138,198), 88 | c(0,140,200), 89 | c(0,141,202), 90 | c(0,143,204), 91 | c(0,143,204), 92 | c(0,145,205), 93 | c(0,146,207), 94 | c(0,148,209), 95 | c(0,149,210), 96 | c(0,151,212), 97 | c(0,153,214), 98 | c(0,154,215), 99 | c(0,156,217), 100 | c(0,157,219), 101 | c(0,159,221), 102 | c(0,160,222), 103 | c(0,160,222), 104 | c(0,162,224), 105 | c(0,164,226), 106 | c(0,165,227), 107 | c(0,167,229), 108 | c(0,168,231), 109 | c(0,170,232), 110 | c(0,172,234), 111 | c(0,173,236), 112 | c(0,175,238), 113 | c(0,175,238), 114 | c(0,176,239), 115 | c(0,178,241), 116 | c(0,180,243), 117 | c(0,181,244), 118 | c(0,183,246), 119 | c(2,184,248), 120 | c(4,186,249), 121 | c(4,186,249), 122 | c(4,186,249), 123 | c(6,188,251), 124 | c(6,188,251), 125 | c(9,189,253), 126 | c(9,189,253), 127 | c( 11,191,255), 128 | c( 11,191,255), 129 | c( 13,192,255), 130 | c( 13,192,255), 131 | c( 13,192,255), 132 | c( 16,194,255), 133 | c( 18,196,255), 134 | c( 20,197,255), 135 | c( 20,197,255), 136 | c( 23,199,255), 137 | c( 25,200,255), 138 | c( 27,202,255), 139 | c( 30,204,255), 140 | c( 32,205,255), 141 | c( 34,207,255), 142 | c( 37,208,255), 143 | c( 37,208,255), 144 | c( 39,210,255), 145 | c( 41,211,255), 146 | c( 44,213,255), 147 | c( 46,215,255), 148 | c( 48,216,255), 149 | c( 51,218,255), 150 | c( 53,219,255), 151 | c( 53,219,255), 152 | c( 55,221,255), 153 | c( 57,223,255), 154 | c( 60,224,255), 155 | c( 62,226,255), 156 | c( 64,227,255), 157 | c( 67,229,255), 158 | c( 67,229,255), 159 | c( 69,231,255), 160 | c( 71,232,255), 161 | c( 74,234,255), 162 | c( 76,235,255), 163 | c( 78,237,255), 164 | c( 81,239,255), 165 | c( 81,239,255), 166 | c( 83,240,255), 167 | c( 85,242,255), 168 | c( 88,243,255), 169 | c( 90,245,255), 170 | c( 92,247,255), 171 | c( 95,248,255), 172 | c( 95,248,255), 173 | c( 97,250,255), 174 | c( 99,251,255), 175 | c(102,253,255), 176 | c(104,255,255), 177 | c(106,255,255), 178 | c(106,255,255), 179 | c(108,255,255), 180 | c(111,255,255), 181 | c(113,255,255), 182 | c(115,255,255), 183 | c(115,255,255), 184 | c(118,255,255), 185 | c(120,255,255), 186 | c(122,255,255), 187 | c(122,255,255), 188 | c(125,255,255), 189 | c(127,255,255), 190 | c(129,255,255), 191 | c(129,255,255), 192 | c(132,255,255), 193 | c(134,255,255), 194 | c(136,255,255), 195 | c(136,255,255), 196 | c(139,255,255), 197 | c(141,255,255), 198 | c(143,255,255), 199 | c(143,255,255), 200 | c(146,255,255), 201 | c(148,255,255), 202 | c(150,255,255), 203 | c(150,255,255), 204 | c(153,255,255), 205 | c(155,255,255), 206 | c(155,255,255), 207 | c(157,255,255), 208 | c(159,255,255), 209 | c(159,255,255), 210 | c(162,255,255), 211 | c(164,255,255), 212 | c(164,255,255), 213 | c(166,255,255), 214 | c(169,255,255), 215 | c(171,255,255), 216 | c(171,255,255), 217 | c(173,255,255), 218 | c(176,255,255), 219 | c(176,255,255), 220 | c(178,255,255), 221 | c(180,255,255), 222 | c(180,255,255), 223 | c(183,255,255), 224 | c(185,255,255), 225 | c(185,255,255), 226 | c(187,255,255), 227 | c(190,255,255), 228 | c(190,255,255), 229 | c(192,255,255), 230 | c(194,255,255), 231 | c(197,255,255), 232 | c(197,255,255), 233 | c(199,255,255), 234 | c(201,255,255), 235 | c(204,255,255), 236 | c(204,255,255), 237 | c(206,255,255), 238 | c(208,255,255), 239 | c(210,255,255), 240 | c(210,255,255), 241 | c(213,255,255), 242 | c(215,255,255), 243 | c(217,255,255), 244 | c(217,255,255), 245 | c(220,255,255), 246 | c(222,255,255), 247 | c(224,255,255), 248 | c(227,255,255), 249 | c(229,255,255), 250 | c(229,255,255), 251 | c(231,255,255), 252 | c(234,255,255), 253 | c(236,255,255), 254 | c(238,255,255), 255 | c(241,255,255), 256 | c(243,255,255), 257 | c(243,255,255), 258 | c(245,255,255), 259 | c(248,255,255), 260 | c(250,255,255), 261 | c(255,255,255))[ round(seq(beg,end, length = n)), ] 262 | 263 | rgb(M[,1]/255, 264 | M[,2]/255, 265 | M[,3]/255) 266 | } 267 | 268 | 269 | -------------------------------------------------------------------------------- /R/BTY.R: -------------------------------------------------------------------------------- 1 | BTY <- function(n, beg = 1, end = 256) 2 | { 3 | if(beg < 1 || end < 1 || beg > 256 || end > 256) 4 | stop("`beg' and `end' must be numbers in the interval [1,256]") 5 | 6 | M <- rbind(c(7,7,254), 7 | c(23,23,252), 8 | c(30,30,250), 9 | c(36,36,248), 10 | c(40,40,247), 11 | c(44,44,245), 12 | c(47,47,243), 13 | c(50,50,242), 14 | c(52,52,240), 15 | c(55,55,239), 16 | c(57,57,238), 17 | c(59,59,236), 18 | c(61,61,235), 19 | c(63,63,234), 20 | c(65,65,233), 21 | c(66,66,231), 22 | c(68,68,230), 23 | c(69,69,229), 24 | c(71,71,228), 25 | c(72,72,227), 26 | c(74,74,226), 27 | c(75,75,225), 28 | c(76,76,225), 29 | c(78,78,224), 30 | c(79,79,223), 31 | c(80,80,222), 32 | c(81,81,221), 33 | c(82,82,221), 34 | c(84,84,220), 35 | c(85,85,219), 36 | c(86,86,218), 37 | c(87,87,218), 38 | c(88,88,217), 39 | c(89,89,216), 40 | c(90,90,216), 41 | c(91,91,215), 42 | c(92,92,214), 43 | c(93,93,214), 44 | c(94,94,213), 45 | c(95,95,213), 46 | c(96,96,212), 47 | c(97,97,212), 48 | c(98,98,211), 49 | c(98,98,210), 50 | c(99,99,210), 51 | c(100,100,209), 52 | c(101,101,209), 53 | c(102,102,208), 54 | c(103,103,208), 55 | c(104,104,208), 56 | c(105,105,207), 57 | c(105,105,207), 58 | c(106,106,206), 59 | c(107,107,206), 60 | c(108,108,205), 61 | c(109,109,205), 62 | c(110,110,204), 63 | c(110,110,204), 64 | c(111,111,204), 65 | c(112,112,203), 66 | c(113,113,203), 67 | c(114,114,202), 68 | c(114,114,202), 69 | c(115,115,202), 70 | c(116,116,201), 71 | c(117,117,201), 72 | c(118,118,200), 73 | c(118,118,200), 74 | c(119,119,200), 75 | c(120,120,199), 76 | c(121,121,199), 77 | c(121,121,199), 78 | c(122,122,198), 79 | c(123,123,198), 80 | c(124,124,198), 81 | c(124,124,197), 82 | c(125,125,197), 83 | c(126,126,197), 84 | c(127,127,196), 85 | c(128,128,196), 86 | c(128,128,195), 87 | c(129,129,195), 88 | c(130,130,195), 89 | c(130,130,194), 90 | c(131,131,194), 91 | c(132,132,194), 92 | c(133,133,193), 93 | c(133,133,193), 94 | c(134,134,193), 95 | c(135,135,192), 96 | c(136,136,192), 97 | c(136,136,192), 98 | c(137,137,191), 99 | c(138,138,191), 100 | c(139,139,191), 101 | c(139,139,190), 102 | c(140,140,190), 103 | c(141,141,190), 104 | c(142,142,189), 105 | c(142,142,189), 106 | c(143,143,189), 107 | c(144,144,188), 108 | c(144,144,188), 109 | c(145,145,188), 110 | c(146,146,187), 111 | c(147,147,187), 112 | c(147,147,187), 113 | c(148,148,186), 114 | c(149,149,186), 115 | c(149,149,186), 116 | c(150,150,185), 117 | c(151,151,185), 118 | c(152,152,185), 119 | c(152,152,184), 120 | c(153,153,184), 121 | c(154,154,184), 122 | c(154,154,183), 123 | c(155,155,183), 124 | c(156,156,182), 125 | c(157,157,182), 126 | c(157,157,182), 127 | c(158,158,181), 128 | c(159,159,181), 129 | c(159,159,181), 130 | c(160,160,180), 131 | c(161,161,180), 132 | c(162,162,180), 133 | c(162,162,179), 134 | c(163,163,179), 135 | c(164,164,178), 136 | c(164,164,178), 137 | c(165,165,178), 138 | c(166,166,177), 139 | c(167,167,177), 140 | c(167,167,176), 141 | c(168,168,176), 142 | c(169,169,176), 143 | c(169,169,175), 144 | c(170,170,175), 145 | c(171,171,174), 146 | c(172,172,174), 147 | c(172,172,173), 148 | c(173,173,173), 149 | c(174,174,173), 150 | c(174,174,172), 151 | c(175,175,172), 152 | c(176,176,171), 153 | c(177,177,171), 154 | c(177,177,170), 155 | c(178,178,170), 156 | c(179,179,169), 157 | c(179,179,169), 158 | c(180,180,168), 159 | c(181,181,168), 160 | c(181,181,167), 161 | c(182,182,167), 162 | c(183,183,166), 163 | c(184,184,166), 164 | c(184,184,165), 165 | c(185,185,165), 166 | c(186,186,164), 167 | c(186,186,164), 168 | c(187,187,163), 169 | c(188,188,163), 170 | c(189,189,162), 171 | c(189,189,162), 172 | c(190,190,161), 173 | c(191,191,161), 174 | c(191,191,160), 175 | c(192,192,159), 176 | c(193,193,159), 177 | c(194,194,158), 178 | c(194,194,158), 179 | c(195,195,157), 180 | c(196,196,157), 181 | c(196,196,156), 182 | c(197,197,155), 183 | c(198,198,155), 184 | c(199,199,154), 185 | c(199,199,153), 186 | c(200,200,153), 187 | c(201,201,152), 188 | c(201,201,151), 189 | c(202,202,151), 190 | c(203,203,150), 191 | c(204,204,149), 192 | c(204,204,149), 193 | c(205,205,148), 194 | c(206,206,147), 195 | c(206,206,146), 196 | c(207,207,146), 197 | c(208,208,145), 198 | c(209,209,144), 199 | c(209,209,143), 200 | c(210,210,143), 201 | c(211,211,142), 202 | c(211,211,141), 203 | c(212,212,140), 204 | c(213,213,139), 205 | c(214,214,138), 206 | c(214,214,138), 207 | c(215,215,137), 208 | c(216,216,136), 209 | c(216,216,135), 210 | c(217,217,134), 211 | c(218,218,133), 212 | c(219,219,132), 213 | c(219,219,131), 214 | c(220,220,130), 215 | c(221,221,129), 216 | c(221,221,128), 217 | c(222,222,127), 218 | c(223,223,126), 219 | c(224,224,125), 220 | c(224,224,124), 221 | c(225,225,123), 222 | c(226,226,122), 223 | c(226,226,121), 224 | c(227,227,119), 225 | c(228,228,118), 226 | c(229,229,117), 227 | c(229,229,116), 228 | c(230,230,114), 229 | c(231,231,113), 230 | c(232,232,112), 231 | c(232,232,110), 232 | c(233,233,109), 233 | c(234,234,107), 234 | c(234,234,106), 235 | c(235,235,104), 236 | c(236,236,103), 237 | c(237,237,101), 238 | c(237,237,100), 239 | c(238,238,98), 240 | c(239,239,96), 241 | c(239,239,94), 242 | c(240,240,92), 243 | c(241,241,91), 244 | c(242,242,89), 245 | c(242,242,86), 246 | c(243,243,84), 247 | c(244,244,82), 248 | c(245,245,80), 249 | c(245,245,77), 250 | c(246,246,74), 251 | c(247,247,72), 252 | c(247,247,69), 253 | c(248,248,65), 254 | c(249,249,62), 255 | c(250,250,58), 256 | c(250,250,54), 257 | c(251,251,49), 258 | c(252,252,44), 259 | c(253,253,37), 260 | c(253,253,28), 261 | c(254,254,13))[ round(seq(beg,end, length = n)), ] 262 | 263 | rgb(M[,1]/255, 264 | M[,2]/255, 265 | M[,3]/255) 266 | } 267 | -------------------------------------------------------------------------------- /R/MAG.R: -------------------------------------------------------------------------------- 1 | magent <- function(n, beg = 1, end = 256) 2 | { 3 | if(beg < 1 || end < 1 || beg > 256 || end > 256) 4 | stop("`beg' and `end' must be numbers in the interval [1,256]") 5 | 6 | M <- rbind(c(0, 0, 0), 7 | c( 40, 0, 0), 8 | c( 56, 0, 4), 9 | c( 61, 0, 9), 10 | c( 64, 0, 12), 11 | c( 66, 0, 14), 12 | c( 69, 0, 17), 13 | c( 73, 0, 20), 14 | c( 74, 0, 22), 15 | c( 78, 0, 25), 16 | c( 79, 0, 27), 17 | c( 83, 0, 30), 18 | c( 85, 0, 31), 19 | c( 86, 0, 33), 20 | c( 90, 0, 36), 21 | c( 91, 0, 38), 22 | c( 93, 0, 39), 23 | c( 95, 0, 41), 24 | c( 96, 0, 43), 25 | c(100, 0, 46), 26 | c(102, 0, 47), 27 | c(103, 0, 49), 28 | c(105, 0, 51), 29 | c(107, 0, 52), 30 | c(108, 0, 54), 31 | c(110, 0, 55), 32 | c(112, 0, 57), 33 | c(112, 0, 57), 34 | c(113, 0, 58), 35 | c(115, 0, 60), 36 | c(117, 0, 62), 37 | c(119, 0, 63), 38 | c(120, 0, 65), 39 | c(122, 0, 66), 40 | c(124, 0, 68), 41 | c(125, 0, 70), 42 | c(127, 0, 71), 43 | c(129, 0, 73), 44 | c(129, 0, 73), 45 | c(130, 0, 74), 46 | c(132, 0, 76), 47 | c(134, 0, 78), 48 | c(136, 0, 79), 49 | c(137, 0, 81), 50 | c(139, 0, 82), 51 | c(141, 0, 84), 52 | c(142, 0, 86), 53 | c(144, 0, 87), 54 | c(146, 0, 89), 55 | c(147, 0, 90), 56 | c(149, 0, 92), 57 | c(151, 0, 94), 58 | c(151, 0, 94), 59 | c(153, 0, 95), 60 | c(154, 0, 97), 61 | c(156, 0, 98), 62 | c(158, 0,100), 63 | c(159, 0,102), 64 | c(161, 0,103), 65 | c(163, 0,105), 66 | c(164, 0,106), 67 | c(166, 0,108), 68 | c(168, 0,109), 69 | c(170, 0,111), 70 | c(171, 0,113), 71 | c(173, 0,114), 72 | c(175, 0,116), 73 | c(176, 0,117), 74 | c(178, 0,119), 75 | c(180, 0,121), 76 | c(180, 0,121), 77 | c(181, 0,122), 78 | c(183, 0,124), 79 | c(185, 0,125), 80 | c(187, 0,127), 81 | c(188, 0,129), 82 | c(190, 0,130), 83 | c(192, 0,132), 84 | c(193, 0,133), 85 | c(195, 0,135), 86 | c(197, 0,137), 87 | c(198, 0,138), 88 | c(200, 0,140), 89 | c(202, 0,141), 90 | c(204, 0,143), 91 | c(204, 0,143), 92 | c(205, 0,145), 93 | c(207, 0,146), 94 | c(209, 0,148), 95 | c(210, 0,149), 96 | c(212, 0,151), 97 | c(214, 0,153), 98 | c(215, 0,154), 99 | c(217, 0,156), 100 | c(219, 0,157), 101 | c(221, 0,159), 102 | c(222, 0,160), 103 | c(222, 0,160), 104 | c(224, 0,162), 105 | c(226, 0,164), 106 | c(227, 0,165), 107 | c(229, 0,167), 108 | c(231, 0,168), 109 | c(232, 0,170), 110 | c(234, 0,172), 111 | c(236, 0,173), 112 | c(238, 0,175), 113 | c(238, 0,175), 114 | c(239, 0,176), 115 | c(241, 0,178), 116 | c(243, 0,180), 117 | c(244, 0,181), 118 | c(246, 0,183), 119 | c(248, 2,184), 120 | c(249, 4,186), 121 | c(249, 4,186), 122 | c(249, 4,186), 123 | c(251, 6,188), 124 | c(251, 6,188), 125 | c(253, 9,189), 126 | c(253, 9,189), 127 | c(255, 11,191), 128 | c(255, 11,191), 129 | c(255, 13,192), 130 | c(255, 13,192), 131 | c(255, 13,192), 132 | c(255, 16,194), 133 | c(255, 18,196), 134 | c(255, 20,197), 135 | c(255, 20,197), 136 | c(255, 23,199), 137 | c(255, 25,200), 138 | c(255, 27,202), 139 | c(255, 30,204), 140 | c(255, 32,205), 141 | c(255, 34,207), 142 | c(255, 37,208), 143 | c(255, 37,208), 144 | c(255, 39,210), 145 | c(255, 41,211), 146 | c(255, 44,213), 147 | c(255, 46,215), 148 | c(255, 48,216), 149 | c(255, 51,218), 150 | c(255, 53,219), 151 | c(255, 53,219), 152 | c(255, 55,221), 153 | c(255, 57,223), 154 | c(255, 60,224), 155 | c(255, 62,226), 156 | c(255, 64,227), 157 | c(255, 67,229), 158 | c(255, 67,229), 159 | c(255, 69,231), 160 | c(255, 71,232), 161 | c(255, 74,234), 162 | c(255, 76,235), 163 | c(255, 78,237), 164 | c(255, 81,239), 165 | c(255, 81,239), 166 | c(255, 83,240), 167 | c(255, 85,242), 168 | c(255, 88,243), 169 | c(255, 90,245), 170 | c(255, 92,247), 171 | c(255, 95,248), 172 | c(255, 95,248), 173 | c(255, 97,250), 174 | c(255, 99,251), 175 | c(255,102,253), 176 | c(255,104,255), 177 | c(255,106,255), 178 | c(255,106,255), 179 | c(255,108,255), 180 | c(255,111,255), 181 | c(255,113,255), 182 | c(255,115,255), 183 | c(255,115,255), 184 | c(255,118,255), 185 | c(255,120,255), 186 | c(255,122,255), 187 | c(255,122,255), 188 | c(255,125,255), 189 | c(255,127,255), 190 | c(255,129,255), 191 | c(255,129,255), 192 | c(255,132,255), 193 | c(255,134,255), 194 | c(255,136,255), 195 | c(255,136,255), 196 | c(255,139,255), 197 | c(255,141,255), 198 | c(255,143,255), 199 | c(255,143,255), 200 | c(255,146,255), 201 | c(255,148,255), 202 | c(255,150,255), 203 | c(255,150,255), 204 | c(255,153,255), 205 | c(255,155,255), 206 | c(255,155,255), 207 | c(255,157,255), 208 | c(255,159,255), 209 | c(255,159,255), 210 | c(255,162,255), 211 | c(255,164,255), 212 | c(255,164,255), 213 | c(255,166,255), 214 | c(255,169,255), 215 | c(255,171,255), 216 | c(255,171,255), 217 | c(255,173,255), 218 | c(255,176,255), 219 | c(255,176,255), 220 | c(255,178,255), 221 | c(255,180,255), 222 | c(255,180,255), 223 | c(255,183,255), 224 | c(255,185,255), 225 | c(255,185,255), 226 | c(255,187,255), 227 | c(255,190,255), 228 | c(255,190,255), 229 | c(255,192,255), 230 | c(255,194,255), 231 | c(255,197,255), 232 | c(255,197,255), 233 | c(255,199,255), 234 | c(255,201,255), 235 | c(255,204,255), 236 | c(255,204,255), 237 | c(255,206,255), 238 | c(255,208,255), 239 | c(255,210,255), 240 | c(255,210,255), 241 | c(255,213,255), 242 | c(255,215,255), 243 | c(255,217,255), 244 | c(255,217,255), 245 | c(255,220,255), 246 | c(255,222,255), 247 | c(255,224,255), 248 | c(255,227,255), 249 | c(255,229,255), 250 | c(255,229,255), 251 | c(255,231,255), 252 | c(255,234,255), 253 | c(255,236,255), 254 | c(255,238,255), 255 | c(255,241,255), 256 | c(255,243,255), 257 | c(255,243,255), 258 | c(255,245,255), 259 | c(255,248,255), 260 | c(255,250,255), 261 | c(255,255,255)) [ round(seq(beg,end,length = n)), ] 262 | 263 | rgb(M[,1]/255, 264 | M[,2]/255, 265 | M[,3]/255) 266 | } 267 | -------------------------------------------------------------------------------- /R/hexPlotMA.R: -------------------------------------------------------------------------------- 1 | plotMAhex <- function (MA, array = 1, xlab = "A", ylab = "M", 2 | main = colnames(MA)[array], 3 | xlim = NULL, ylim = NULL, status = NULL, 4 | values, pch, col, cex, nbin=40, 5 | zero.weights = FALSE, 6 | style = "colorscale", legend = 1.2, lcex = 1, 7 | minarea = 0.04, maxarea = 0.8, mincnt = 2, 8 | maxcnt = NULL, trans = NULL, inv = NULL, 9 | colorcut = NULL, 10 | border = NULL, density = NULL, pen = NULL, 11 | colramp = function(n){ LinGray(n,beg = 90,end = 15) }, 12 | newpage = TRUE, type = c("p", "l", "n"), 13 | xaxt = c("s", "n"), yaxt = c("s", "n"), 14 | verbose = getOption("verbose")) 15 | { 16 | if (!requireNamespace("marray", quietly = TRUE)) 17 | stop("cannot process objects without package marray") 18 | if (!requireNamespace("limma", quietly = TRUE)) 19 | stop("cannot process objects without package limma") 20 | if(is.null(main))main <- "" 21 | switch(class(MA),marrayRaw={ 22 | x <- marray::maA(MA[,array]) 23 | y <- marray::maM(MA[,array]) 24 | w <- marray::maW(MA[,array]) 25 | },RGList = { 26 | MA <- limma::MA.RG(MA[, array]) 27 | array <- 1 28 | x <- MA$A 29 | y <- MA$M 30 | w <- MA$w 31 | }, MAList = { 32 | x <- as.matrix(MA$A)[, array] 33 | y <- as.matrix(MA$M)[, array] 34 | if (is.null(MA$weights)) 35 | w <- NULL 36 | else 37 | w <- as.matrix(MA$weights)[, array] 38 | }, list = { 39 | if (is.null(MA$A) || is.null(MA$M)) 40 | stop("No data to plot") 41 | x <- as.matrix(MA$A)[, array] 42 | y <- as.matrix(MA$M)[, array] 43 | if (is.null(MA$weights)) 44 | w <- NULL 45 | else 46 | w <- as.matrix(MA$weights)[, array] 47 | }, MArrayLM = { 48 | x <- MA$Amean 49 | y <- as.matrix(MA$coefficients)[, array] 50 | if (is.null(MA$weights)) 51 | w <- NULL 52 | else 53 | w <- as.matrix(MA$weights)[, array] 54 | }, matrix = { 55 | narrays <- ncol(MA) 56 | if (narrays < 2) 57 | stop("Need at least two arrays") 58 | if (narrays > 5) 59 | x <- apply(MA, 1, median, na.rm = TRUE) 60 | else 61 | x <- rowMeans(MA, na.rm = TRUE) 62 | y <- MA[, array] - x 63 | w <- NULL 64 | }, ExpressionSet = { 65 | if (!requireNamespace("Biobase", quietly = TRUE)) 66 | stop("cannot process ExpressionSet objects without package Biobase") 67 | narrays <- ncol(Biobase::exprs(MA)) 68 | if (narrays < 2) 69 | stop("Need at least two arrays") 70 | if (narrays > 5) 71 | x <- apply(Biobase::exprs(MA), 1, median, na.rm = TRUE) 72 | else 73 | x <- rowMeans(Biobase::exprs(MA), na.rm = TRUE) 74 | y <- Biobase::exprs(MA)[, array] - x 75 | w <- NULL 76 | if (missing(main)) 77 | main <- colnames(Biobase::exprs(MA))[array] 78 | }, AffyBatch = { 79 | if (!requireNamespace("Biobase", quietly = TRUE) || 80 | !requireNamespace("affy", quietly = TRUE)) 81 | stop("cannot process AffyBatch objects without package Biobase and affy") 82 | narrays <- ncol(Biobase::exprs(MA)) 83 | if (narrays < 2) 84 | stop("Need at least two arrays") 85 | if (narrays > 5) 86 | x <- apply(log2(Biobase::exprs(MA)), 1, median, na.rm = TRUE) 87 | else 88 | x <- rowMeans(log2(Biobase::exprs(MA)), na.rm = TRUE) 89 | y <- log2(Biobase::exprs(MA)[, array]) - x 90 | w <- NULL 91 | if (missing(main)) 92 | main <- colnames(Biobase::exprs(MA))[array] 93 | }, stop("MA is invalid object")) 94 | if (!is.null(w) && !zero.weights) { 95 | i <- is.na(w) | (w <= 0) 96 | y[i] <- NA 97 | } 98 | if (is.null(xlim)) 99 | xlim <- range(x, na.rm = TRUE) 100 | if (is.null(ylim)) 101 | ylim <- range(y, na.rm = TRUE) 102 | 103 | hbin <- hexbin(x,y,xbins=nbin,xbnds=xlim,ybnds=ylim, IDs = TRUE) 104 | hp <- plot(hbin, legend=legend, xlab = xlab, ylab = ylab, main = main, 105 | type='n', newpage=newpage) 106 | ## plot the hexagons 107 | pushHexport(hp$plot.vp) 108 | if(is.null(maxcnt)) maxcnt <- max(hbin@count) 109 | if(is.null(colorcut)) colorcut<-seq(0, 1, length = min(17, maxcnt)) 110 | grid.hexagons(hbin, style=style, minarea = minarea, maxarea = maxarea, 111 | mincnt = mincnt, maxcnt= maxcnt, trans = trans, 112 | colorcut = colorcut, density = density, border = border, 113 | pen = pen, colramp = colramp) 114 | if (is.null(status) || all(is.na(status))) { 115 | if (missing(pch)) 116 | pch <- 16 117 | if (missing(cex)) 118 | cex <- 0.3 119 | if (missing(col)) { 120 | clrs <- colramp(length(colorcut)-1) 121 | col <- clrs[1] 122 | } 123 | pp <- inout.hex(hbin,mincnt) 124 | grid.points(x[pp], y[pp], pch = pch[[1]], 125 | gp=gpar(cex = cex[1], col=col, fill=col)) 126 | } 127 | else { 128 | if (missing(values)) { 129 | if (is.null(attr(status, "values"))) 130 | values <- names(sort(table(status), decreasing = TRUE)) 131 | else 132 | values <- attr(status, "values") 133 | } 134 | sel <- !(status %in% values) 135 | nonhi <- any(sel) 136 | if (nonhi) grid.points(x[sel], y[sel], pch = 16, gp=gpar(cex = 0.3)) 137 | nvalues <- length(values) 138 | if (missing(pch)) { 139 | if (is.null(attr(status, "pch"))) 140 | pch <- rep(16, nvalues) 141 | else 142 | pch <- attr(status, "pch") 143 | } 144 | if (missing(cex)) { 145 | if (is.null(attr(status, "cex"))) { 146 | cex <- rep(1, nvalues) 147 | if (!nonhi) 148 | cex[1] <- 0.3 149 | } 150 | else 151 | cex <- attr(status, "cex") 152 | } 153 | if (missing(col)) { 154 | if (is.null(attr(status, "col"))) { 155 | col <- nonhi + 1:nvalues 156 | } 157 | else 158 | col <- attr(status, "col") 159 | } 160 | pch <- rep(pch, length = nvalues) 161 | col <- rep(col, length = nvalues) 162 | cex <- rep(cex, length = nvalues) 163 | for (i in 1:nvalues) { 164 | sel <- status == values[i] 165 | grid.points(x[sel], y[sel], pch = pch[[i]], gp=gpar(cex = cex[i], col = col[i])) 166 | } 167 | } 168 | popViewport() 169 | if (legend > 0) { 170 | inner <- getPlt(hp$plot.vp, ret.unit="inches", numeric=TRUE)[1] 171 | inner <- inner/hbin@xbins 172 | ysize <- getPlt(hp$plot.vp, ret.unit="inches", numeric=TRUE)[2] 173 | pushViewport(hp$legend.vp) 174 | grid.hexlegend(legend, ysize=ysize, lcex = lcex, inner = inner, 175 | style= style, minarea= minarea, maxarea= maxarea, 176 | mincnt= mincnt, maxcnt= maxcnt, 177 | trans=trans, inv=inv, 178 | colorcut = colorcut, 179 | density = density, border = border, pen = pen, 180 | colramp = colramp) 181 | 182 | #if (is.list(pch)) 183 | # legend(x = xlim[1], y = ylim[2], legend = values, 184 | # fill = col, col = col, cex = 0.9) 185 | #else legend(x = xlim[1], y = ylim[2], legend = values, 186 | # pch = pch, , col = col, cex = 0.9) 187 | popViewport() 188 | } 189 | invisible(list(hbin = hbin, plot.vp = hp$plot.vp, legend.vp = hp$legend.vp)) 190 | } 191 | 192 | hexMA.loess <- function(pMA, span = .4, col = 'red', n = 200, ...) 193 | { 194 | fit <- hexVP.loess(pMA$hbin, pMA$plot.vp, span = span, col = col, n = n, ...) 195 | invisible(fit) 196 | } 197 | -------------------------------------------------------------------------------- /man/grid.hexagons.Rd: -------------------------------------------------------------------------------- 1 | \name{grid.hexagons} 2 | \alias{grid.hexagons} 3 | \title{Add Hexagon Cells to Plot} 4 | \description{ 5 | Plots cells in an hexbin object. The function distinquishes among 6 | counts using 5 different styles. This function is the hexagon 7 | plotting engine from the \code{plot} method for \code{\link{hexbin}} 8 | objects. 9 | } 10 | \usage{ 11 | grid.hexagons(dat, style = c("colorscale", "centroids", "lattice", 12 | "nested.lattice", "nested.centroids", "constant.col"), 13 | use.count=TRUE, cell.at=NULL, 14 | minarea = 0.05, maxarea = 0.8, check.erosion = TRUE, 15 | mincnt = 1, maxcnt = max(dat@count), trans = NULL, 16 | colorcut = seq(0, 1, length = 17), 17 | density = NULL, border = NULL, pen = NULL, 18 | colramp = function(n){ LinGray(n,beg = 90, end = 15) }, 19 | def.unit= "native", 20 | verbose = getOption("verbose")) 21 | } 22 | \arguments{ 23 | \item{dat}{an object of class \code{hexbin}, see \code{\link{hexbin}}.} 24 | \item{style}{character string specifying the type of plotting; must be (a 25 | unique abbrevation) of the values given in \sQuote{Usage} above.} 26 | \item{use.count}{logical specifying if counts should be used.} 27 | \item{cell.at}{numeric vector to be plotted instead of counts, must 28 | besame length as the number of cells.} 29 | \item{minarea}{numeric, the fraction of cell area for the lowest count.} 30 | \item{maxarea}{the fraction of the cell area for the largest count.} 31 | \item{check.erosion}{logical indicating only eroded points should be 32 | used for \code{"erodebin"} objects; simply passed to 33 | \code{\link{hcell2xy}}, see its documentation.} 34 | \item{mincnt}{numeric; cells with counts smaller than \code{mincnt} 35 | are not shown.} 36 | \item{maxcnt}{cells with counts larger than this are not shown.} 37 | \item{trans}{a transformation function (or \code{NULL}) for the counts, 38 | e.g., \code{\link{sqrt}}.} 39 | \item{colorcut}{a vector of values covering [0, 1] which determine 40 | hexagon color class boundaries or hexagon size boundaries -- for 41 | \code{style = "colorscale"} only.} 42 | \item{density}{\code{\link[grid]{grid.polygon}} argument for shading. 0 causes 43 | the polygon not to be filled. \emph{This is not implemented (for 44 | \code{\link[grid]{grid.polygon}}) yet}.} 45 | \item{border}{\code{\link[grid]{grid.polygon}()} argument. Draw the border for 46 | each hexagon.} 47 | \item{pen}{colors for \code{\link[grid]{grid.polygon}()}. Determines the color 48 | with which the polygon will be filled.} 49 | \item{colramp}{function of an integer argument \code{n} returning n 50 | colors. \code{n} is determined }%% how? FIXME 51 | \item{def.unit}{default \code{\link[grid]{unit}} to be used.}% FIXME 52 | \item{verbose}{logical indicating if some diagnostic output should happen.} 53 | } 54 | \section{SIDE EFFECTS}{Adds hexagons to the plot.} 55 | 56 | \details{ 57 | The six plotting styles have the following effect: 58 | \describe{ 59 | \item{\code{style="lattice"} or \code{"centroids"}:}{ 60 | 61 | Plots the hexagons in different sizes based on counts. The 62 | \code{"lattice"} version centers the hexagons at the cell centers 63 | whereas \code{"centroids"} moves the hexagon centers close to the 64 | center of mass for the cells. In all cases the hexagons will not 65 | plot outside the cell unless \code{maxarea > 1}. Counts are rescaled 66 | into the interval [0,1] and colorcuts determine the class 67 | boundaries for sizes and counts. The pen argument for this style 68 | should be a single color or a vector of colors of 69 | \code{length(bin@count)}.} 70 | 71 | \item{\code{style="colorscale"}:}{ 72 | Counts are rescaled into the interval [0,1] and colorcuts determines 73 | the class boundaries for the color classes. For this style, the 74 | function passed as \code{colramp} is used to define the n colors for 75 | the n+1 color cuts. The pen argument is ignored. 76 | %% S-plus: In motif color options try polygon: black 16 white 77 | See \code{\link{LinGray}} for the default \code{colramp} and 78 | alternative \dQuote{color ramp} functions. 79 | } 80 | \item{\code{style="constant.col"}:}{ 81 | This is an even simpler alternative to \code{"colorscale"}, 82 | using constant colors (determined \code{pen} optionally). 83 | } 84 | 85 | \item{\code{style="nested.lattice"} and \code{"nested.centroids"}:}{ 86 | Counts are partitioned into classes by power of 10. The encoding 87 | nests hexagon size within powers of 10 color contours. 88 | 89 | If the pen argument is used it should be a matrix of colors with 2 90 | columns and either \code{ceiling(log10(max(bin@count)))} or 91 | \code{length(bin@count)} rows. The default uses the \R color palatte 92 | so that pens numbers 2-11 determine colors for completely filled 93 | cell Pen 2 is the color for 1's, Pen 3 is the color for 10's, etc. 94 | Pens numbers 12-21 determine the color of the foreground hexagons. The 95 | hexagon size shows the relative count for the power of 10. Different 96 | color schemes give different effects including 3-D illusions 97 | %% S-plus : 98 | %% One motif color option for the first 4 powers is black \#BBB \#36F 99 | %% \#0E3 \#F206 \#FFF4 \#FFF 100 | %% 101 | %% A second option is for the first 5 power is black \#FFF \#08F \#192 102 | %% \#F11 \#FF04 \#000 \#999 \#5CF \#AFA \#FAAF \#000 103 | } 104 | } 105 | 106 | \emph{Hexagon size encoding \code{minarea} and \code{maxarea}} 107 | determine the area of the smallest and largest hexagons 108 | plotted. Both are expressed fractions of the bin cell size. Typical 109 | values might be .04 and 1. When both values are 1, all plotted 110 | hexagons are bin cell size, if \code{maxarea} is greater than 1 than 111 | hexagons will overlap. This is sometimes interesting with the lattice 112 | and centroid styles. 113 | 114 | \emph{Count scaling} 115 | 116 | \code{relcnt <- (trans(cnt)-trans(mincnt)) / (trans(maxcnt)-trans(mincnt))} 117 | \cr 118 | \code{area <- minarea + relcnt*maxarea} 119 | 120 | By default the transformation \code{trans()} is the identity 121 | function. The legend routine requires the transformation inverse 122 | for some options. 123 | 124 | \emph{Count windowing \code{mincnt} and \code{maxcnt}} 125 | Only routine only plots cells with cnts in [mincnts, maxcnts] 126 | } 127 | \references{ 128 | Carr, D. B. (1991) 129 | Looking at Large Data Sets Using Binned Data Plots, 130 | pp. 7--39 in \emph{Computing and Graphics in Statistics}; 131 | Eds. A. Buja and P. Tukey, Springer-Verlag, New York. 132 | } 133 | \author{ 134 | Dan Carr ; 135 | ported and extended by Nicholas Lewin-Koh \email{nikko@hailmail.net}. 136 | } 137 | \seealso{\code{\link{hexbin}}, \code{\link{smooth.hexbin}}, 138 | \code{\link{erode.hexbin}}, \code{\link{hcell2xy}},% \code{\link{hcell}}, 139 | \code{\link{gplot.hexbin}}, \code{\link{hboxplot}}, \code{\link{hdiffplot}}, 140 | \code{\link{grid.hexlegend}}% \code{\link{hmatplot}} 141 | } 142 | 143 | \examples{ 144 | set.seed(506) 145 | x <- rnorm(10000) 146 | y <- rnorm(10000) 147 | 148 | # bin the points 149 | bin <- hexbin(x,y) 150 | 151 | # Typical approach uses plot( ) which controls the plot shape : 152 | plot(bin, main = "Bivariate rnorm(10000)") 153 | 154 | ## but we can have more manual control: 155 | 156 | # A mixture distribution 157 | x <- c(rnorm(5000),rnorm(5000,4,1.5)) 158 | y <- c(rnorm(5000),rnorm(5000,2,3)) 159 | hb2 <- hexbin(x,y) 160 | 161 | # Show color control and overplotting of hexagons 162 | ## 1) setup coordinate system: 163 | P <- plot(hb2, type="n", main = "Bivariate mixture (10000)")# asp=1 164 | 165 | ## 2) add hexagons (in the proper viewport): 166 | pushHexport(P$plot.vp) 167 | grid.hexagons(hb2, style= "lattice", border = gray(.1), pen = gray(.6), 168 | minarea = .1, maxarea = 1.5) 169 | library("grid") 170 | popViewport() 171 | 172 | ## How to treat 'singletons' specially: 173 | P <- plot(hb2, type="n", main = "Bivariate mixture (10000)")# asp=1 174 | pushHexport(P$plot.vp) 175 | grid.hexagons(hb2, style= "nested.centroids", mincnt = 2)# not the single ones 176 | grid.hexagons(hb2, style= "centroids", maxcnt = 1, maxarea=0.04)# single points 177 | popViewport() 178 | 179 | 180 | %% FIXME --- this would mix grid- and traditional-graphics 181 | %% ----- would need grid-graphics for 'gpclib' -- aaargs... 182 | % # And if we had all the information... 183 | % if(require(gpclib)){ 184 | % h1 <- chull(x[1:5000], y[1:5000]) 185 | % h2 <- chull(x[5001:10000], y[5001:10000]) 186 | % h2 <- h2+5000 187 | % h1 <- as(cbind(x[1:5000],y [1:5000])[h1, ], "gpc.poly") 188 | % h2 <- as(cbind(x,y)[h2, ], "gpc.poly") 189 | % plot(hb2, type="n", main = "Bivariate mixture (10000)")# asp=1 190 | % 191 | % plot(h1,poly.args = list(col ="#CCEBC5"),add = TRUE) 192 | % plot(h2,poly.args = list(col ="#FBB4AE"),add = TRUE) 193 | % plot(intersect(h1, h2), poly.args = list(col = 2), add = TRUE) 194 | % grid.hexagons(hb2, style= "centroids", border = gray(.1), pen = gray(.6), 195 | % minarea = .1, maxarea = 1.5) 196 | % } 197 | 198 | } 199 | \keyword{aplot} 200 | -------------------------------------------------------------------------------- /R/RB.R: -------------------------------------------------------------------------------- 1 | plinrain <- function(n, beg = 1, end = 256) 2 | { 3 | if(beg < 1 || end < 1 || beg > 256 || end > 256) 4 | stop("`beg' and `end' must be numbers in the interval [1,256]") 5 | 6 | M <- rbind(c( 0, 0, 0), 7 | c( 45, 0, 36), 8 | c( 56, 0, 46), 9 | c( 60, 0, 49), 10 | c( 67, 0, 54), 11 | c( 70, 0, 59), 12 | c( 71, 0, 61), 13 | c( 75, 0, 68), 14 | c( 74, 0, 73), 15 | c( 74, 0, 77), 16 | c( 73, 0, 81), 17 | c( 71, 0, 87), 18 | c( 69, 1, 90), 19 | c( 68, 2, 94), 20 | c( 66, 3, 97), 21 | c( 63, 6,102), 22 | c( 61, 7,106), 23 | c( 58, 10,109), 24 | c( 56, 12,113), 25 | c( 53, 15,116), 26 | c( 48, 18,119), 27 | c( 47, 20,121), 28 | c( 44, 23,124), 29 | c( 41, 27,128), 30 | c( 40, 28,129), 31 | c( 37, 32,132), 32 | c( 34, 36,134), 33 | c( 29, 43,137), 34 | c( 25, 52,138), 35 | c( 24, 57,139), 36 | c( 24, 62,141), 37 | c( 24, 64,142), 38 | c( 23, 65,142), 39 | c( 23, 69,143), 40 | c( 23, 71,142), 41 | c( 23, 71,142), 42 | c( 23, 73,142), 43 | c( 23, 75,142), 44 | c( 23, 75,142), 45 | c( 23, 78,142), 46 | c( 23, 80,142), 47 | c( 23, 80,142), 48 | c( 23, 82,141), 49 | c( 23, 85,141), 50 | c( 23, 85,141), 51 | c( 23, 87,140), 52 | c( 23, 87,140), 53 | c( 24, 90,140), 54 | c( 24, 90,140), 55 | c( 24, 93,139), 56 | c( 24, 93,139), 57 | c( 24, 93,139), 58 | c( 24, 93,139), 59 | c( 24, 97,139), 60 | c( 24, 97,139), 61 | c( 25,101,138), 62 | c( 25,101,138), 63 | c( 25,104,137), 64 | c( 25,104,137), 65 | c( 25,104,137), 66 | c( 26,108,137), 67 | c( 26,108,137), 68 | c( 27,111,136), 69 | c( 27,111,136), 70 | c( 27,111,136), 71 | c( 27,115,135), 72 | c( 27,115,135), 73 | c( 28,118,134), 74 | c( 28,118,134), 75 | c( 29,122,133), 76 | c( 29,122,133), 77 | c( 29,122,133), 78 | c( 29,122,133), 79 | c( 29,125,132), 80 | c( 29,125,132), 81 | c( 30,128,131), 82 | c( 30,128,131), 83 | c( 31,131,130), 84 | c( 31,131,130), 85 | c( 31,131,130), 86 | c( 32,134,128), 87 | c( 32,134,128), 88 | c( 33,137,127), 89 | c( 33,137,127), 90 | c( 33,137,127), 91 | c( 34,140,125), 92 | c( 34,140,125), 93 | c( 35,142,123), 94 | c( 35,142,123), 95 | c( 36,145,121), 96 | c( 36,145,121), 97 | c( 36,145,121), 98 | c( 37,147,118), 99 | c( 37,147,118), 100 | c( 38,150,116), 101 | c( 38,150,116), 102 | c( 40,152,113), 103 | c( 40,152,113), 104 | c( 41,154,111), 105 | c( 41,154,111), 106 | c( 42,156,108), 107 | c( 42,156,108), 108 | c( 43,158,106), 109 | c( 43,158,106), 110 | c( 43,158,106), 111 | c( 45,160,104), 112 | c( 45,160,104), 113 | c( 46,162,101), 114 | c( 46,162,101), 115 | c( 48,164, 99), 116 | c( 48,164, 99), 117 | c( 50,166, 97), 118 | c( 50,166, 97), 119 | c( 51,168, 95), 120 | c( 53,170, 93), 121 | c( 53,170, 93), 122 | c( 53,170, 93), 123 | c( 55,172, 91), 124 | c( 55,172, 91), 125 | c( 57,174, 88), 126 | c( 57,174, 88), 127 | c( 59,175, 86), 128 | c( 62,177, 84), 129 | c( 64,178, 82), 130 | c( 64,178, 82), 131 | c( 67,180, 80), 132 | c( 67,180, 80), 133 | c( 69,181, 79), 134 | c( 72,183, 77), 135 | c( 72,183, 77), 136 | c( 72,183, 77), 137 | c( 75,184, 76), 138 | c( 77,186, 74), 139 | c( 80,187, 73), 140 | c( 83,189, 72), 141 | c( 87,190, 72), 142 | c( 91,191, 71), 143 | c( 95,192, 70), 144 | c( 99,193, 70), 145 | c(103,194, 70), 146 | c(107,195, 70), 147 | c(111,196, 70), 148 | c(111,196, 70), 149 | c(115,196, 70), 150 | c(119,197, 70), 151 | c(123,197, 70), 152 | c(130,198, 71), 153 | c(133,199, 71), 154 | c(137,199, 72), 155 | c(140,199, 72), 156 | c(143,199, 73), 157 | c(143,199, 73), 158 | c(147,199, 73), 159 | c(150,199, 74), 160 | c(153,199, 74), 161 | c(156,199, 75), 162 | c(160,200, 76), 163 | c(167,200, 78), 164 | c(170,200, 79), 165 | c(173,200, 79), 166 | c(173,200, 79), 167 | c(177,200, 80), 168 | c(180,200, 81), 169 | c(183,199, 82), 170 | c(186,199, 82), 171 | c(190,199, 83), 172 | c(196,199, 85), 173 | c(199,198, 85), 174 | c(199,198, 85), 175 | c(203,198, 86), 176 | c(206,197, 87), 177 | c(212,197, 89), 178 | c(215,196, 90), 179 | c(218,195, 91), 180 | c(224,194, 94), 181 | c(224,194, 94), 182 | c(230,193, 96), 183 | c(233,192, 98), 184 | c(236,190,100), 185 | c(238,189,104), 186 | c(240,188,106), 187 | c(240,188,106), 188 | c(242,187,110), 189 | c(244,185,114), 190 | c(245,184,116), 191 | c(247,183,120), 192 | c(248,182,123), 193 | c(248,182,123), 194 | c(250,181,125), 195 | c(251,180,128), 196 | c(252,180,130), 197 | c(253,180,133), 198 | c(253,180,133), 199 | c(254,180,134), 200 | c(254,179,138), 201 | c(255,179,142), 202 | c(255,179,145), 203 | c(255,179,145), 204 | c(255,179,152), 205 | c(255,180,161), 206 | c(255,180,164), 207 | c(255,180,167), 208 | c(255,180,167), 209 | c(255,181,169), 210 | c(255,181,170), 211 | c(255,182,173), 212 | c(255,183,176), 213 | c(255,183,176), 214 | c(255,184,179), 215 | c(255,185,179), 216 | c(255,185,182), 217 | c(255,186,182), 218 | c(255,186,182), 219 | c(255,187,185), 220 | c(255,188,185), 221 | c(255,189,188), 222 | c(255,189,188), 223 | c(255,190,188), 224 | c(255,191,191), 225 | c(255,192,191), 226 | c(255,194,194), 227 | c(255,194,194), 228 | c(255,197,197), 229 | c(255,198,198), 230 | c(255,200,200), 231 | c(255,201,201), 232 | c(255,201,201), 233 | c(255,202,202), 234 | c(255,203,203), 235 | c(255,205,205), 236 | c(255,206,206), 237 | c(255,206,206), 238 | c(255,208,208), 239 | c(255,209,209), 240 | c(255,211,211), 241 | c(255,215,215), 242 | c(255,216,216), 243 | c(255,216,216), 244 | c(255,218,218), 245 | c(255,219,219), 246 | c(255,221,221), 247 | c(255,223,223), 248 | c(255,226,226), 249 | c(255,228,228), 250 | c(255,230,230), 251 | c(255,230,230), 252 | c(255,232,232), 253 | c(255,235,235), 254 | c(255,237,237), 255 | c(255,240,240), 256 | c(255,243,243), 257 | c(255,246,246), 258 | c(255,249,249), 259 | c(255,251,251), 260 | c(255,253,253), 261 | c(255,255,255))[ round(seq(beg,end, length = n)), ] 262 | 263 | rgb(M[,1]/255, 264 | M[,2]/255, 265 | M[,3]/255) 266 | } 267 | -------------------------------------------------------------------------------- /man/hexbinplot.Rd: -------------------------------------------------------------------------------- 1 | \name{hexbinplot} 2 | \alias{hexbinplot} 3 | \alias{hexbinplot.formula} 4 | \alias{panel.hexbinplot} 5 | \alias{prepanel.hexbinplot} 6 | \alias{hexlegendGrob} 7 | \title{Trellis Hexbin Displays} 8 | \description{ 9 | 10 | Display of hexagonally binned data, as implemented in the 11 | \code{hexbin} packge, under the Trellis framework, with associated 12 | utilities. \code{hexbinplot} is the high level generic function, with 13 | the \code{"formula"} method doing the actual work. 14 | \code{prepanel.hexbinplot} and \code{panel.hexbinplot} are associated 15 | prepanel and panel functions. \code{hexlegendGrob} produces a 16 | suitable legend. 17 | 18 | } 19 | \usage{ 20 | 21 | hexbinplot(x, data, \dots) 22 | 23 | \method{hexbinplot}{formula}(x, data = NULL, 24 | prepanel = prepanel.hexbinplot, 25 | panel = panel.hexbinplot, 26 | groups = NULL, 27 | aspect = "xy", 28 | trans = NULL, 29 | inv = NULL, 30 | colorkey = TRUE, 31 | \dots, 32 | maxcnt, 33 | legend = NULL, 34 | legend.width = TRUE, 35 | subset) 36 | 37 | prepanel.hexbinplot(x, y, type = character(0), \dots) 38 | 39 | panel.hexbinplot(x, y, ..., groups = NULL) 40 | 41 | hexlegendGrob(legend = 1.2, 42 | inner = legend / 5, 43 | cex.labels = 1, 44 | cex.title = 1.2, 45 | style = "colorscale", 46 | minarea = 0.05, maxarea = 0.8, 47 | mincnt = 1, maxcnt, 48 | trans = NULL, inv = NULL, 49 | colorcut = seq(0, 1, length = 17), 50 | density = NULL, border = NULL, pen = NULL, 51 | colramp = function(n) { LinGray(n,beg = 90,end = 15) }, 52 | \dots, 53 | vp = NULL, 54 | draw = FALSE) 55 | 56 | 57 | } 58 | \arguments{ 59 | \item{x}{ For \code{hexbinplot}, the object on which method dispatch 60 | is carried out. 61 | 62 | For the \code{"formula"} methods, a formula describing the form of 63 | conditioning plot. Formulas that are valid for \code{xyplot} are 64 | acceptable. 65 | 66 | In \code{panel.hexbinplot}, the x variable. 67 | } 68 | \item{y}{ In \code{panel.hexbinplot}, the y variable. } 69 | 70 | \item{data}{For the \code{formula} method, a data frame containing 71 | values for any variables in the formula, as well as \code{groups} 72 | and \code{subset} if applicable (using \code{groups} currently 73 | causes an error with the default panel function). By default, the 74 | environment where the function was called from is used. } 75 | 76 | \item{minarea, maxarea, mincnt, maxcnt, trans, inv, colorcut, density, 77 | border, pen, colramp, style}{ see 78 | \code{\link[hexbin:gplot.hexbin]{gplot.hexbin}} } 79 | 80 | \item{prepanel, panel, aspect}{ See 81 | \code{\link[lattice]{xyplot}}. \code{aspect="fill"} is not 82 | allowed. The current default of \code{"xy"} may not always be the 83 | best choice, often \code{aspect=1} will be more reasonable. } 84 | 85 | \item{colorkey}{logical, whether a legend should be drawn. Currently 86 | a legend can be drawn only on the right. } 87 | 88 | \item{legend.width, legend}{ width of the legend in inches when 89 | \code{style} is \code{"nested.lattice"} or 90 | \code{"nested.centroids"}. The name \code{legend.width} is used to 91 | avoid conflict with the standard trellis argument \code{legend}. It 92 | is possible to specify additional legends using the \code{legend} or 93 | \code{key} arguments as long as they do not conflict with the 94 | hexbin legend (i.e., are not on the right). } 95 | 96 | \item{inner}{ Inner radius in inches of hexagons in the legend when 97 | \code{style} is \code{"nested.lattice"} or 98 | \code{"nested.centroids"}. } 99 | 100 | \item{cex.labels, cex.title}{ in the legend, multiplier for numeric 101 | labels and text annotation respectively } 102 | 103 | \item{type}{ character vector controlling additional augmentation of 104 | the display. A \code{"g"} in \code{type} adds a reference grid, 105 | \code{"r"} adds a regression line (y on x), \code{"smooth"} adds a 106 | loess smooth } 107 | \item{draw}{ logical, whether to draw the legend grob. Useful when 108 | \code{hexlegendGrob} is used separately } 109 | \item{vp}{ grid viewport to draw the legend in } 110 | 111 | \item{\dots}{ extra arguments, passed on as appropriate. Arguments to 112 | \code{\link[hexbin:gplot.hexbin]{gplot.hexbin}}, 113 | \code{\link[lattice]{xyplot}}, \code{panel.hexbinplot} and 114 | \code{hexlegendGrob} can be supplied to the high level 115 | \code{hexbinplot} call. 116 | 117 | \code{panel.hexbinplot} calls one of two (unexported) low-level 118 | functions depending on whether \code{groups} is supplied (although 119 | specifying \code{groups} currently leads to an error). Arguments of 120 | the appropriate function can be supplied; some important ones are 121 | 122 | \describe{ 123 | 124 | \item{\code{xbins}:}{ number of hexagons covering x values. The 125 | number of y-bins depends on this, the aspect ratio, and 126 | \code{xbnds} and \code{ybnds}} 127 | 128 | \item{\code{xbnds, ybnds}:}{ Numeric vector specifying range of 129 | values that should be covered by the binning. In a multi-panel 130 | display, it is not necessarily a good idea to use the same 131 | bounds (which along with \code{xbins} and the aspect ratio 132 | determine the size of the hexagons) for all panels. For 133 | example, when data is concentrated in small subregions of 134 | different panels, more detail will be shown by using smaller 135 | hexagons covering those regions. To control this, \code{xbnds} 136 | and \code{ybnds} can also be character strings \code{"panel"} or 137 | \code{"data"} (which are not very good names and may be changed 138 | in future). In the first case, the bounds are taken to be the 139 | limits of the panel, in the second case, the limits of the data 140 | (packet) in that panel. Note that all panels will have the same 141 | limits (enough to cover all the data) by default if 142 | \code{relation="free"} in the standard trellis argument 143 | \code{scales}, but not otherwise.} 144 | 145 | } 146 | 147 | } 148 | 149 | \item{groups}{ in \code{hexbinplot}, a grouping variable that is 150 | evaluated in \code{data}, and passed on to the panel function. } 151 | 152 | \item{subset}{ an expression that is evaluated in evaluated in 153 | \code{data} to produce a logical vector that is used to subset the 154 | data before being used in the plot. } 155 | 156 | } 157 | \details{ 158 | 159 | The panel function \code{panel.hexbinplot} creates a hexbin object 160 | from data supplied to it and plots it using 161 | \code{\link[hexbin:grid.hexagons]{grid.hexagons}}. To make panels 162 | comparable, all panels have the same \code{maxcnt} value, by default 163 | the maximum count over all panels. This default value can be 164 | calculated only if the aspect ratio is known, and so 165 | \code{aspect="fill"} is not allowed. The default choice of aspect 166 | ratio is different from the choice in \code{hexbin} (namely, 167 | \code{1}), which may sometimes give better results for multi-panel 168 | displays. \code{xbnds} and \code{ybnds} can be numeric range vectors 169 | as in \code{hexbin}, but they can also be character strings specifying 170 | whether all panels should have the same bins. If they are not, then 171 | bins in different panels could be of different sizes, in which case 172 | \code{style="lattice"} and \code{style="centroids"} should be 173 | interpreted carefully. 174 | 175 | 176 | The dimensions of the legend and the size of the hexagons therein are 177 | given in absolute units (inches) by \code{legend.width} and 178 | \code{inner} only when \code{style} is \code{"nested.lattice"} or 179 | \code{"nested.centroids"}. For other styles, the dimensions of the 180 | legend are determined relative to the plot. Specifically, the height 181 | of the legend is the same as the height of the plot (the panel and 182 | strip regions combined), and the width is the minimum required to fit 183 | the legend in the display. This is different in some ways from the 184 | \code{hexbin} implementation. In particular, the size of the hexagons 185 | in the legend are completely unrelated to the sizes in the panels, 186 | which is pretty much unavoidable because the sizes need not be the 187 | same across panels if \code{xbnds} or \code{ybnds} is \code{"data"}. 188 | The size of the hexagons encode information when \code{style} is 189 | \code{"lattice"} or \code{"centroids"}, consequently a warning is 190 | issued when a legend is drawn with wither of these styles. 191 | 192 | } 193 | 194 | \value{ 195 | \code{hexbinplot} produces an object of class \code{"trellis"}. The 196 | \code{update} method can be used to update components of the object and 197 | the \code{print} method (usually called by default) will plot it on an 198 | appropriate plotting device. \code{hexlegendGrob} produces a 199 | \code{"grob"} (grid object). 200 | 201 | } 202 | \author{ Deepayan Sarkar \email{deepayan.sarkar@r-project.org}} 203 | \seealso{ 204 | \code{\link{hexbin}}, \code{\link[lattice]{xyplot}} 205 | } 206 | 207 | \examples{ 208 | mixdata <- 209 | data.frame(x = c(rnorm(5000),rnorm(5000,4,1.5)), 210 | y = c(rnorm(5000),rnorm(5000,2,3)), 211 | a = gl(2, 5000)) 212 | hexbinplot(y ~ x, mixdata, aspect = 1, 213 | trans = sqrt, inv = function(x) x^2) 214 | hexbinplot(y ~ x | a, mixdata) 215 | hexbinplot(y ~ x | a, mixdata, style = "lattice", 216 | xbnds = "data", ybnds = "data") 217 | hexbinplot(y ~ x | a, mixdata, style = "nested.centroids") 218 | hexbinplot(y ~ x | a, mixdata, style = "nested.centroids", 219 | border = FALSE, type = c("g", "smooth")) 220 | } 221 | 222 | \keyword{dplot} 223 | -------------------------------------------------------------------------------- /R/hexViewport.R: -------------------------------------------------------------------------------- 1 | if ("simpleUnit" %in% class(unit(1, "mm"))) { 2 | setOldClass(c("unit", "unit_v2")) 3 | setOldClass(c("simpleUnit", "unit", "unit_v2")) 4 | } else { 5 | setOldClass("unit") 6 | } 7 | setOldClass("viewport") 8 | 9 | smartBnds <- function(hbin, eps=.05) 10 | { 11 | hxy <- hcell2xy(hbin) 12 | xr <- range(hxy$x) 13 | yr <- range(hxy$y) 14 | dx <- diff(xr) 15 | dy <- diff(yr) 16 | lambda <- function(a) pmax(log(a), 1) 17 | epsx <- c(-1,1)*(dx*eps/lambda(dx)) 18 | epsy <- c(-1,1)*(dy*eps/lambda(dy)) 19 | sx <- hbin@xbins/diff(hbin@xbnds) 20 | sy <- (hbin@xbins * hbin@shape)/diff(hbin@ybnds) 21 | inner <- 0.5 22 | outer <- 1/sqrt(3) 23 | dx <- inner/sx 24 | dy <- outer/sy 25 | #xb <- dx/(hbin@xbins+1) 26 | #yb <- dy/((1/sqrt(3))*(hbin@xbins+1)*hbin@shape) 27 | list(xr = xr+ c(-dx,dx)+ epsx, 28 | yr = yr+ c(-dy,dy)+ epsy) 29 | } 30 | 31 | rname <- function(n, chars = letters) 32 | { 33 | ## random name with n characters 34 | paste(sample(chars, size = n, replace = TRUE), collapse="") 35 | } 36 | 37 | setClass("hexVP", 38 | representation(hexVp.on = "viewport", hexVp.off = "viewport", 39 | mar = "unit", fig = "unit", plt = "unit", 40 | xscale = "numeric", yscale = "numeric",shape="numeric", 41 | hp.name="character") 42 | ) 43 | 44 | hexViewport <- 45 | function(x, offset = unit(0,"inches"), mar = NULL, 46 | xbnds = NULL, ybnds = NULL, newpage = FALSE, 47 | clip ="off", vp.name=NULL) 48 | { 49 | if(!is(x,"hexbin")) 50 | stop("first argument must be a hexbin object.") 51 | stopifnot(is.unit(offset)) 52 | 53 | hvp <- new("hexVP") 54 | if (newpage) 55 | grid.newpage() 56 | 57 | if(is.null(mar)) { 58 | mar <- unit(0.1 + c(5,4,4,2),"lines") 59 | } 60 | else { 61 | if(!is.unit(mar)) stop("'mar' must be specified in unit()s") 62 | if(length(mar) == 1) 63 | mar <- rep(mar, 4) 64 | else if(length(mar) != 4) 65 | stop("'mar' must have length 1 or 4") 66 | } 67 | ## in both cases 68 | mai <- as.numeric(convertUnit(mar, "inches")) 69 | vpin <- c(convertWidth (unit(1,"npc"),"inches"), convertHeight(unit(1,"npc"),"inches")) 70 | fig <- c(as.numeric(convertUnit(unit(vpin[1],"inches") - offset,"inches")), as.numeric(vpin[2])) 71 | pin <- c(fig[1]-mai[2]-mai[4], fig[2]-mai[1]-mai[3]) 72 | xsize <- pin[1] 73 | ysize <- pin[2] 74 | 75 | ## The point is to optimize the placement 76 | ## and plotting area of the plotting window with 77 | ## the constraint that the margins are preserved 78 | ## to within some epsilon. This is going to get even 79 | ## harder for cases where the complex layouts are 80 | ## being constructed. NL -- I think it is fixed now (NL --3/22/2005) 81 | 82 | ## Now find the maximum rectangle in fig that 83 | ## has the correct aspect ratio and does not spill over epsilon into 84 | ## the margins, i.e. ysize/xsize - aspect.ratio < eps and 85 | ## xsize < fig[1], ysize < fig[2] 86 | 87 | if(x@shape * xsize <= ysize) { 88 | ##center <- (ysize - x@shape * xsize)/2 89 | center <- (ysize - x@shape * xsize)/2 90 | mai[1] <- mai[1] + center 91 | mai[3] <- mai[3] + center 92 | ysize <- x@shape * xsize 93 | } else { 94 | center <- (xsize - ysize/x@shape)/2 95 | mai[2] <- mai[2] + center 96 | mai[4] <- mai[4] + center 97 | xsize <- ysize/x@shape 98 | } 99 | ##fig <- c(pin[1]+mai[2]+ mai[4],fig[2]) 100 | pin <- c(xsize,ysize) 101 | mar <- c(convertUnit(unit(mai[1],"inches"),"lines"), 102 | convertUnit(unit(mai[2],"inches"),"lines"), 103 | convertUnit(unit(mai[3],"inches"),"lines"), 104 | convertUnit(unit(mai[4],"inches"),"lines")) 105 | ##pin <- c(fig[1]-(mai[2] + mai[4]), 106 | ## fig[2]-(mai[1] + mai[3])) 107 | margins <- rep(as.numeric(mar), length.out = 4) 108 | wd <- convertUnit(unit(pin[1],"inches"),"npc") 109 | ## (unit(sum(margins[c(2, 4)]), "lines") + 110 | ## convertUnit(unit(legend,"inches"),"lines")) 111 | ## Oy, mi stupido! This is the problem, need to get the bounds right 112 | ## here. Fixed, do we need to guard against others stupidity and put some 113 | ## checks on xbnds and ybnds? (NL,4/1/2005) 114 | if(is.null(vp.name)) 115 | vp.name <- rname(5) 116 | xyb <- smartBnds(x) 117 | hvp@xscale <- xs <- if(is.null(xbnds)) xyb$xr else xbnds 118 | hvp@yscale <- ys <- if(is.null(ybnds)) xyb$yr else ybnds 119 | ht <- unit(1, "npc") - unit(sum(margins[c(1,3)]), "lines") 120 | hvp@hexVp.off <- 121 | viewport(x = unit(margins[2], "lines"), 122 | y = unit(margins[1], "lines"), 123 | width = wd, height = ht, xscale = xs, yscale = ys, 124 | just = c("left", "bottom"), default.units = "native", 125 | clip = "off", name = paste(vp.name,".off",sep="")) 126 | hvp@hexVp.on <- 127 | viewport(x = unit(margins[2], "lines"), 128 | y = unit(margins[1], "lines"), 129 | width = wd, height = ht, xscale = xs, yscale = ys, 130 | just = c("left", "bottom"), default.units = "native", 131 | clip = "on", name = paste(vp.name,".on",sep="")) 132 | hvp@mar <- unit(mar,"lines") 133 | hvp@fig <- convertUnit(unit(fig,"inches"),"npc") 134 | hvp@plt <- convertUnit(unit(pin,"inches"),"npc") 135 | hvp@shape <- x@shape 136 | ##hvp@leg <-convertUnit(offset,"npc") 137 | hvp 138 | } 139 | 140 | ## Potentially: 141 | ## setGeneric("grid:::pushViewport") 142 | ## setMethod("pushViewport", signature(x="hexVP"), 143 | ## function(hvp) { pushViewport(hvp@hexVp) }) 144 | 145 | pushHexport <- function(hvp, clip="off") 146 | { 147 | if(!is(hvp, "hexVP")) 148 | stop("1st argument must be 'hexVP' object") 149 | pushViewport(if(clip=="on") hvp@hexVp.on else hvp@hexVp.off) 150 | } 151 | 152 | ## maybe in the future 153 | ## setMethod("push",signature("hexVP"), pushHexport) 154 | 155 | setGeneric("getMargins", function(x, ret.unit = "npc", numeric = FALSE) 156 | standardGeneric("getMargins")) 157 | setMethod("getMargins", "hexVP", 158 | function(x, ret.unit = "npc", numeric = FALSE){ 159 | mar <- convertUnit(x@mar,ret.unit) 160 | if(numeric) as.numeric(mar) else mar 161 | }) 162 | 163 | setGeneric("getPlt", function(x, ret.unit = "npc", numeric = FALSE) 164 | standardGeneric("getPlt")) 165 | setMethod("getPlt", "hexVP", 166 | function(x, ret.unit = "npc", numeric = FALSE){ 167 | plt <- convertUnit(x@plt,ret.unit) 168 | if(numeric) as.numeric(plt) else plt 169 | }) 170 | 171 | setGeneric("getFig", function(x, ret.unit = "npc", numeric = FALSE) 172 | standardGeneric("getFig")) 173 | setMethod("getFig", "hexVP", 174 | function(x, ret.unit = "npc", numeric = FALSE){ 175 | fig <- convertUnit(x@fig,ret.unit) 176 | if(numeric) as.numeric(fig) else fig 177 | }) 178 | 179 | ## MM doesn't think it's ok to "pollute" the generic-space 180 | ## just for basic slot accessors : 181 | 182 | ## setGeneric("getXscale", function(x)standardGeneric("getXscale")) 183 | ## setMethod("getXscale", "hexVP", function(x){ x@xscale }) 184 | 185 | ## setGeneric("getYscale", function(x)standardGeneric("getYscale")) 186 | ## setMethod("getYscale", "hexVP", function(x){ x@yscale }) 187 | 188 | hexVP.abline <- function(hvp, a = NULL, b = NULL, h = numeric(0), 189 | v = numeric(0), col = 'black', 190 | lty = 1, lwd = 2, ...) 191 | { 192 | pushHexport(hvp, clip = 'on') 193 | col.line <- col 194 | if (!is.null(a)) { 195 | if (inherits(a, "lm")) { 196 | coeff <- coef(a) 197 | } 198 | else if (!is.null(tryCatch(coef(a), error = function(e) NULL))) 199 | coeff <- coef(a) 200 | else coeff <- c(a, b) 201 | if (length(coeff) == 1) 202 | coeff <- c(0, coeff) 203 | if (coeff[2] == 0) 204 | h <- c(h, coeff[1]) 205 | else if (!any(is.null(coeff))) { 206 | xx <- current.viewport()$xscale 207 | yy <- current.viewport()$yscale 208 | x <- numeric(0) 209 | y <- numeric(0) 210 | ll <- function(i, j, k, l) 211 | (yy[j] - coeff[1] - coeff[2] * xx[i]) * (yy[l] - coeff[1] - coeff[2] * xx[k]) 212 | if (ll(1, 1, 2, 1) <= 0) { 213 | y <- c(y, yy[1]) 214 | x <- c(x, (yy[1] - coeff[1])/coeff[2]) 215 | } 216 | if (ll(2, 1, 2, 2) <= 0) { 217 | x <- c(x, xx[2]) 218 | y <- c(y, coeff[1] + coeff[2] * xx[2]) 219 | } 220 | if (ll(2, 2, 1, 2) <= 0) { 221 | y <- c(y, yy[2]) 222 | x <- c(x, (yy[2] - coeff[1])/coeff[2]) 223 | } 224 | if (ll(1, 2, 1, 1) <= 0) { 225 | x <- c(x, xx[1]) 226 | y <- c(y, coeff[1] + coeff[2] * xx[1]) 227 | } 228 | if (length(x) > 0) 229 | grid.lines(x = x, y = y, default.units = "native", 230 | gp = gpar(col = col.line, lty = lty, lwd = lwd)) 231 | } 232 | } 233 | h <- as.numeric(h) 234 | v <- as.numeric(v) 235 | for (i in seq(along = h)) 236 | grid.lines(y = rep(h[i], 2), default.units = "native", 237 | gp = gpar(col = col.line, lty = lty, lwd = lwd)) 238 | for (i in seq(along = v)) 239 | grid.lines(x = rep(v[i], 2), default.units = "native", 240 | gp = gpar(col = col.line, lty = lty, lwd = lwd)) 241 | popViewport() 242 | } 243 | 244 | hexVP.loess <- function(hbin, hvp = NULL, span = 0.4, col = 'red', n = 200, ...) 245 | { 246 | fit <- loess(hbin@ycm ~ hbin@xcm, weights = hbin@count, span = span) 247 | if(!is.null(hvp)) { 248 | pushHexport(hvp, clip = 'on') 249 | # grid.lines(seq(0,16, length = n), 250 | # predict(fit,seq(0,16, length = n)), 251 | # gp = gpar(col = col), default.units = 'native') 252 | grid.lines(seq(hbin@xbnds[1], hbin@xbnds[2], length = n), 253 | predict(fit,seq(hbin@xbnds[1], hbin@xbnds[2], length = n)), 254 | gp = gpar(col = col, ...), default.units = 'native') 255 | popViewport() 256 | } 257 | invisible(fit) 258 | } 259 | --------------------------------------------------------------------------------