forked from tidyverse/ggplot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeom-hex.r
49 lines (39 loc) · 1.31 KB
/
geom-hex.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
GeomHex <- proto(Geom, {
objname <- "hex"
desc <- "Tile the plane with hexagons"
draw <- function(., data, scales, coordinates, ...) {
with(coordinates$transform(data, scales),
ggname(.$my_name(), hexGrob(x, y, col=colour,
fill = alpha(fill, alpha)))
)
}
required_aes <- c("x", "y")
default_aes <- function(.) aes(colour=NA, fill = "grey50", size=0.5, alpha = 1)
default_stat <- function(.) StatBinhex
guide_geom <- function(.) "polygon"
examples <- function() {
# See ?stat_binhex for examples
}
})
# Draw hexagon grob
# Modified from code by Nicholas Lewin-Koh and Martin Maechler
#
# @arguments x positions of hex centres
# @arguments y positions
# @arguments vector of hex sizes
# @arguments border colour
# @arguments fill colour
# @keyword internal
hexGrob <- function(x, y, size = rep(1, length(x)), colour = "grey50", fill = "grey90") {
stopifnot(length(y) == length(x))
dx <- resolution(x, FALSE)
dy <- resolution(y, FALSE) / sqrt(3) / 2 * 1.15
hexC <- hexcoords(dx, dy, n = 1)
n <- length(x)
polygonGrob(
x = rep.int(hexC$x, n) * rep(size, each = 6) + rep(x, each = 6),
y = rep.int(hexC$y, n) * rep(size, each = 6) + rep(y, each = 6),
default.units = "native",
id.lengths = rep(6, n), gp = gpar(col = colour, fill = fill)
)
}