Skip to content

Fix annotate without global data #1690

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

Merged
merged 1 commit into from
Aug 3, 2016
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
Expand Up @@ -39,6 +39,9 @@

* Fix error message of Stats ggprotos when required aesthetics are
missing.

* Fix bug that resulted in several annotation_x function not getting drawn when
global data was lacking (#1655)

# ggplot2 2.1.0

Expand Down
4 changes: 2 additions & 2 deletions R/annotation-custom.r
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ NULL
#' annotation_custom(grob = g, xmin = 1, xmax = 10, ymin = 8, ymax = 10)
annotation_custom <- function(grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) {
layer(
data = NULL,
data = dummy_data(),
stat = StatIdentity,
position = PositionIdentity,
geom = GeomCustomAnn,
inherit.aes = TRUE,
inherit.aes = FALSE,
params = list(
grob = grob,
xmin = xmin,
Expand Down
2 changes: 1 addition & 1 deletion R/annotation-logticks.r
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ annotation_logticks <- function(base = 10, sides = "bl", scaled = TRUE,
colour <- color

layer(
data = data.frame(x = NA),
data = dummy_data(),
mapping = NULL,
stat = StatIdentity,
geom = GeomLogticks,
Expand Down
2 changes: 1 addition & 1 deletion R/annotation-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ annotation_map <- function(map, ...) {
stopifnot(all(c("x", "y", "id") %in% names(map)))

layer(
data = NULL,
data = dummy_data(),
stat = StatIdentity,
geom = GeomAnnotationMap,
position = PositionIdentity,
Expand Down
4 changes: 2 additions & 2 deletions R/annotation-raster.r
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ annotation_raster <- function(raster, xmin, xmax, ymin, ymax,
raster <- grDevices::as.raster(raster)

layer(
data = NULL,
data = dummy_data(),
mapping = NULL,
stat = StatIdentity,
position = PositionIdentity,
geom = GeomRasterAnn,
inherit.aes = TRUE,
inherit.aes = FALSE,
params = list(
raster = raster,
xmin = xmin,
Expand Down
4 changes: 4 additions & 0 deletions R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ dispatch_args <- function(f, ...) {
f
}

# Used in annotations to ensure printed even when no
# global data
dummy_data <- function() data.frame(x = NA)

# Needed to trigger package loading
#' @importFrom tibble tibble
NULL
20 changes: 20 additions & 0 deletions tests/testthat/test-annotate.r
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,23 @@ test_that("segment annotations transform with scales", {
annotate("segment", x = 2, y = 10, xend = 5, yend = 30, colour = "red") +
scale_y_reverse()
})

test_that("annotation_* has dummy data assigned and don't inherit aes", {
custom <- annotation_custom(zeroGrob())
logtick <- annotation_logticks()
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)

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