Skip to contents

Bins 2-d points on a hexagon lattice using the Dan Carr hbin algorithm: two interleaved rectangular lattices of hexagon centers, lattice A at \((jw, iw\sqrt{3})\) and lattice B at \(((j+1/2)w, (i+1/2)w\sqrt{3})\), with hexagon x-spacing w. In scaled coordinates \(s_x = (x - x_{min})/w\), \(s_y = (y - y_{min})/(w\sqrt{3})\) a point belongs to the nearer candidate center under \(d^2 = (s_x - j)^2 + 3(s_y - i)^2\), the Euclidean distance in data units up to the factor \(w^2\). The fast-path thresholds 1/4 and 1/3 are the con1 and con2 constants of the Fortran original.

Usage

Hexgrd(coords, values = NULL, cell_size = 1)

hexagonal_grid(coords, values = NULL, cell_size = 1)

Arguments

coords

Point locations, n by 2.

values

Optional values to average per cell.

cell_size

Hexagon width w (center spacing along x).

Value

List with cell_id (1-based, per point), centers, counts, xcm, ycm, cell_size, n, and value_mean if values were given.

References

Carr, D. B., Littlefield, R. J., Nicholson, W. L. and Littlefield, J. S. (1987). Scatterplot matrix techniques for large N. Journal of the American Statistical Association, 82(398), 424-436.

Carr, D. B., Olsen, A. R. and White, D. (1992). Hexagon mosaic maps for display of univariate and bivariate geographical data. Cartography and Geographic Information Systems, 19(4), 228-236.

Reference implementation: Fortran subroutine hbin, src/hbin.f in CRAN hexbin 1.28.6.

Examples

set.seed(1)
Hexgrd(cbind(runif(50), runif(50)), cell_size = 0.3)
#> $cell_id
#>  [1]  7  8 12 11  5 14 15 12 12  2  4  2 12  6 16  6 12 17  5 13 15  4 11  2  7
#> [26]  8  4  6 13  9  6 12  6  2 13 11 13  1 11  5 11 10 16  8  8 13  3  6 13 12
#> 
#> $centers
#>             [,1]       [,2]
#>  [1,] 0.01339033 0.05893438
#>  [2,] 0.16339033 0.31874200
#>  [3,] 0.01339033 0.57854962
#>  [4,] 0.16339033 0.83835724
#>  [5,] 0.31339033 0.05893438
#>  [6,] 0.46339033 0.31874200
#>  [7,] 0.31339033 0.57854962
#>  [8,] 0.46339033 0.83835724
#>  [9,] 0.31339033 1.09816486
#> [10,] 0.61339033 0.05893438
#> [11,] 0.76339033 0.31874200
#> [12,] 0.61339033 0.57854962
#> [13,] 0.76339033 0.83835724
#> [14,] 0.91339033 0.05893438
#> [15,] 1.06339033 0.31874200
#> [16,] 0.91339033 0.57854962
#> [17,] 1.06339033 0.83835724
#> 
#> $counts
#>  [1] 1 4 1 3 3 6 2 4 1 1 5 7 6 1 2 2 1
#> 
#> $xcm
#>  [1] 0.1079436 0.1375289 0.0233312 0.1438358 0.3309972 0.4528404 0.2663647
#>  [8] 0.4602485 0.3403490 0.6470602 0.7546011 0.6513863 0.7984032 0.8983897
#> [15] 0.9396902 0.7763871 0.9919061
#> 
#> $ycm
#>  [1] 0.12169192 0.33989016 0.45527445 0.87221858 0.09941011 0.37085649
#>  [7] 0.47698543 0.85214793 0.96061800 0.05893438 0.25585819 0.55339934
#> [13] 0.78817158 0.09946616 0.32767232 0.64657936 0.76631067
#> 
#> $cell_size
#> [1] 0.3
#> 
#> $n
#> [1] 50
#> 
#> $method
#> [1] "Carr hexagon binning (hbin transcription)"
#>