Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 3.0.0.9000

* `geom_hex()` now understands the `size` and `linetype` aesthetics
(@mikmart, #2488).

# ggplot2 3.0.0

## Breaking changes
Expand Down
28 changes: 19 additions & 9 deletions R/geom-hex.r
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,27 @@ GeomHex <- ggproto("GeomHex", Geom,
stop("geom_hex() only works with Cartesian coordinates", call. = FALSE)
}

coord <- coord$transform(data, panel_params)
coords <- coord$transform(data, panel_params)
ggname("geom_hex", hexGrob(
coord$x, coord$y, colour = coord$colour,
fill = alpha(coord$fill, coord$alpha)
coords$x, coords$y,
gp = gpar(
col = coords$colour,
fill = alpha(coords$fill, coords$alpha),
lwd = coords$size * .pt,
lty = coords$linetype
)
))
},

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

default_aes = aes(colour = NA, fill = "grey50", size = 0.5, alpha = NA),
default_aes = aes(
colour = NA,
fill = "grey50",
size = 0.5,
linetype = 1,
alpha = NA
),

draw_key = draw_key_polygon
)
Expand All @@ -79,11 +90,10 @@ GeomHex <- ggproto("GeomHex", Geom,
#
# @param x positions of hex centres
# @param y positions
# @param vector of hex sizes
# @param border colour
# @param fill colour
# @param size vector of hex sizes
# @param gp graphical parameters
# @keyword internal
hexGrob <- function(x, y, size = rep(1, length(x)), colour = "grey50", fill = "grey90") {
hexGrob <- function(x, y, size = rep(1, length(x)), gp = gpar()) {
stopifnot(length(y) == length(x))

dx <- resolution(x, FALSE)
Expand All @@ -97,6 +107,6 @@ hexGrob <- function(x, y, size = rep(1, length(x)), colour = "grey50", fill = "g
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)
id.lengths = rep(6, n), gp = gp
)
}
1 change: 1 addition & 0 deletions man/geom_hex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-geom-hex.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ test_that("density and value summaries are available", {
expect_equal(out$density, c(0.75, 0.25), tolerance = 1e-7)
expect_equal(out$count, c(3, 1), tolerance = 1e-7)
})

test_that("size and linetype are applied", {
df <- data.frame(x = c(1, 1, 1, 2), y = c(1, 1, 1, 2))
plot <- ggplot(df, aes(x, y)) +
geom_hex(color = "red", size = 4, linetype = 2)

gpar <- layer_grob(plot)[[1]]$children[[1]]$gp
expect_equal(gpar$lwd, c(4, 4) * .pt, tolerance = 1e-7)
expect_equal(gpar$lty, c(2, 2), tolerance = 1e-7)
})