Skip to content

Commit e0797dc

Browse files
mikmarthadley
authored andcommitted
Support size and linetype in geom_hex (#2488) (#2670)
* Support size and linetype in geom_hex (#2488) * Add unit test for geom_hex size and linetype * Update NEWS with new geom_hex() aesthetics
1 parent d506990 commit e0797dc

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 3.0.0.9000
22

3+
* `geom_hex()` now understands the `size` and `linetype` aesthetics
4+
(@mikmart, #2488).
5+
36
# ggplot2 3.0.0
47

58
## Breaking changes

R/geom-hex.r

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,27 @@ GeomHex <- ggproto("GeomHex", Geom,
5959
stop("geom_hex() only works with Cartesian coordinates", call. = FALSE)
6060
}
6161

62-
coord <- coord$transform(data, panel_params)
62+
coords <- coord$transform(data, panel_params)
6363
ggname("geom_hex", hexGrob(
64-
coord$x, coord$y, colour = coord$colour,
65-
fill = alpha(coord$fill, coord$alpha)
64+
coords$x, coords$y,
65+
gp = gpar(
66+
col = coords$colour,
67+
fill = alpha(coords$fill, coords$alpha),
68+
lwd = coords$size * .pt,
69+
lty = coords$linetype
70+
)
6671
))
6772
},
6873

6974
required_aes = c("x", "y"),
7075

71-
default_aes = aes(colour = NA, fill = "grey50", size = 0.5, alpha = NA),
76+
default_aes = aes(
77+
colour = NA,
78+
fill = "grey50",
79+
size = 0.5,
80+
linetype = 1,
81+
alpha = NA
82+
),
7283

7384
draw_key = draw_key_polygon
7485
)
@@ -79,11 +90,10 @@ GeomHex <- ggproto("GeomHex", Geom,
7990
#
8091
# @param x positions of hex centres
8192
# @param y positions
82-
# @param vector of hex sizes
83-
# @param border colour
84-
# @param fill colour
93+
# @param size vector of hex sizes
94+
# @param gp graphical parameters
8595
# @keyword internal
86-
hexGrob <- function(x, y, size = rep(1, length(x)), colour = "grey50", fill = "grey90") {
96+
hexGrob <- function(x, y, size = rep(1, length(x)), gp = gpar()) {
8797
stopifnot(length(y) == length(x))
8898

8999
dx <- resolution(x, FALSE)
@@ -97,6 +107,6 @@ hexGrob <- function(x, y, size = rep(1, length(x)), colour = "grey50", fill = "g
97107
x = rep.int(hexC$x, n) * rep(size, each = 6) + rep(x, each = 6),
98108
y = rep.int(hexC$y, n) * rep(size, each = 6) + rep(y, each = 6),
99109
default.units = "native",
100-
id.lengths = rep(6, n), gp = gpar(col = colour, fill = fill)
110+
id.lengths = rep(6, n), gp = gp
101111
)
102112
}

man/geom_hex.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-geom-hex.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ test_that("density and value summaries are available", {
1010
expect_equal(out$density, c(0.75, 0.25), tolerance = 1e-7)
1111
expect_equal(out$count, c(3, 1), tolerance = 1e-7)
1212
})
13+
14+
test_that("size and linetype are applied", {
15+
df <- data.frame(x = c(1, 1, 1, 2), y = c(1, 1, 1, 2))
16+
plot <- ggplot(df, aes(x, y)) +
17+
geom_hex(color = "red", size = 4, linetype = 2)
18+
19+
gpar <- layer_grob(plot)[[1]]$children[[1]]$gp
20+
expect_equal(gpar$lwd, c(4, 4) * .pt, tolerance = 1e-7)
21+
expect_equal(gpar$lty, c(2, 2), tolerance = 1e-7)
22+
})

0 commit comments

Comments
 (0)