Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 62 lines (55 sloc) 1.741 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
# Name ggplot grid object
# Convenience function to name grid objects
#
# @keyword internal
ggname <- function(prefix, grob) {
  grob$name <- grobName(grob, prefix)
  grob
}

# Global grob editing
# To match grid.gedit
#
# @keywords internal
geditGrob <- function(..., grep = TRUE, global = TRUE) {
  editGrob(..., grep = grep, global = global)
}

width_cm <- function(x) convertWidth(grobWidth(x), "cm", TRUE)
height_cm <- function(x) convertHeight(grobHeight(x), "cm", TRUE)

# Grob row heights
# Given a matrix of grobs, calculate the height needed for each row
#
# @arguments matrix of grobs
# @keywords internal
grobRowHeight <- function(mat) {
  row_heights <- alply(mat, 1, function(x) llply(x, grobHeight))
  do.call("unit.c", llply(row_heights, splat(max)))
}

# Grob column widths
# Given a matrix of grobs, calculate the width needed for each column
#
# @arguments matrix of grobs
# @keywords internal
grobColWidth <- function(mat) {
  col_widths <- alply(mat, 2, function(x) llply(x, grobWidth))
  do.call("unit.c", llply(col_widths, splat(max)))
}

# Build grob matrix
# Build a matrix of grobs given a vector of grobs and the desired dimensions of the matrix
#
# Any missing cells at the end will be filled in with zeroGrobs.
#
# @arguments vector of grobs
# @arguments number of rows
# @arguments number of columns
# @arguments should the matrix be arranged like a table or a plot
# @keywords internal
grobMatrix <- function(vec, nrow, ncol, as.table = FALSE) {
  if (nrow == 0 || ncol == 0) {
    return(matrix(ncol = ncol, nrow = nrow))
  }
  
  mat <- c(vec, rep(list(zeroGrob()), nrow * ncol - length(vec)))
  dim(mat) <- c(ncol, nrow)
  mat <- t(mat)
  if (!as.table) mat <- mat[rev(seq_len(nrow)), ]
  
  mat
}
Something went wrong with that request. Please try again.