Skip to content

Commit 073c9a0

Browse files
authored
Convert size = NA to 0 before rendering (#4672)
1 parent 4d2333a commit 073c9a0

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

NEWS.md

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

3+
* Setting `size = NA` will no longer cause `guide_legend()` to error
4+
(@thomasp85, #4559)
5+
36
* Setting `stroke` to `NA` in `geom_point()` will no longer impair the sizing of
47
the points (@thomasp85, #4624)
58

R/guide-legend.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ guide_geom.legend <- function(guide, layers, default_mapping) {
278278
# override.aes in guide_legend manually changes the geom
279279
data <- modify_list(data, guide$override.aes)
280280

281+
if (!is.null(data$size)) {
282+
data$size[is.na(data$size)] <- 0
283+
}
284+
281285
list(
282286
draw_key = layer$geom$draw_key,
283287
data = data,
@@ -383,6 +387,7 @@ guide_gengrob.legend <- function(guide, theme) {
383387
)
384388

385389
key_size_mat <- do.call("cbind", lapply(guide$geoms, function(g) g$data$size / 10))
390+
386391
if (nrow(key_size_mat) == 0 || ncol(key_size_mat) == 0) {
387392
key_size_mat <- matrix(0, ncol = 1, nrow = nbreak)
388393
}

tests/testthat/test-guides.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ test_that("guide merging for guide_legend() works as expected", {
182182
expect_equal(repeated_identical_labels[[1]]$key$.label, c("label1", "label1", "label2"))
183183
})
184184

185+
test_that("size = NA doesn't throw rendering errors", {
186+
df = data.frame(
187+
x = c(1, 2),
188+
group = c("a","b")
189+
)
190+
p <- ggplot(df, aes(x = x, y = 0, colour = group)) +
191+
geom_point(size = NA, na.rm = TRUE)
192+
193+
expect_silent(plot(p))
194+
})
195+
185196
# Visual tests ------------------------------------------------------------
186197

187198
test_that("axis guides are drawn correctly", {

0 commit comments

Comments
 (0)