Skip to content

Commit 0e39f4b

Browse files
committed
implement GeomHex
1 parent 692b838 commit 0e39f4b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ S3method(to_basic,GeomDensity)
2121
S3method(to_basic,GeomDensity2d)
2222
S3method(to_basic,GeomErrorbar)
2323
S3method(to_basic,GeomErrorbarh)
24+
S3method(to_basic,GeomHex)
2425
S3method(to_basic,GeomHline)
2526
S3method(to_basic,GeomJitter)
2627
S3method(to_basic,GeomLine)

R/layers2traces.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,23 @@ to_basic.GeomTile <- function(data, prestats_data, layout, params, ...) {
234234
}
235235
}
236236

237+
#' @export
238+
to_basic.GeomHex <- function(data, prestats_data, layout, params, ...) {
239+
# see ggplot2:::hexGrob
240+
dx <- resolution(data$x, FALSE)
241+
dy <- resolution(data$y, FALSE)/sqrt(3)/2 * 1.15
242+
hexC <- hexbin::hexcoords(dx, dy, n = 1)
243+
n <- nrow(data)
244+
data$size <- ifelse(data$size < 1, data$size ^ (1 / 6), data$size ^ 2)
245+
x <- rep.int(hexC$x, n) * rep(data$size, each = 6) + rep(data$x, each = 6)
246+
y <- rep.int(hexC$y, n) * rep(data$size, each = 6) + rep(data$y, each = 6)
247+
data <- data[rep(seq_len(n), each = 6), ]
248+
data$x <- x
249+
data$y <- y
250+
data$group <- rep(seq_len(n), each = 6)
251+
prefix_class(data, c("GeomPolygon", "GeomHex"))
252+
}
253+
237254
#' @export
238255
to_basic.GeomContour <- function(data, prestats_data, layout, params, ...) {
239256
if (!"fill" %in% names(data)) data$fill <- NA
@@ -291,6 +308,7 @@ to_basic.GeomJitter <- function(data, prestats_data, layout, params, ...) {
291308
prefix_class(data, "GeomPoint")
292309
}
293310

311+
294312
#' @export
295313
to_basic.GeomErrorbar <- function(data, prestats_data, layout, params, ...) {
296314
# width for ggplot2 means size of the entire bar, on the data scale

0 commit comments

Comments
 (0)