hexclass: Hexagonal binning with classes

View source: R/hextri.R

hexclassR Documentation

Hexagonal binning with classes

Description

Displays a hexagonally-binned scatterplot with coloured subtriangles to indicate number of observations in each class. With a single class, gives a base-graphics version of the ordinary hexbin plot.

Usage

hextri(x,...)
## Default S3 method:
hextri(x, y, class, colours, nbins = 10, border =
TRUE, diffuse=FALSE, style=c("alpha","size"), weights=NULL,
sorted=!diffuse,minfrac=0, ...)
## S3 method for class 'formula'
hextri(x, data=parent.frame(), class,colours,nbins=10,
border=TRUE, diffuse=FALSE, style=c("alpha","size"),weights=NULL, sorted=!diffuse,
 xlab=NULL, ylab=NULL,minfrac=0,...)

Arguments

x

Coordinates of points, or for the formula method, a model formula

y

coordinates

data

The class and weights arguments are looked up here, in addition to the formula itself

class

Factor giving class membership for points

colours

Vector of colors (number, name, or hashtag format) giving the colours for each class

nbins

Number of bins in the x-axis direction

border

Include a narrow transparent border around each triangle and hexagon

diffuse

Pass on rounding error to nearest not-yet-drawn hexes so that rare classes get represented

style

Represent data by hexagon size or by alpha-blending

weights

If not NULL, sampling weights or frequency weights. The hexbin plot will be based on the sum of weights in the hex

sorted

Should the triangles be sorted into a consistent order within each hex? More attractive but can cause 3-d artifacts and may be less accurate.

xlab,ylab

Specify to override the formula-based defaults

minfrac

Cells with radius smaller than this fraction of a whole cell will be drawn as points. Most useful with style="size"

...

Passed to the plot call that sets up the plot region

Details

Uses the Sainte-Lague method to apportion counts to triangles, ensuring the counts round to exactly six triangles per hexagon.

The binning/aspect ratio are adjusted to give regular hexagons on the figure region set by plot(x,y). If you reshape the window you will need to redraw the plot.

Value

A list with components x, y, and col suitable as input to polygon()

Author(s)

Thomas Lumley

See Also

sainte_lague

Examples

xx<-rnorm(1000)
yy<-rnorm(1000)
cc<-cut(xx*yy,c(-Inf,-.4,0,.4,Inf))

plot(xx,yy,col=(1:4)[cc])
hextri(xx,yy,cc,1:4,nbins=20,border=TRUE,style="size")

##formula method
data(NHANES, package="hexbin")
hextri(Weight~Age, class=Smoke, col=c("red","orange","green","grey40"),
   data=NHANES, style="size",nbins=20, main="Smoking")
legend("topright",fill=c("red","orange","green","grey40"),
   legend=c("Current","Past","Never","unknown"),bty="n")

## minimum sizes

data(NHANES, package="hexbin")
hextri(Weight~Age, class=Smoke, col=c("red","orange","green","grey40"),
   data=NHANES, style="size",nbins=20, main="Smoking",minfrac=.2)
legend("topright",fill=c("red","orange","green","grey40"),
   legend=c("Current","Past","Never","unknown"),bty="n")


## using the return value of hextri
rval<-hextri(xx,yy,cc,1:4,nbins=20,border=TRUE,style="alpha")
plot(y~x,data=rval,type="n")
with(rval, polygon(x,y,col=col,border=NA))

## diffusion
xx<-runif(10000)
yy<-runif(10000)
cc<-rep(1:3,c(4750,4750,500))
hextri(xx,yy,cc,2:4,border=TRUE,diffuse=FALSE,style="size")
hextri(xx,yy,cc,2:4,border=TRUE,diffuse=TRUE,sorted=TRUE, style="size")

hextri(xx,yy,cc,2:4,border=TRUE,diffuse=TRUE,style="size",weights=cc)


hextri documentation built on April 26, 2022, 5:06 p.m.