Title: | Fast Kernel Density Estimation with Hexagonal Grid |
---|---|
Description: | Kernel density estimation with hexagonal grid for bivariate data. Hexagonal grid has many beneficial properties like equidistant neighbours and less edge bias, making it better for spatial analyses than the more commonly used rectangular grid. Carr, D. B. et al. (1987) <doi:10.2307/2289444>. Diggle, P. J. (2010) <doi:10.1201/9781420072884>. Hill, B. (2017) <https://blog.bruce-hill.com/meandering-triangles>. Jones, M. C. (1993) <doi:10.1007/BF00147776>. |
Authors: | Quoc Hoang Nguyen [aut, cre] |
Maintainer: | Quoc Hoang Nguyen <[email protected]> |
License: | GPL |
Version: | 1.4.8 |
Built: | 2025-03-13 05:01:44 UTC |
Source: | https://github.com/chenlaboratory/hexdensity |
Adapted from grid.hexagons by hexbin with speedup specific for hexagonal tiling (avoid plotting the most abundance hexagons by setting its color as background).
grid.hexagontile( hexDensity, use.count = TRUE, cell.at = NULL, trans = NULL, colorcut = seq(0, 1, length = 1024), colramp = colorRampPalette(col.viridis), def.unit = "native" )
grid.hexagontile( hexDensity, use.count = TRUE, cell.at = NULL, trans = NULL, colorcut = seq(0, 1, length = 1024), colramp = colorRampPalette(col.viridis), def.unit = "native" )
hexDensity |
hexbin object returned by hexDensity. |
use.count |
logical specifying if counts from hexbin object should be used. |
cell.at |
numeric vector to be plotted instead of counts, must be same length as the number of cells. |
trans |
a transformation function (or NULL) for the counts, e.g., sqrt. |
colorcut |
An integer for the number of equi-spaced colorcut in [0,1] to assign colors to values. Alternatively, a vector of custom colorcut spacing between [0, 1]. |
colramp |
Color function that accept an integer n and return n colors. |
def.unit |
Default unit to be used. |
No return value
Adds hexagons to plot
Dan Carr <[email protected]>; ported and extended by Nicholas Lewin-Koh [email protected]. Modified by Quoc Hoang Nguyen <[email protected]> for hexDensity.
Adapted from hexbin to output hexagons with 0 count, and also to allow points with different weights. Default to use regular hexagon. See hexbin for extra detail on the output.
hexbinFull( x, y = NULL, xbins = 128, shape = NULL, xbnds = range(x), ybnds = range(y), xlab = NULL, ylab = NULL, IDs = FALSE, weight = NULL )
hexbinFull( x, y = NULL, xbins = 128, shape = NULL, xbnds = range(x), ybnds = range(y), xlab = NULL, ylab = NULL, IDs = FALSE, weight = NULL )
x , y
|
Coords of the points or a single plotting structure to be used in binning. See xy.coords. |
xbins |
Number of bins in a row. |
shape |
shape = yheight/xwidth of the plotting regions |
xbnds , ybnds
|
Horizontal and vertical limits of the binning region in x or y units respectively, must encompass range(x) or range(y) respectively; Vector of length 2 |
xlab , ylab
|
Optional character strings used as labels for x and y. If NULL, sensible defaults are used. |
IDs |
Logical indicating if the hexagonal cell ID for each point should be returned, see hexbin. |
weight |
Numeric weight vector to be assigned to points. |
an S4 object of class hexbin.
Dan Carr <[email protected]>; ported and extended by Nicholas Lewin-Koh <[email protected]>. Modified by Quoc Hoang Nguyen <[email protected]> for hexDensity.
Carr, D. B. et al. (1987) Scatterplot Matrix Techniques for Large N. JASA 83, 398, 424–436.
set.seed(133) d=hexbinFull(x=rnorm(20000),y=rnorm(20000),xbins=50) plotHexDensity(d)
set.seed(133) d=hexbinFull(x=rnorm(20000),y=rnorm(20000),xbins=50) plotHexDensity(d)
Algorithm is a modification of the meandering triangles as described in https://blog.bruce-hill.com/meandering-triangles to work with hexagons. See isolines for details about the output.
hexContour(hexDensity, levels)
hexContour(hexDensity, levels)
hexDensity |
hexDensity object to be contoured. |
levels |
Numeric vector for which contour lines should be generated |
This function is made to follow the same behaviour as isolines. A dedicated plotting function is in the work. Meanwhile, see example of how to plot the output with ggplot2's geom_path.
A list of x, y, and ID, for the contour line at each levels. ID indicates the different line segments making up the contour.
set.seed(133) x=rnorm(200) y=rnorm(200) d = hexDensity(x=x,y=y,bandwidth=0.4) cutoff=quantile(d@count,0.9) lines = hexContour(d,cutoff) library(ggplot2) library(hexbin) #plot against density ggplot()+ geom_point( aes(x=hcell2xy(d)$x, y=hcell2xy(d)$y, col=d@count) ) + scale_color_viridis_c()+ geom_path( aes( x = lines[[1]]$x, y = lines[[1]]$y, group = lines[[1]]$id ) ) #plot against data points ggplot() + geom_point( aes(x=x,y=y)) + geom_path( aes( x = lines[[1]]$x, y = lines[[1]]$y, group = lines[[1]]$id ) )
set.seed(133) x=rnorm(200) y=rnorm(200) d = hexDensity(x=x,y=y,bandwidth=0.4) cutoff=quantile(d@count,0.9) lines = hexContour(d,cutoff) library(ggplot2) library(hexbin) #plot against density ggplot()+ geom_point( aes(x=hcell2xy(d)$x, y=hcell2xy(d)$y, col=d@count) ) + scale_color_viridis_c()+ geom_path( aes( x = lines[[1]]$x, y = lines[[1]]$y, group = lines[[1]]$id ) ) #plot against data points ggplot() + geom_point( aes(x=x,y=y)) + geom_path( aes( x = lines[[1]]$x, y = lines[[1]]$y, group = lines[[1]]$id ) )
Kernel Density Estimation with Hexagonal grid.
hexDensity( x, y = NULL, xbins = 128, bandwidth = NULL, edge = TRUE, diggle = FALSE, weight = NULL, ... )
hexDensity( x, y = NULL, xbins = 128, bandwidth = NULL, edge = TRUE, diggle = FALSE, weight = NULL, ... )
x , y
|
Coords of the points or a single plotting structure to be used in binning. See xy.coords. |
xbins |
Number of bins in a row. |
bandwidth |
Bandwidth for the smoothing kernel. Either a scalar, a vector of length 2, or a 2x2 variance-covariance matrix for the bandwidths in the x and y directions. |
edge |
Logical value for whether to apply edge correction. Default is TRUE. |
diggle |
Logical value for apply edge correction with the more accurate Jones-Diggle method (need 'edge' to be TRUE). |
weight |
numeric weight vector to be assigned to points. |
... |
arguments for hexbinFull |
Default bandwidth is 1/8 of the range of the smaller dimensions.
an S4 object of class hexbin.
Diggle, P. J. (2010) Nonparametric methods. Chapter 18, pp. 299–316 in A.E. Gelfand, P.J. Diggle, M. Fuentes and P. Guttorp (eds.) Handbook of Spatial Statistics, CRC Press, Boca Raton, FL.
Jones, M. C. (1993) Simple boundary corrections for kernel density estimation. Statistics and Computing 3, 135–146.
set.seed(133) d = hexDensity(x=rnorm(200),y=rnorm(200),bandwidth=0.15)
set.seed(133) d = hexDensity(x=rnorm(200),y=rnorm(200),bandwidth=0.15)
Meandering triangles for hexagonal grid in C++
meanderingTriangles(x.coords.left, x.coords.right, y.coords, z, levels)
meanderingTriangles(x.coords.left, x.coords.right, y.coords, z, levels)
x.coords.left |
Vector for x coords of left-aligned rows (row 1,3,5,...) |
x.coords.right |
Vector for x coords of right-aligned rows (row 2,4,6,...) |
y.coords |
Vector for y coords of all rows. |
z |
Matrix for elevation values for the grid point |
levels |
Vector of z value cutoffs for contouring. |
This function is not meant to be used as is, unless you are very familiar with how hexContour works.
list of x, y, and ID, for the contour line at each levels.
Hill, B. (2017) Meandering triangles. Naming Things. https://blog.bruce-hill.com/meandering-triangles
Adapted the plotting function from hexbin. X and Y axes now have the same scale with option for different aspect ratio. Ribbon legend for continuous data.
plotHexDensity( hexDensity, main = NULL, xlab = NULL, ylab = NULL, xaxt = TRUE, yaxt = TRUE, lcex = 1, colramp = colorRampPalette(col.viridis), colorcut = 1024, legend = TRUE, legendWidth = 0.05, legendDistance = 0.15, aspectRatio = diff(hexDensity@xbnds)/diff(hexDensity@ybnds), margin = 0.18, newpage = TRUE )
plotHexDensity( hexDensity, main = NULL, xlab = NULL, ylab = NULL, xaxt = TRUE, yaxt = TRUE, lcex = 1, colramp = colorRampPalette(col.viridis), colorcut = 1024, legend = TRUE, legendWidth = 0.05, legendDistance = 0.15, aspectRatio = diff(hexDensity@xbnds)/diff(hexDensity@ybnds), margin = 0.18, newpage = TRUE )
hexDensity |
hexbin object returned by hexDensity |
main |
Main title |
xlab , ylab
|
x-axis and y-axis label |
xaxt , yaxt
|
Logical. Whether to plot x,y axes |
lcex |
Expansion factor for all letters. |
colramp |
Color function that accept an integer n and return n colors. |
colorcut |
An integer for the number of equi-spaced colorcut in [0,1] to assign colors to values. Alternatively, a vector of custom colorcut spacing between [0, 1]. |
legend |
Legend is currently non-functional and should be ignored. |
legendWidth |
Expansion factor for legend width. |
legendDistance |
Expansion factor for the space between the plot and the legend.s |
aspectRatio |
width to height ratio of the plot. Default is the (inverse of) shape value of hexDensity. |
margin |
Minimum guaranteed margin for the plot. Different aspect ratio between the screen and the plot means that margin can be larger on certain sides. |
newpage |
logical for whether to plot on a new page. |
No return value
Create kernel density estimate plot with hexagons
Dan Carr <[email protected]>; ported and extended by Nicholas Lewin-Koh [email protected]. Modified by Quoc Hoang Nguyen <[email protected]> for hexDensity.
set.seed(133) d = hexDensity(x=rnorm(200),y=rnorm(200),bandwidth=0.15) plotHexDensity(d)
set.seed(133) d = hexDensity(x=rnorm(200),y=rnorm(200),bandwidth=0.15) plotHexDensity(d)
Find the hexagon cells IDs from xy coordinates given a hexbin object. Useful if you want to get the KDE value at a certain coordinate.
xy2hcell( hexbin = NULL, x, y = NULL, xbins = NULL, xbnds = NULL, ybnds = NULL, shape = NULL )
xy2hcell( hexbin = NULL, x, y = NULL, xbins = NULL, xbnds = NULL, ybnds = NULL, shape = NULL )
hexbin |
hexbin object to be referenced to. |
x , y
|
coordinates or vectors of coordinates of the points. |
xbins |
number of bins partitioning the range of xbnds |
xbnds , ybnds
|
horizontal and vertical limit of the binning region. Must be numeric vector of length 2. |
shape |
shape = yheight/xwidth of the plotting regions. |
If a hexbin object is not provided, parameters of the binning region (xbins, xbnds, ybnds, shape) can be used instead. For finding the xy coordinates of the hexagons for a hexbin object, see hcell2xy.
a vector the same length as x with the hexagonal cell ID for each point
library(hexbin) set.seed(133) d=hexDensity(x=rnorm(20000),y=rnorm(20000),xbins=50) #Get KDE value at the coordinate x=0,y=0 loc = xy2hcell(d,x=0,y=0) d@count[loc]
library(hexbin) set.seed(133) d=hexDensity(x=rnorm(20000),y=rnorm(20000),xbins=50) #Get KDE value at the coordinate x=0,y=0 loc = xy2hcell(d,x=0,y=0) d@count[loc]