Plot A Hexagonal Lattice

DESCRIPTION:

Plots an object of class hexbin with legend.

USAGE:

plot.hexbin(bin, style="grayscale", minarea=0.04, maxarea=0.8, 
    mincount=1, maxcount=max(bin$count), cuts=min(16, maxcount), 
    col.regions=trellis.par.get("regions")$col, 
    at=pretty(bin$count, cuts), border=F, density=-1, legend=T, 
    legend.width=1, legend.lab="Counts", legend.cex=1, xlab="", 
    ylab="", ...) 
add.to.hexbin(plot.output, expr)

REQUIRED ARGUMENTS:

bin
an object of class "hexbin".

OPTIONAL ARGUMENTS:

style
a character string with one of 5 possible styles for plotting the hexagons. This must be one of "grayscale", "lattice", "centroids", "nested.lattice", or "nested.centroids". See the DETAILS section for their descriptions and relative merits. Default is "grayscale". Partial string matching is allowed.
minarea
fraction of cell area for the lowest count. Default is 0.04 or 4%.
maxarea
fraction of the cell area for the largest count. Default is 80%.
mincount
cells with fewer counts than mincount are ignored.
maxcount
cells with more counts than maxcount are ignored.
cuts
the number of levels to divide the full range of cell counts into. This is used for generating the at vector. How many different colors are used will depend, to an extent, on this parameter and vice versa. Default is 16. This is overridden by the length of at, if that argument is given.
col.regions
numeric vector integers. These correspond to the colors in the graphics device colormap that are to be used to color the hexagons. When style is either "centroids" or "lattice" only the first color is used, so this argument could then be only one number. Different color devices have more or less adequate colormaps. Using a device defined with a call to trellis.device will ensure that a colormap close to adequate is used. See the DETAILS section for ways to select other colormaps.
at
numeric vector with breaks to determine the mapping from the bin counts to the colormap. Since the colormap has minimal effect on the plot when style is either "centroids" or "lattice", this argument is not used for those styles. This parameter allows more flexibility in the coloring by allowing non-equally spaced color intervals. If at is omitted, it is computed as pretty(bin$count, cuts).
border
logical flag: should the border of the hexagon be drawn? (Same as the corresponding argument in the function polygon.)
density
density of shading lines in lines per inch. If density is zero, no shading will occur. If density is negative, the hexagons will be filled solidly using the device-dependent polygon filling algorithm. (Same as the corresponding argument in the function polygon.) If this argument is given, the user might want to set border=T or just the shading lines will appear in the plot.
legend
logical flag: should a legend be provided? This implies that space for the legend will be allocated in the plot region.
legend.width
numerical value. Width of the space to the right of the plot to be used for the legend in inches. Default is 1.
legend.lab
character string to designate the counts in the legend. Default is "Counts".
legend.cex
character expansion parameter for the text in the legend.
xlab
x-axis label.
ylab
y-axis label. Graphical parameters may also be supplied as arguments to this function (see ).

VALUE:

invisibly returns the values of the graphical parameters used for the plot. This list can then be used to add to the plot using the function add.to.hexbin or to further identify specific points using the function identify. See the EXAMPLES below.

SIDE EFFECTS:

Produces a plot of the hexagons as determined by the input object. This plot may include a legend or not.

DETAILS:

The five plotting styles are:

style="grayscale" A smoothly varying color mapping of the counts is determined from the values in cuts, at, and col.regions. The best use of this option requires that the plotting device is activated through a call to the S-PLUS function trellis.device. This ensures that an adequate color map is the default although other devices as well as customized colormaps can be provided by the user.

style="lattice" or "centroids" Plots the hexagons in sizes proportional to cell counts. The "lattice" option places the hexagons at the lattice centers. In some cases, the regularity of this structure may be visually overwhelming. In those cases, the user should use the "centroids" option which places the hexagons at their centers of mass. This results in the breaking of the regularity of the lattice structure thereby placing the focus on other properties of the data. In all cases the hexagons will not plot outside the cell unless maxarea > 1.

style="nested.lattice" and "nested.centroids" Two overlaying hexagons are plotted: a background hexagon with area equal to the full hexagon's and color proportional to the cell count in powers of 10 and a foreground hexagon with area proportional to log10(count) - floor(log10(count)). When style="nested.centroids" counts <10 are plotted and the centers of the plotted hexagons are placed at their centers of mass. The outside color encodes hexagon size within color contours representing powers of 10. Different color schemes give different effects including 3-D illusions.

A way to try different colormaps is by using the Options pull down menu in your graphics device driver and typing in your own color map in the Polygons widget. For smoothly varying colormaps, you may want to copy the Image colors into the Polygons slot.

PERCEPTUAL CONSIDERATIONS:

Visual response to relative symbol area is not linear and varies from person to person. The argument at can be manipulated to give non-linear color variations to aid the interpretation of a hexbin plot. See the EXAMPLES below for some ways to determine an ideal transformation for your colormap.

Plotting the symbols near the center of mass is not only more accurate, but it helps reduce the visual dominance of the lattice structure. Of course higher resolution binning reduces the possible distance between the center of mass for a bin and the bin center. When symbols nearly fill their bin, the plot appears to vibrate. This can be partially controlled by reducing maxarea or by reducing contrast.

The local background influences color interpretation. Having defined color breaks to focus attention on specific contours can help. See nested options.

WARNING:

If style is either "centroids" or "lattice" the legend may consist of overlapping hexagons given the varying areas of these. If that is the case then a smaller value for cuts will allow the legend to fit in the space provided for it.

REFERENCES:

Carr, D. B. (1991). Looking at large data sets using binned data plots. In Computing and Graphics in Statistics. A. Buja and P. Tukey, eds. Springer-Verlag, New York. pp. 7-39.

SEE ALSO:

, , , , , .

EXAMPLES:

# Simple binning 
x <- rnorm(10000) 
y <- rnorm(10000) 
mybin <- hexbin(x,y) 
# Basic plot 
plot(mybin,style="nested.lat") 
# Lower resolution binning and overplotting with counts 
mybin2 <- hexbin(x, y, xbins=20) 
plot.output <- plot(mybin2, style="lat", minarea=1, maxarea=1,  
                  density=0,border=T) 
xy <- cell2xy(mybin2) 
# add.to.hexbin temporarily restores the coordinate system
# of the hexbin plot and evaluates the expression in its
# second argument using that coordinate system
add.to.hexbin(plot.output,
    text(xy$x, xy$y, format(mybin2$count), adj=.5, cex=.3))
# Histogram equalization to determine amount of color 
# Use quantile function to break up the number of counts into 
#     more or less equal size groups  
at.q <- quantile(mybin$count,probs=seq(10,100,10)/100) 
plot(mybin,at=at.q) 
# More detail for low counts, use a log transform to determine 
# the breaks 
at.l <- range(log(mybin$count)) 
plot(mybin,at=exp(seq(at.l[1],at.l[2],length=10))) 
#add a regression line to a hexbin plot
Weight  <- fuel.frame$Weight
Mileage <- fuel.frame$Mileage
my.plot <-  plot(hexbin(Weight, Mileage))
add.to.hexbin(my.plot, abline(lsfit(Weight, Mileage)))