Skip to content

Commit c877eaf

Browse files
committed
Unit arithmetic objects are lists.
Fixes #1437
1 parent c68ce24 commit c877eaf

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

NEWS.md

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

3+
* You can once again set legend key and height width to unit arithmetic
4+
objects (like `2 * unit(1, "cm")`) (#1437).
5+
36
* When mapping an aesthetic to a constant (e.g.
47
`geom_smooth(aes(colour = "loess")))`), the default guide title is the name
58
of the aesthetic, not the value (#1431).

R/utilities-grid.r

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ ggname <- function(prefix, grob) {
1616
width_cm <- function(x) {
1717
if (is.grob(x)) {
1818
convertWidth(grobWidth(x), "cm", TRUE)
19-
} else if (is.list(x)) {
20-
vapply(x, width_cm, numeric(1))
2119
} else if (is.unit(x)) {
2220
convertWidth(x, "cm", TRUE)
21+
} else if (is.list(x)) {
22+
vapply(x, width_cm, numeric(1))
2323
} else {
2424
stop("Unknown input")
2525
}
2626
}
2727
height_cm <- function(x) {
2828
if (is.grob(x)) {
2929
convertWidth(grobHeight(x), "cm", TRUE)
30-
} else if (is.list(x)) {
31-
vapply(x, height_cm, numeric(1))
3230
} else if (is.unit(x)) {
3331
convertHeight(x, "cm", TRUE)
32+
} else if (is.list(x)) {
33+
vapply(x, height_cm, numeric(1))
3434
} else {
3535
stop("Unknown input")
3636
}

tests/testthat/test-grid-utils.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
context("Grid utilites")
2+
3+
test_that("width_cm and height_cm work with unit arithmetic", {
4+
x <- 2 * unit(1, "cm")
5+
6+
expect_equal(width_cm(x), 2)
7+
expect_equal(height_cm(x), 2)
8+
})

0 commit comments

Comments
 (0)