r-lib / gtable

The layout packages that powers ggplot2
https://gtable.r-lib.org
Other
87 stars 18 forks source link

gtable_matrix should inherit dimnames from matrix #31

Closed wch closed 5 years ago

wch commented 12 years ago

These two blocks of code should give the same result for dimnames(gt), but they don't:

g <- rectGrob()

grobmat <- matrix(list(g,g,g,g,g,g), nrow=2,
  dimnames = list(yname = letters[1:2], xname = LETTERS[1:3]))
gt <- gtable_matrix("test", grobmat,
  widths=unit(c(1,2,3),"cm"), heights=unit(c(1,2),"cm"))
dimnames(gt)
# [[1]]
# NULL
# [[2]]
# NULL

grobmat <- matrix(list(g,g,g,g,g,g), nrow=2)
gt <- gtable_matrix("test", grobmat,
  widths=unit(c(1,2,3),"cm"), heights=unit(c(1,2),"cm"))
dimnames(gt) <- list(yname = letters[1:2], nname = LETTERS[1:3])
dimnames(gt)
# [[1]]
# [1] "a" "b"
# [[2]]
# [1] "A" "B" "C"

The same should also work for gtable_col and gtable_row:

groblist <- list(a=g, b=g, c=g)
gt <- gtable_row("test", groblist)
dimnames(gt)
# [[1]]
# NULL
# [[2]]
# NULL

## The above code _should_ give the same result as this:
groblist <- list(g,g,g)
gt <- gtable_row("test", groblist)
dimnames(gt) <- list(c("a", "b", "c"), NULL)
dimnames(gt)
# [[1]]
# [1] "a" "b" "c"
# [[2]]
# NULL

Although it's not clear how the [[2]] dimension name should be set in this case.