Skip to content

WIP: Pass actual data to annotation_raster() and annotation_custom() instead of dummy data #3121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
21 changes: 10 additions & 11 deletions R/annotation-custom.r
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,17 @@ NULL
#' base +
#' annotation_custom(grob = g, xmin = 1, xmax = 10, ymin = 8, ymax = 10)
annotation_custom <- function(grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) {
data <- new_data_frame(list(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), n = 1)

layer(
data = dummy_data(),
data = data,
mapping = aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
stat = StatIdentity,
position = PositionIdentity,
geom = GeomCustomAnn,
inherit.aes = FALSE,
params = list(
grob = grob,
xmin = xmin,
xmax = xmax,
ymin = ymin,
ymax = ymax
grob = grob
)
)
}
Expand All @@ -64,18 +63,18 @@ annotation_custom <- function(grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax =
#' @export
GeomCustomAnn <- ggproto("GeomCustomAnn", Geom,
extra_params = "",

handle_na = function(data, params) {
data
},

draw_panel = function(data, panel_params, coord, grob, xmin, xmax,
ymin, ymax) {
draw_panel = function(data, panel_params, coord, grob) {
if (!inherits(coord, "CoordCartesian")) {
stop("annotation_custom only works with Cartesian coordinates",
call. = FALSE)
}
corners <- new_data_frame(list(x = c(xmin, xmax), y = c(ymin, ymax)), n = 2)
data <- coord$transform(corners, panel_params)
data <- new_data_frame(list(x = c(data$xmin, data$xmax), y = c(data$ymin, data$ymax)), n = 2)
data <- coord$transform(data, panel_params)

x_rng <- range(data$x, na.rm = TRUE)
y_rng <- range(data$y, na.rm = TRUE)
Expand All @@ -86,7 +85,7 @@ GeomCustomAnn <- ggproto("GeomCustomAnn", Geom,
editGrob(grob, vp = vp, name = paste(grob$name, annotation_id()))
},

default_aes = aes_(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
required_aes = c("xmin", "xmax", "ymin", "ymax")
)

annotation_id <- local({
Expand Down
17 changes: 8 additions & 9 deletions R/annotation-raster.r
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,17 @@ NULL
annotation_raster <- function(raster, xmin, xmax, ymin, ymax,
interpolate = FALSE) {
raster <- grDevices::as.raster(raster)
data <- new_data_frame(list(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), n = 1)

layer(
data = dummy_data(),
mapping = NULL,
data = data,
mapping = aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
stat = StatIdentity,
position = PositionIdentity,
geom = GeomRasterAnn,
inherit.aes = FALSE,
params = list(
raster = raster,
xmin = xmin,
xmax = xmax,
ymin = ymin,
ymax = ymax,
interpolate = interpolate
)
)
Expand All @@ -76,14 +73,16 @@ GeomRasterAnn <- ggproto("GeomRasterAnn", Geom,
stop("annotation_raster only works with Cartesian coordinates",
call. = FALSE)
}
corners <- new_data_frame(list(x = c(xmin, xmax), y = c(ymin, ymax)), n = 2)
data <- coord$transform(corners, panel_params)
data <- new_data_frame(list(x = c(data$xmin, data$xmax), y = c(data$ymin, data$ymax)), n = 2)
data <- coord$transform(data, panel_params)

x_rng <- range(data$x, na.rm = TRUE)
y_rng <- range(data$y, na.rm = TRUE)

rasterGrob(raster, x_rng[1], y_rng[1],
diff(x_rng), diff(y_rng), default.units = "native",
just = c("left","bottom"), interpolate = interpolate)
}
},

required_aes = c("xmin", "xmax", "ymin", "ymax")
)
42 changes: 29 additions & 13 deletions tests/testthat/test-annotate.r
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,38 @@ test_that("segment annotations transform with scales", {
expect_doppelganger("line matches points", plot)
})

test_that("annotation_* has dummy data assigned and don't inherit aes", {
custom <- annotation_custom(zeroGrob())
logtick <- annotation_logticks()
test_that("annotation_custom() has data and don't inherit aes", {
custom <- annotation_custom(zeroGrob(), xmin = -1, xmax = 1, ymin = -1, ymax = 1)

expect_equal(custom$data, data_frame(xmin = -1, xmax = 1, ymin = -1, ymax = 1))
# can be transformed
expect_equal(layer_data(ggplot() + custom + scale_x_reverse() + scale_y_reverse())[, c("xmin", "xmax", "ymin", "ymax")],
data_frame(xmin = 1, xmax = -1, ymin = 1, ymax = -1))

expect_false(custom$inherit.aes)
})

test_that("annotation_raster() has data and don't inherit aes", {
rainbow <- matrix(hcl(seq(0, 360, length.out = 50 * 50), 80, 70), nrow = 50)
raster <- annotation_raster(rainbow, 15, 20, 3, 4)

expect_equal(raster$data, data_frame(xmin = 15, xmax = 20, ymin = 3, ymax = 4))
# can be transformed
expect_equal(layer_data(ggplot() + raster + scale_x_reverse() + scale_y_reverse())[, c("xmin", "xmax", "ymin", "ymax")],
data_frame(xmin = -15, xmax = -20, ymin = -3, ymax = -4))

expect_false(raster$inherit.aes)
})

test_that("annotation_map() and annotation_logstick() has dummy data assigned and don't inherit aes", {
library(maps)
usamap <- map_data("state")
map <- annotation_map(usamap)
rainbow <- matrix(hcl(seq(0, 360, length.out = 50 * 50), 80, 70), nrow = 50)
raster <- annotation_raster(rainbow, 15, 20, 3, 4)
dummy <- dummy_data()
expect_equal(custom$data, dummy)
expect_equal(logtick$data, dummy)
expect_equal(map$data, dummy)
expect_equal(raster$data, dummy)
logtick <- annotation_logticks()

expect_equal(map$data, dummy_data())
expect_equal(logtick$data, dummy_data())

expect_false(custom$inherit.aes)
expect_false(logtick$inherit.aes)
expect_false(map$inherit.aes)
expect_false(raster$inherit.aes)
expect_false(logtick$inherit.aes)
})