gridExtra/0000755000175100001440000000000012551253646012232 5ustar hornikusersgridExtra/TODO0000644000175100001440000000016212547411623012715 0ustar hornikusers NEW FEATURES * basic plots (image, plot, É) in grid, using scales and guide packages of Hadley W. * matrix plotgridExtra/inst/0000755000175100001440000000000012551155163013202 5ustar hornikusersgridExtra/inst/tests/0000755000175100001440000000000012551155163014344 5ustar hornikusersgridExtra/inst/tests/testthat.r0000644000175100001440000000007612550055367016376 0ustar hornikuserslibrary(gridExtra) library(testthat) test_check("gridExtra") gridExtra/inst/tests/testthat/0000755000175100001440000000000012551253646016211 5ustar hornikusersgridExtra/inst/tests/testthat/test-arrangeGrob.R0000644000175100001440000000374212550060463021540 0ustar hornikuserslibrary(gridExtra) library(testthat) library(grid) r <- rectGrob(gp=gpar(fill="grey90")) context("Checking layout") test_that("nrow/ncol define a layout", { expect_that(dim(arrangeGrob(r,r,r)), equals(c(3,1))) expect_that(dim(arrangeGrob(r,r,r, nrow=2)), equals(c(2,2))) expect_that(dim(arrangeGrob(r,r,r, ncol=2)), equals(c(2,2))) expect_that(dim(arrangeGrob(r,r,r, ncol=4)), equals(c(1,4))) expect_that(dim(arrangeGrob(r,r,r, nrow=4)), equals(c(4,1))) expect_that(dim(arrangeGrob(r,r,r, nrow=1)), equals(c(1,3))) expect_that(dim(arrangeGrob(r,r,r, ncol=1)), equals(c(3,1))) expect_that(dim(arrangeGrob(r,r,r, ncol=2,nrow=2)), equals(c(2,2))) expect_that(dim(arrangeGrob(r,r,r, ncol=3,nrow=4)), equals(c(4,3))) expect_error(arrangeGrob(r,r,r, ncol=1,nrow=1)) }) test_that("widths/heights define a layout", { expect_that(dim(arrangeGrob(r,r,r, widths=1)), equals(c(3,1))) expect_that(dim(arrangeGrob(r,r,r, heights=1)), equals(c(1,3))) expect_that(dim(arrangeGrob(r,r,r, widths=1:3)), equals(c(1,3))) expect_that(dim(arrangeGrob(r,r,r, widths=1:5)), equals(c(1,5))) expect_that(dim(arrangeGrob(r,r,r, heights=1:3)), equals(c(3,1))) expect_that(dim(arrangeGrob(r,r,r, heights=1:5)), equals(c(5,1))) expect_that(dim(arrangeGrob(r,r,r, widths=1:5)), equals(c(1,5))) expect_that(dim(arrangeGrob(r,r,r, widths=1:5, heights=1:5)), equals(c(5,5))) expect_error(arrangeGrob(r,r,r, widths=1, heights=1)) }) test_that("combinations of nrow/ncol and widths/heights define a layout", { expect_that(dim(arrangeGrob(r,r,r, nrow=2, widths=1:3)), equals(c(2,3))) expect_that(dim(arrangeGrob(r,r,r, ncol=2, heights=1:3)), equals(c(3,2))) expect_that(dim(arrangeGrob(r,r,r, ncol=2, widths=1:2)), equals(c(2,2))) expect_that(dim(arrangeGrob(r,r,r, nrow=2, heights=1:2)), equals(c(2,2))) expect_error(arrangeGrob(r,r,r, ncol=2, widths=1:3)) expect_error(arrangeGrob(r,r,r, nrow=2, heights=1:3)) }) gridExtra/inst/tests/testthat/test-tableGrob.R0000644000175100001440000000064712550057363021216 0ustar hornikuserslibrary(gridExtra) library(testthat) library(grid) m <- iris[1:4, 1:3] context("Checking tableGrob layout") test_that("tableGrob has the correct size", { expect_that(dim(tableGrob(m)), equals(c(5,4))) expect_that(dim(tableGrob(m, rows = NULL)), equals(c(5,3))) expect_that(dim(tableGrob(m, cols = NULL)), equals(c(4,4))) expect_that(dim(tableGrob(m, rows = NULL, cols = NULL)), equals(c(4,3))) }) gridExtra/inst/NEWS.md0000644000175100001440000000133412550061357014301 0ustar hornikusers# gridExtra 2.0.0 (2015-07-11) * removed experimental grobs and functions not widely used (they can be found at https://github.com/baptiste/gridextra if needed) * arrangeGrob/grid.arrange is now based on gtable * tableGrob/grid.table is now based on gtable # gridExtra 1.0.0 (2014-10-05) ## CLEANUP * several buggy functions removed # gridExtra 0.9.1 (2012-08-09) ## FIX * small compatibility issue of arrangeGrob with new class of ggplot2 # gridExtra 0.9 (2012-01-06) ## FIX * dependencies in examples, imports and exports ## NEW * multipage output and ggsave support for grid.arrange # gridExtra 0.8.5 (2011-10-26) ## FIX * removed LazyLoad, deprecated in R>=2.14 ## NEW * stextGrob text with a backgroundgridExtra/inst/doc/0000755000175100001440000000000012551155163013747 5ustar hornikusersgridExtra/inst/doc/tableGrob.R0000644000175100001440000001013512551155163015773 0ustar hornikusers## ----setup, echo=FALSE, results='hide'----------------------------------- library(knitr) opts_chunk$set(message=FALSE, fig.width=4, fig.height=2) ## ----basic--------------------------------------------------------------- library(gridExtra) library(grid) d <- head(iris[,1:3]) grid.table(d) ## ----annotations, fig.height=3------------------------------------------- d[2,3] <- "this is very wwwwwide" d[1,2] <- "this\nis\ntall" colnames(d) <- c("alpha*integral(xdx,a,infinity)", "this text\nis high", 'alpha/beta') tt <- ttheme_default(colhead=list(fg_params = list(parse=TRUE))) grid.table(d, theme=tt) ## ----theme, fig.width=8-------------------------------------------------- tt1 <- ttheme_default() tt2 <- ttheme_minimal() tt3 <- ttheme_minimal( core=list(bg_params = list(fill = blues9, col=NA), fg_params=list(fontface=3)), colhead=list(fg_params=list(col="navyblue")), rowhead=list(fg_params=list(col="navyblue", fontface=2L))) grid.arrange( tableGrob(iris[1:4, 1:2], theme=tt1), tableGrob(iris[1:4, 1:2], theme=tt2), tableGrob(iris[1:4, 1:2], theme=tt3), nrow=1) ## ----justify, fig.width=8------------------------------------------------ tt1 <- ttheme_default() tt2 <- ttheme_default(core=list(fg_params=list(hjust=1, x=0.9)), rowhead=list(fg_params=list(hjust=1, x=0.95))) tt3 <- ttheme_default(core=list(fg_params=list(hjust=0, x=0.1)), rowhead=list(fg_params=list(hjust=0, x=0))) grid.arrange( tableGrob(mtcars[1:4, 1:2], theme=tt1), tableGrob(mtcars[1:4, 1:2], theme=tt2), tableGrob(mtcars[1:4, 1:2], theme=tt3), nrow=1) ## ----sizes, fig.width=8-------------------------------------------------- g <- g2 <- tableGrob(iris[1:4, 1:3], cols = NULL, rows=NULL) g2$widths <- unit(rep(1/ncol(g2), ncol(g2)), "npc") grid.arrange(rectGrob(), rectGrob(), nrow=1) grid.arrange(g, g2, nrow=1, newpage = FALSE) ## ----separators---------------------------------------------------------- g <- tableGrob(iris[1:4, 1:3], theme=ttheme_minimal()) separators <- replicate(ncol(g) - 2, segmentsGrob(x1 = unit(0,"npc")), simplify=FALSE) g <- gtable::gtable_add_grob(g, grobs = separators, t = 1, b = nrow(g), l = seq_len(ncol(g)-2)+2) grid.draw(g) ## ----highlight----------------------------------------------------------- g <- tableGrob(iris[1:4, 1:3]) find_cell <- function(table, row, col, name="core-fg"){ l <- table$layout which(l$t==row & l$l==col & l$name==name) } ind <- find_cell(g, 3, 2, "core-fg") ind2 <- find_cell(g, 2, 3, "core-bg") g$grobs[ind][[1]][["gp"]] <- gpar(fontsize=15, fontface="bold") g$grobs[ind2][[1]][["gp"]] <- gpar(fill="darkolivegreen1", col = "darkolivegreen4", lwd=5) grid.draw(g) ## ----ftable, fig.width=6------------------------------------------------- grid.ftable <- function(d, padding = unit(4, "mm"), ...) { nc <- ncol(d) nr <- nrow(d) ## character table with added row and column names extended_matrix <- cbind(c("", rownames(d)), rbind(colnames(d), as.matrix(d))) ## string width and height w <- apply(extended_matrix, 2, strwidth, "inch") h <- apply(extended_matrix, 2, strheight, "inch") widths <- apply(w, 2, max) heights <- apply(h, 1, max) padding <- convertUnit(padding, unitTo = "in", valueOnly = TRUE) x <- cumsum(widths + padding) - 0.5 * padding y <- cumsum(heights + padding) - padding rg <- rectGrob(x = unit(x - widths/2, "in"), y = unit(1, "npc") - unit(rep(y, each = nc + 1), "in"), width = unit(widths + padding, "in"), height = unit(heights + padding, "in")) tg <- textGrob(c(t(extended_matrix)), x = unit(x - widths/2, "in"), y = unit(1, "npc") - unit(rep(y, each = nc + 1), "in"), just = "center") g <- gTree(children = gList(rg, tg), ..., x = x, y = y, widths = widths, heights = heights) grid.draw(g) invisible(g) } grid.newpage() grid.ftable(head(iris, 4), gp = gpar(fill = rep(c("grey90", "grey95"), each = 6))) gridExtra/inst/doc/ngonGrob.rmd0000644000175100001440000000305212551155163016226 0ustar hornikusers--- title: "Regular polygons and ellipses in grid graphics" author: "Baptiste Auguie" date: '`r Sys.Date()`' vignette: > %\VignetteEngine{knitr::rmarkdown} %\VignetteIndexEntry{ngonGrob: regular polygons and ellipses in grid graphics} output: knitr:::html_vignette: toc: yes --- ```{r setup, echo=FALSE, results='hide'} library(knitr) opts_chunk$set(message=FALSE, fig.width=4, fig.height=3) ``` The `gridExtra` package provides a basic implementation of regular polygons, `ngonGrob()/grid.ngon`, and a convenience function to draw ellipses, `ellipseGrob()/grid.ellipse()`. We illustrate below the basic usage of these vectorised functions. ## Basic usage ```{r basic} library(gridExtra) library(grid) library(grid) N <- 5 xy <- polygon_regular(N)*2 # draw multiple polygons g <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), n=seq_len(N)+2, gp=gpar(fill=1:N)) grid.newpage() grid.draw(g) ``` ## Rotated and stretched polygons ```{r rotated} g2 <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), n=seq_len(N)+2, rho=seq_len(N), phase=0, angle=pi/(seq_len(N)+2), size=1:N+5, gp=gpar(fill=1:N)) grid.newpage() grid.draw(g2) ``` ## Ellipses ```{r ellipse} g3 <- ellipseGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), angle=-2*seq(0,N-1)*pi/N+pi/2, size=5, rho=1/3, gp=gpar(fill=1:N)) grid.newpage() grid.draw(g3) ``` gridExtra/inst/doc/arrangeGrob.R0000644000175100001440000000325612551155163016331 0ustar hornikusers## ----setup, echo=FALSE, results='hide'----------------------------------- library(knitr) opts_chunk$set(message=FALSE, fig.width=4, fig.height=3) ## ----basic--------------------------------------------------------------- library(gridExtra) library(grid) library(ggplot2) library(lattice) p <- qplot(1,1) p2 <- xyplot(1~1) r <- rectGrob(gp=gpar(fill="grey90")) t <- textGrob("text") grid.arrange(t, p, p2, r, ncol=2) ## ----annotations--------------------------------------------------------- gs <- lapply(1:9, function(ii) grobTree(rectGrob(gp=gpar(fill=ii, alpha=0.5)), textGrob(ii))) grid.arrange(grobs=gs, ncol=4, top="top label", bottom="bottom\nlabel", left="left label", right="right label") grid.rect(gp=gpar(fill=NA)) ## ----layout-------------------------------------------------------------- lay <- rbind(c(1,1,1,2,3), c(1,1,1,4,5), c(6,7,8,9,9)) grid.arrange(grobs = gs, layout_matrix = lay) ## ----sizes, fig.height=2------------------------------------------------- grid.arrange(grobs=gs[1:3], ncol=2, widths = 1:2, heights=unit(c(1,10), c("in", "mm"))) ## ----grob---------------------------------------------------------------- g1 <- arrangeGrob(grobs = gs, layout_matrix = t(lay)) g2 <- arrangeGrob(grobs = gs, layout_matrix = lay) grid.arrange(g1, g2, ncol=2) ## ----marrange------------------------------------------------------------ set.seed(123) pl <- lapply(1:11, function(.x) qplot(1:10, rnorm(10), main=paste("plot", .x))) ml <- marrangeGrob(pl, nrow=2, ncol=2) ## non-interactive use, multipage pdf ## ggsave("multipage.pdf", ml) ## interactive use; calling `dev.new` multiple times ml gridExtra/inst/doc/arrangeGrob.html0000644000175100001440000041476212551155163017104 0ustar hornikusers Basic usage

The grid package provides low-level functions to create graphical objects (grobs), and position them on a page in specific viewports. The gtable package introduced a higher-level layout scheme, arguably more amenable to user-level interaction. With the arrangeGrob/grid.arrange() pair of functions, gridExtra builds upon gtable to arrange multiple grobs on a page.

Basic usage

In this example we mix a few grobs and plots,

library(gridExtra)
library(grid)
library(ggplot2)
library(lattice)
p <- qplot(1,1)
p2 <- xyplot(1~1)
r <- rectGrob(gp=gpar(fill="grey90"))
t <- textGrob("text")
grid.arrange(t, p, p2, r, ncol=2)

plot of chunk basic

Title and/or annotations

gs <- lapply(1:9, function(ii) 
  grobTree(rectGrob(gp=gpar(fill=ii, alpha=0.5)), textGrob(ii)))
grid.arrange(grobs=gs, ncol=4, 
               top="top label", bottom="bottom\nlabel", 
               left="left label", right="right label")
grid.rect(gp=gpar(fill=NA))

plot of chunk annotations

Complex layouts

We can provide a matrix defining the layout,

lay <- rbind(c(1,1,1,2,3),
             c(1,1,1,4,5),
             c(6,7,8,9,9))
grid.arrange(grobs = gs, layout_matrix = lay)

plot of chunk layout

All cells are of equal size by default, but users may pass explicity widths and/or heights in any valid grid units, or as relative numbers (interpreted as null),

grid.arrange(grobs=gs[1:3], ncol=2, widths = 1:2, 
             heights=unit(c(1,10), c("in", "mm")))

plot of chunk sizes

Nested layouts with arrangeGrob

The grid.arrange() function draws on the device; for more complex layouts, we may want to store the gtable and combine it with other objects, e.g. forming nested layouts. To this end, use arrangeGrob(),

g1 <- arrangeGrob(grobs = gs, layout_matrix = t(lay))
g2 <- arrangeGrob(grobs = gs, layout_matrix = lay)
grid.arrange(g1, g2, ncol=2)

plot of chunk grob

Multiple pages output

Finally, we may want to place grobs on multiple pages; the marrangeGrob() function provides a convenient interface for this, also compatible with ggsave().

set.seed(123)
pl <- lapply(1:11, function(.x) 
             qplot(1:10, rnorm(10), main=paste("plot", .x)))
ml <- marrangeGrob(pl, nrow=2, ncol=2)
## non-interactive use, multipage pdf
## ggsave("multipage.pdf", ml)
## interactive use; calling `dev.new` multiple times
ml

plot of chunk marrange plot of chunk marrange plot of chunk marrange

gridExtra/inst/doc/arrangeGrob.rmd0000644000175100001440000000530712551155163016711 0ustar hornikusers--- title: "Arranging multiple grobs on a page" author: "Baptiste Auguie" date: '`r Sys.Date()`' vignette: > %\VignetteEngine{knitr::rmarkdown} %\VignetteIndexEntry{arrangeGrob: arranging multiple grobs on a page} output: knitr:::html_vignette: toc: yes --- ```{r setup, echo=FALSE, results='hide'} library(knitr) opts_chunk$set(message=FALSE, fig.width=4, fig.height=3) ``` The `grid` package provides low-level functions to create graphical objects (`grobs`), and position them on a page in specific `viewports`. The `gtable` package introduced a higher-level layout scheme, arguably more amenable to user-level interaction. With the `arrangeGrob/grid.arrange()` pair of functions, `gridExtra` builds upon `gtable` to arrange multiple grobs on a page. ## Basic usage In this example we mix a few grobs and plots, ```{r basic} library(gridExtra) library(grid) library(ggplot2) library(lattice) p <- qplot(1,1) p2 <- xyplot(1~1) r <- rectGrob(gp=gpar(fill="grey90")) t <- textGrob("text") grid.arrange(t, p, p2, r, ncol=2) ``` ## Title and/or annotations ```{r annotations} gs <- lapply(1:9, function(ii) grobTree(rectGrob(gp=gpar(fill=ii, alpha=0.5)), textGrob(ii))) grid.arrange(grobs=gs, ncol=4, top="top label", bottom="bottom\nlabel", left="left label", right="right label") grid.rect(gp=gpar(fill=NA)) ``` ## Complex layouts We can provide a matrix defining the layout, ```{r layout} lay <- rbind(c(1,1,1,2,3), c(1,1,1,4,5), c(6,7,8,9,9)) grid.arrange(grobs = gs, layout_matrix = lay) ``` All cells are of equal size by default, but users may pass explicity `widths` and/or `heights` in any valid grid units, or as relative numbers (interpreted as `null`), ```{r sizes, fig.height=2} grid.arrange(grobs=gs[1:3], ncol=2, widths = 1:2, heights=unit(c(1,10), c("in", "mm"))) ``` ## Nested layouts with `arrangeGrob` The `grid.arrange()` function draws on the device; for more complex layouts, we may want to store the gtable and combine it with other objects, e.g. forming nested layouts. To this end, use `arrangeGrob()`, ```{r grob} g1 <- arrangeGrob(grobs = gs, layout_matrix = t(lay)) g2 <- arrangeGrob(grobs = gs, layout_matrix = lay) grid.arrange(g1, g2, ncol=2) ``` ## Multiple pages output Finally, we may want to place grobs on multiple pages; the `marrangeGrob()` function provides a convenient interface for this, also compatible with `ggsave()`. ```{r marrange} set.seed(123) pl <- lapply(1:11, function(.x) qplot(1:10, rnorm(10), main=paste("plot", .x))) ml <- marrangeGrob(pl, nrow=2, ncol=2) ## non-interactive use, multipage pdf ## ggsave("multipage.pdf", ml) ## interactive use; calling `dev.new` multiple times ml ``` gridExtra/inst/doc/ngonGrob.R0000644000175100001440000000223612551155163015650 0ustar hornikusers## ----setup, echo=FALSE, results='hide'----------------------------------- library(knitr) opts_chunk$set(message=FALSE, fig.width=4, fig.height=3) ## ----basic--------------------------------------------------------------- library(gridExtra) library(grid) library(grid) N <- 5 xy <- polygon_regular(N)*2 # draw multiple polygons g <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), n=seq_len(N)+2, gp=gpar(fill=1:N)) grid.newpage() grid.draw(g) ## ----rotated------------------------------------------------------------- g2 <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), n=seq_len(N)+2, rho=seq_len(N), phase=0, angle=pi/(seq_len(N)+2), size=1:N+5, gp=gpar(fill=1:N)) grid.newpage() grid.draw(g2) ## ----ellipse------------------------------------------------------------- g3 <- ellipseGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), angle=-2*seq(0,N-1)*pi/N+pi/2, size=5, rho=1/3, gp=gpar(fill=1:N)) grid.newpage() grid.draw(g3) gridExtra/inst/doc/tableGrob.html0000644000175100001440000047073712551155163016560 0ustar hornikusers Basic usage

Tabular data is usually formatted outside the graphics device, e.g via LaTeX, or html tables. However, in some cases it may be convenient to display small tables alongside graphics. A couple of packages offer this possibility with base graphics (plotrix for instance); the gridExtra provides the pair of tableGrob/grid.table functions for this purpose.

Basic usage

library(gridExtra)
library(grid)
d <- head(iris[,1:3])
grid.table(d)

plot of chunk basic

Spacing

The spacing of each row/column is automatic, and will adjust to bigger cell contents. Plotmath notation may be used, with the parse=TRUE argument. Note that this is applied to individual strings of text, and reverts to standard text if parsing fails (this is useful when mixing multiline text with plotmath in different cells).

d[2,3] <- "this is very wwwwwide"
d[1,2] <- "this\nis\ntall"
colnames(d) <- c("alpha*integral(xdx,a,infinity)", 
                 "this text\nis high", 'alpha/beta')

tt <- ttheme_default(colhead=list(fg_params = list(parse=TRUE)))
grid.table(d, theme=tt)

plot of chunk annotations

Aesthetic formatting

The formatting is controlled by themes, which are nested lists of graphical parameters. See ttheme_default and ttheme_minimal for two built-in examples. Changing a few parameters at a time amounts to modifying the list with the new values.

tt1 <- ttheme_default()
tt2 <- ttheme_minimal()
tt3 <- ttheme_minimal(
  core=list(bg_params = list(fill = blues9, col=NA),
            fg_params=list(fontface=3)), 
  colhead=list(fg_params=list(col="navyblue")),
  rowhead=list(fg_params=list(col="navyblue", fontface=2L)))

grid.arrange(
  tableGrob(iris[1:4, 1:2], theme=tt1),
  tableGrob(iris[1:4, 1:2], theme=tt2),
  tableGrob(iris[1:4, 1:2], theme=tt3),
  nrow=1)

plot of chunk theme

Text justification

The text labels can be justified; the default is “centre” for the core, header, and row names. These settings can be adjusted by passing the relevant parameters of textGrob via the theme nested lists,

tt1 <- ttheme_default()
tt2 <- ttheme_default(core=list(fg_params=list(hjust=1, x=0.9)),
                      rowhead=list(fg_params=list(hjust=1, x=0.95)))
tt3 <- ttheme_default(core=list(fg_params=list(hjust=0, x=0.1)),
                      rowhead=list(fg_params=list(hjust=0, x=0)))
grid.arrange(
  tableGrob(mtcars[1:4, 1:2], theme=tt1),
  tableGrob(mtcars[1:4, 1:2], theme=tt2),
  tableGrob(mtcars[1:4, 1:2], theme=tt3),
  nrow=1)

plot of chunk justify

Further gtable processing and integration

Being based on gtable, the table can be further processed. In particular, we may edit the cell sizes to align with other content on the page.

g <- g2 <- tableGrob(iris[1:4, 1:3], cols = NULL, rows=NULL)
g2$widths <- unit(rep(1/ncol(g2), ncol(g2)), "npc")
grid.arrange(rectGrob(), rectGrob(), nrow=1)
grid.arrange(g, g2, nrow=1, newpage = FALSE)

plot of chunk sizes

Other grobs such as separating lines may be added.

g <- tableGrob(iris[1:4, 1:3], theme=ttheme_minimal())
separators <- replicate(ncol(g) - 2, 
                     segmentsGrob(x1 = unit(0,"npc")), 
                     simplify=FALSE)
g <- gtable::gtable_add_grob(g, grobs = separators, 
                     t = 1, b = nrow(g), l = seq_len(ncol(g)-2)+2)
grid.draw(g)

plot of chunk separators

We may also access and modify individual cells, e.g. to highlight a value.

g <- tableGrob(iris[1:4, 1:3])
find_cell <- function(table, row, col, name="core-fg"){
  l <- table$layout
  which(l$t==row & l$l==col & l$name==name)
}

ind <- find_cell(g, 3, 2, "core-fg")
ind2 <- find_cell(g, 2, 3, "core-bg")
g$grobs[ind][[1]][["gp"]] <- gpar(fontsize=15, fontface="bold")
g$grobs[ind2][[1]][["gp"]] <- gpar(fill="darkolivegreen1", col = "darkolivegreen4", lwd=5)
grid.draw(g)

plot of chunk highlight

Faster tables: an alternative grid function

The tableGrob function can be very slow; unfortunately this is the price to pay for its versatility and easier implementation. We use individual textGrob and rectGrob elements for each cell, instead of relying on the vectorised implementation of these functions. The reason is practical: it is much easier to place, measure, and customise individual grobs, than modify the graphical parameters and positions of a single vectorised grob. An alternative function is presented below, using this vectorised approach, but lacking many of the customisations of tableGrob.

grid.ftable <- function(d, padding = unit(4, "mm"), ...) {

  nc <- ncol(d)
  nr <- nrow(d)

  ## character table with added row and column names
  extended_matrix <- cbind(c("", rownames(d)),   
                           rbind(colnames(d),
                                 as.matrix(d)))

  ## string width and height
  w <- apply(extended_matrix, 2, strwidth, "inch")
  h <- apply(extended_matrix, 2, strheight, "inch")

  widths <- apply(w, 2, max)
  heights <- apply(h, 1, max)

  padding <- convertUnit(padding, unitTo = "in", valueOnly = TRUE)

  x <- cumsum(widths + padding) - 0.5 * padding
  y <- cumsum(heights + padding) - padding

  rg <- rectGrob(x = unit(x - widths/2, "in"), 
                 y = unit(1, "npc") - unit(rep(y, each = nc + 1), "in"), 
                 width = unit(widths + padding, "in"), 
                 height = unit(heights + padding, "in"))

  tg <- textGrob(c(t(extended_matrix)), x = unit(x - widths/2, "in"), 
                 y = unit(1, "npc") - unit(rep(y, each = nc + 1), "in"), 
                 just = "center")

  g <- gTree(children = gList(rg, tg), ..., 
             x = x, y = y, widths = widths, heights = heights)

  grid.draw(g)
  invisible(g)
}

grid.newpage()
grid.ftable(head(iris, 4), gp = gpar(fill = rep(c("grey90", "grey95"), each = 6)))

plot of chunk ftable

gridExtra/inst/doc/ngonGrob.html0000644000175100001440000014030112551155163016407 0ustar hornikusers Basic usage

The gridExtra package provides a basic implementation of regular polygons, ngonGrob()/grid.ngon, and a convenience function to draw ellipses, ellipseGrob()/grid.ellipse(). We illustrate below the basic usage of these vectorised functions.

Basic usage

library(gridExtra)
library(grid)
library(grid)
N <- 5
xy <- polygon_regular(N)*2

# draw multiple polygons
g <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"),
              unit(xy[,2],"cm") + unit(0.5,"npc"),
              n=seq_len(N)+2, gp=gpar(fill=1:N))

grid.newpage()
grid.draw(g)

plot of chunk basic

Rotated and stretched polygons

g2 <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"),
              unit(xy[,2],"cm") + unit(0.5,"npc"),
              n=seq_len(N)+2, rho=seq_len(N),
              phase=0, angle=pi/(seq_len(N)+2),
              size=1:N+5, gp=gpar(fill=1:N))

grid.newpage()
grid.draw(g2)

plot of chunk rotated

Ellipses

g3 <- ellipseGrob(unit(xy[,1],"cm") + unit(0.5,"npc"),
                  unit(xy[,2],"cm") + unit(0.5,"npc"),
                  angle=-2*seq(0,N-1)*pi/N+pi/2,
                  size=5, rho=1/3, gp=gpar(fill=1:N))

grid.newpage()
grid.draw(g3)

plot of chunk ellipse

gridExtra/inst/doc/tableGrob.rmd0000644000175100001440000001407212551155163016360 0ustar hornikusers--- title: "Displaying tables as grid graphics" author: "Baptiste Auguie" date: '`r Sys.Date()`' vignette: > %\VignetteEngine{knitr::rmarkdown} %\VignetteIndexEntry{tableGrob: displaying tables as grid graphics} output: knitr:::html_vignette: toc: yes --- ```{r setup, echo=FALSE, results='hide'} library(knitr) opts_chunk$set(message=FALSE, fig.width=4, fig.height=2) ``` Tabular data is usually formatted outside the graphics device, e.g via LaTeX, or html tables. However, in some cases it may be convenient to display *small* tables alongside graphics. A couple of packages offer this possibility with base graphics (`plotrix` for instance); the `gridExtra` provides the pair of `tableGrob/grid.table` functions for this purpose. ## Basic usage ```{r basic} library(gridExtra) library(grid) d <- head(iris[,1:3]) grid.table(d) ``` ## Spacing The spacing of each row/column is automatic, and will adjust to bigger cell contents. Plotmath notation may be used, with the `parse=TRUE` argument. Note that this is applied to individual strings of text, and reverts to standard text if parsing fails (this is useful when mixing multiline text with plotmath in different cells). ```{r annotations, fig.height=3} d[2,3] <- "this is very wwwwwide" d[1,2] <- "this\nis\ntall" colnames(d) <- c("alpha*integral(xdx,a,infinity)", "this text\nis high", 'alpha/beta') tt <- ttheme_default(colhead=list(fg_params = list(parse=TRUE))) grid.table(d, theme=tt) ``` ## Aesthetic formatting The formatting is controlled by *themes*, which are nested lists of graphical parameters. See `ttheme_default` and `ttheme_minimal` for two built-in examples. Changing a few parameters at a time amounts to modifying the list with the new values. ```{r theme, fig.width=8} tt1 <- ttheme_default() tt2 <- ttheme_minimal() tt3 <- ttheme_minimal( core=list(bg_params = list(fill = blues9, col=NA), fg_params=list(fontface=3)), colhead=list(fg_params=list(col="navyblue")), rowhead=list(fg_params=list(col="navyblue", fontface=2L))) grid.arrange( tableGrob(iris[1:4, 1:2], theme=tt1), tableGrob(iris[1:4, 1:2], theme=tt2), tableGrob(iris[1:4, 1:2], theme=tt3), nrow=1) ``` ## Text justification The text labels can be justified; the default is "centre" for the core, header, and row names. These settings can be adjusted by passing the relevant parameters of `textGrob` via the theme nested lists, ```{r justify, fig.width=8} tt1 <- ttheme_default() tt2 <- ttheme_default(core=list(fg_params=list(hjust=1, x=0.9)), rowhead=list(fg_params=list(hjust=1, x=0.95))) tt3 <- ttheme_default(core=list(fg_params=list(hjust=0, x=0.1)), rowhead=list(fg_params=list(hjust=0, x=0))) grid.arrange( tableGrob(mtcars[1:4, 1:2], theme=tt1), tableGrob(mtcars[1:4, 1:2], theme=tt2), tableGrob(mtcars[1:4, 1:2], theme=tt3), nrow=1) ``` ## Further gtable processing and integration Being based on `gtable`, the table can be further processed. In particular, we may edit the cell sizes to align with other content on the page. ```{r sizes, fig.width=8} g <- g2 <- tableGrob(iris[1:4, 1:3], cols = NULL, rows=NULL) g2$widths <- unit(rep(1/ncol(g2), ncol(g2)), "npc") grid.arrange(rectGrob(), rectGrob(), nrow=1) grid.arrange(g, g2, nrow=1, newpage = FALSE) ``` Other grobs such as separating lines may be added. ```{r separators} g <- tableGrob(iris[1:4, 1:3], theme=ttheme_minimal()) separators <- replicate(ncol(g) - 2, segmentsGrob(x1 = unit(0,"npc")), simplify=FALSE) g <- gtable::gtable_add_grob(g, grobs = separators, t = 1, b = nrow(g), l = seq_len(ncol(g)-2)+2) grid.draw(g) ``` We may also access and modify individual cells, e.g. to highlight a value. ```{r highlight} g <- tableGrob(iris[1:4, 1:3]) find_cell <- function(table, row, col, name="core-fg"){ l <- table$layout which(l$t==row & l$l==col & l$name==name) } ind <- find_cell(g, 3, 2, "core-fg") ind2 <- find_cell(g, 2, 3, "core-bg") g$grobs[ind][[1]][["gp"]] <- gpar(fontsize=15, fontface="bold") g$grobs[ind2][[1]][["gp"]] <- gpar(fill="darkolivegreen1", col = "darkolivegreen4", lwd=5) grid.draw(g) ``` ## Faster tables: an alternative grid function The `tableGrob` function can be very slow; unfortunately this is the price to pay for its versatility and easier implementation. We use individual `textGrob` and `rectGrob` elements for each cell, instead of relying on the vectorised implementation of these functions. The reason is practical: it is much easier to place, measure, and customise individual grobs, than modify the graphical parameters and positions of a single vectorised grob. An alternative function is presented below, using this vectorised approach, but lacking many of the customisations of `tableGrob`. ```{r ftable, fig.width=6} grid.ftable <- function(d, padding = unit(4, "mm"), ...) { nc <- ncol(d) nr <- nrow(d) ## character table with added row and column names extended_matrix <- cbind(c("", rownames(d)), rbind(colnames(d), as.matrix(d))) ## string width and height w <- apply(extended_matrix, 2, strwidth, "inch") h <- apply(extended_matrix, 2, strheight, "inch") widths <- apply(w, 2, max) heights <- apply(h, 1, max) padding <- convertUnit(padding, unitTo = "in", valueOnly = TRUE) x <- cumsum(widths + padding) - 0.5 * padding y <- cumsum(heights + padding) - padding rg <- rectGrob(x = unit(x - widths/2, "in"), y = unit(1, "npc") - unit(rep(y, each = nc + 1), "in"), width = unit(widths + padding, "in"), height = unit(heights + padding, "in")) tg <- textGrob(c(t(extended_matrix)), x = unit(x - widths/2, "in"), y = unit(1, "npc") - unit(rep(y, each = nc + 1), "in"), just = "center") g <- gTree(children = gList(rg, tg), ..., x = x, y = y, widths = widths, heights = heights) grid.draw(g) invisible(g) } grid.newpage() grid.ftable(head(iris, 4), gp = gpar(fill = rep(c("grey90", "grey95"), each = 6))) ``` gridExtra/NAMESPACE0000644000175100001440000000107212551061120013431 0ustar hornikusers# Generated by roxygen2 (4.1.1): do not edit by hand S3method(drawDetails,lattice) S3method(grid.draw,arrangelist) S3method(print,arrangelist) export(arrangeGrob) export(ellipseGrob) export(grid.arrange) export(grid.ellipse) export(grid.ngon) export(grid.table) export(marrangeGrob) export(ngonGrob) export(polygon_regular) export(tableGrob) export(ttheme_default) export(ttheme_minimal) import(grid) import(gtable) importFrom(grDevices,dev.interactive) importFrom(grDevices,dev.new) importFrom(grDevices,n2mfrow) importFrom(graphics,plot) importFrom(utils,modifyList) gridExtra/R/0000755000175100001440000000000012551155163012426 5ustar hornikusersgridExtra/R/arrangeGrob.r0000644000175100001440000002032212551147250015037 0ustar hornikusers##' @aliases grid.arrange arrangeGrob marrangeGrob ##' @title Arrange multiple grobs on a page ##' @description Set up a gtable layout to place multiple grobs on a page ##' @describeIn arrangeGrob return a grob without drawing ##' @param ... grobs, gtables, ggplot or trellis objects ##' @param grobs list of grobs ##' @param top optional string, or grob ##' @param bottom optional string, or grob ##' @param left optional string, or grob ##' @param right optional string, or grob ##' @param padding unit of length one, margin around annotations ##' @param as.table logical: bottom-left to top-right or top-left to bottom-right ##' @param layout_matrix optional layout ##' @param name argument of gtable ##' @param respect argument of gtable ##' @param clip argument of gtable ##' @param nrow argument of gtable ##' @param ncol argument of gtable ##' @param widths argument of gtable ##' @param heights argument of gtable ##' @param vp viewport ##' @return arrangeGrob returns a gtable ##' @import gtable ##' @import grid ##' @importFrom grDevices n2mfrow ##' @export ##' ##' @examples ##' library(grid) ##' grid.arrange(rectGrob(), rectGrob()) arrangeGrob <- function(..., grobs=list(...), layout_matrix, vp=NULL, name = "arrange", as.table=TRUE, respect = FALSE, clip = "off", nrow=NULL, ncol=NULL, widths = NULL, heights = NULL, top = NULL, bottom = NULL, left = NULL, right = NULL, padding = unit(0.5,"line")){ n <- length(grobs) ## logic for the layout # if nrow/ncol supplied, honour this # if not, use length of widths/heights, if supplied # if nothing supplied, work out sensible defaults ## nothing to be done but check inconsistency if (!is.null(ncol) && !is.null(widths)){ stopifnot(length(widths) == ncol) } if (!is.null(nrow) && !is.null(heights)){ stopifnot(length(heights) == nrow) } ## use widths/heights if supplied if (is.null(ncol) && !is.null(widths)){ ncol <- length(widths) } if (is.null(nrow) && !is.null(heights)){ nrow <- length(heights) } ## work out the missing one if(is.null(nrow) && !is.null(ncol)) { nrow <- ceiling(n/ncol) } if(is.null(ncol) && !is.null(nrow)) { ncol <- ceiling(n/nrow) } ## it may happen that sufficient info was passed, ## but incompatible with number of grobs (fewer cells) stopifnot(nrow*ncol >= n) ## last case: nothing exists if(is.null(nrow) && is.null(ncol) && is.null(widths) && is.null(heights)) { nm <- grDevices::n2mfrow(n) nrow = nm[1] ncol = nm[2] } ## debugging # message("nrow:", nrow, " ncol:", ncol) ## conversions inherit.ggplot <- unlist(lapply(grobs, inherits, what="ggplot")) inherit.trellis <- unlist(lapply(grobs, inherits, what="trellis")) if(any(inherit.ggplot)) { stopifnot(requireNamespace("ggplot2", quietly = TRUE)) toconv <- which(inherit.ggplot) grobs[toconv] <- lapply(grobs[toconv], ggplot2::ggplotGrob) } if(any(inherit.trellis)) { stopifnot(requireNamespace("lattice", quietly = TRUE)) toconv <- which(inherit.trellis) grobs[toconv] <- lapply(grobs[toconv], latticeGrob) } if(missing(layout_matrix)){ # default layout: one cell for each grob positions <- expand.grid(t = seq_len(nrow), l = seq_len(ncol)) positions$b <- positions$t positions$r <- positions$l if(as.table) # fill table by rows positions <- positions[order(positions$t),] positions <- positions[seq_along(grobs), ] # n might be < ncol*nrow } else { # a layout was supplied cells <- sort(unique(as.vector(layout_matrix))) ## left/right/top/bottom borders for given id range_cell <- function(ii){ ind <- which(layout_matrix == ii, arr.ind=TRUE) c(l=min(ind[,"col"]), r=max(ind[,"col"]), t=min(ind[,"row"]), b=max(ind[,"row"])) } positions <- data.frame(do.call(rbind, lapply(cells, range_cell))) ncol <- max(positions$r) nrow <- max(positions$b) } ## sizes if(is.null(widths)) widths <- unit(rep(1, ncol), "null") if(is.null(heights)) heights <- unit(rep(1,nrow), "null") ## lazy size specification as relative numbers if (!is.unit(widths)) widths <- unit(widths, "null") if (!is.unit(heights)) heights <- unit(heights, "null") ## build the gtable, similar steps to gtable_matrix gt <- gtable(name=name, respect = respect, heights = heights, widths = widths, vp=vp) gt <- gtable_add_grob(gt, grobs, t = positions$t, b = positions$b, l = positions$l, r = positions$r, z = seq_along(grobs), clip = clip) ## titles given as strings are converted to text grobs if(is.character(top)){ top <- textGrob(top) } if(is.grob(top)){ h <- grobHeight(top) + padding gt <- gtable_add_rows(gt, heights=h, 0) gt <- gtable_add_grob(gt, top, t=1, l=1, r=ncol(gt), z=Inf, clip = clip) } if(is.character(bottom)){ bottom <- textGrob(bottom) } if(is.grob(bottom)){ h <- grobHeight(bottom) + padding gt <- gtable_add_rows(gt, heights = h, -1) gt <- gtable_add_grob(gt, bottom, t=nrow(gt), l=1, r=ncol(gt), z=Inf, clip = clip) } if(is.character(left)){ left <- textGrob(left, rot = 90) } if(is.grob(left)){ w <- grobWidth(left) + padding gt <- gtable_add_cols(gt, widths=w, 0) gt <- gtable_add_grob(gt, left, t=1, b=nrow(gt), l=1, r=1, z=Inf, clip = clip) } if(is.character(right)){ right <- textGrob(right, rot = -90) } if(is.grob(right)){ w <- grobWidth(right) + padding gt <- gtable_add_cols(gt, widths=w, -1) gt <- gtable_add_grob(gt, right, t=1, b=nrow(gt), l=ncol(gt), r=ncol(gt), z=Inf, clip = clip) } gt } ##' @describeIn arrangeGrob draw on the current device ##' @param newpage open a new page ##' @inheritParams arrangeGrob ##' @export grid.arrange <- function(..., newpage=TRUE){ if(newpage) grid.newpage() g <- arrangeGrob(...) grid.draw(g) invisible(g) } ##' @describeIn arrangeGrob interface to arrangeGrob that can dispatch on multiple pages ##' @details Using marrangeGrob, if the layout specifies both nrow and ncol, the list of grobs can be split into multiple pages. On interactive devices print open new windows, whilst non-interactive devices such as pdf call grid.newpage() between the drawings. ##' @return marrangeGrobm returns a list of class arrangelist ##' @export ##' @examples ##' \dontrun{ ##' library(ggplot2) ##' pl <- lapply(1:11, function(.x) qplot(1:10,rnorm(10), main=paste("plot",.x))) ##' ml <- marrangeGrob(pl, nrow=2, ncol=2) ##' ## interactive use; open new devices ##' ml ##' ## non-interactive use, multipage pdf ##' ggsave("multipage.pdf", ml) ##' } marrangeGrob <- function(grobs, ncol, nrow, ..., top = quote(paste("page", g, "of", pages))){ n <- length(grobs) nlay <- nrow*ncol pages <- n %/% nlay + as.logical(n %% nlay) groups <- split(seq_along(grobs), gl(pages, nlay, n)) pl <- vector(mode = "list", length = pages) for(page in seq_along(groups)){ g <- page params <- modifyList(list(...), list(top=eval(top), nrow=nrow, ncol=ncol)) pl[[g]] <- do.call(arrangeGrob, c(grobs[groups[[g]]], params)) } class(pl) <- c("arrangelist", class(pl)) pl } ##' @noRd ##' @importFrom grDevices dev.interactive dev.new ##' @export grid.draw.arrangelist = function(x, ...) lapply(x, function(.x) { if(dev.interactive()) dev.new() else grid.newpage() grid.draw(.x) }, ...) ##' @noRd ##' @importFrom grDevices dev.interactive dev.new ##' @export print.arrangelist = function(x, ...) lapply(x, function(.x) { if(dev.interactive()) dev.new() else grid.newpage() grid.draw(.x) }, ...) gridExtra/R/ngonGrob.r0000644000175100001440000001057312551037123014365 0ustar hornikusers##' @aliases ngonGrob grid.ngon ellipseGrob grid.ellipse ##' @title Regular polygon grob ##' @description Regular polygons with optional rotation, stretching, and aesthetic attributes ##' @describeIn ngonGrob return a polygon grob ##' @param x x unit ##' @param y y unit ##' @param n number of vertices ##' @param size radius of circumscribing circle ##' @param phase angle in radians of first point relative to x axis ##' @param rho aspect ratio ##' @param angle angle of polygon in radians ##' @param position.units default units for the positions ##' @param size.units grid units for the sizes ##' @param gp gpar ##' @param ... further parameters passed to polygonGrob ##' @return grob ##' @export ##' @examples ##' library(grid) ##' N <- 5 ##' xy <- polygon_regular(N)*2 ##' ##' # draw multiple polygons ##' g <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), ##' unit(xy[,2],"cm") + unit(0.5,"npc"), ##' n = seq_len(N) + 2, gp=gpar(fill=1:N)) ##' ##' grid.newpage() ##' grid.draw(g) ##' ##' # rotated and stretched ##' g2 <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), ##' unit(xy[,2],"cm") + unit(0.5,"npc"), ##' n = seq_len(N) + 2, rho = seq_len(N), ##' phase = 0, angle=pi/(seq_len(N)+2), ##' size=1:N+5) ##' ##' grid.newpage() ##' grid.draw(g2) ##' ##' # ellipse ##' g3 <- ellipseGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), ##' unit(xy[,2],"cm") + unit(0.5,"npc"), ##' angle=-2*seq(0,N-1)*pi/5+pi/2, ##' size=5, rho=1/3) ##' ##' grid.newpage() ##' grid.draw(g3) ngonGrob <- function (x, y, n = 5, size = 5, phase = pi/2, angle = 0, rho = 1, gp = gpar(colour = "black", fill = NA, linejoin = "mitre"), ..., position.units = "npc", size.units="mm") { N <- length(x) stopifnot(length(y) == N) if (!is.unit(x)) x <- unit(x, position.units) if (!is.unit(y)) y <- unit(y, position.units) xv <- convertX(x, position.units, TRUE) yv <- convertY(y, position.units, TRUE) if (length(n) < N) n <- rep(n, length.out = N) if (length(size) < N) size <- rep(size, length.out = N) if (length(phase) < N) phase <- rep(phase, length.out = N) if (length(angle) < N) angle <- rep(angle, length.out = N) if (length(rho) < N) rho <- rep(rho, length.out = N) lngon <- mapply(polygon_regular, n = n, phase = phase, SIMPLIFY = FALSE) vertices <- sapply(lngon, nrow) # browser() stretch_rotate_move <- function(p, size, rho, angle, x, y){ central <- size * p %*% diag(c(1/sqrt(rho), sqrt(rho))) %*% rbind(c(cos(angle), -sin(angle)), c(sin(angle), cos(angle))) list(x = unit(central[,1], size.units) + unit(x, position.units), y = unit(central[,2], size.units) + unit(y, position.units)) } lxy <- mapply(stretch_rotate_move, p=lngon, size=size, rho=rho, angle=angle, x=xv, y=yv, SIMPLIFY = FALSE) allx <- do.call("unit.c", lapply(lxy, "[[", 1)) ally <- do.call("unit.c", lapply(lxy, "[[", 2)) polygonGrob(allx, ally, id.lengths = vertices, gp = gp, ...) } #' @describeIn ngonGrob draw a polygon grob on the current device #' @inheritParams ngonGrob #' @export grid.ngon <- function(...) { grid.draw(ngonGrob(...)) } #' @describeIn ngonGrob return an ellipse grob #' @inheritParams ngonGrob #' @export ellipseGrob <- function(x, y, size = 5, angle = pi/4, rho = 1, n = 50, gp = gpar(colour = "black", fill = NA, linejoin = "mitre"), ..., position.units = "npc", size.units="mm") { ngonGrob(x, y, n = n , phase = 0, size = size, angle = angle, rho = rho, gp = gp, position.units = position.units, size.units = size.units, ...) } #' @describeIn ngonGrob draw an ellipse grob #' @inheritParams ngonGrob #' @export grid.ellipse <- function(...) { grid.draw(ellipseGrob(...)) } #' @describeIn ngonGrob return the x,y coordinates of a regular polygon inscribed in the unit circle #' @inheritParams ngonGrob #' @export polygon_regular <- function(n = 5, phase = 0){ stopifnot(n > 2) cc <- exp(seq(0, n)*2i*pi/n) * exp(1i*(phase+pi/2)) cbind(Re(cc), Im(cc)) } gridExtra/R/gridExtra-package.r0000644000175100001440000000103612551140674016134 0ustar hornikusers#' @title Miscellaneous Functions for "Grid" Graphics #' @description Provides a number of user-level functions to work with "grid" graphics, notably to arrange multiple grid-based plots on a page, and draw tables. #' @name gridExtra-package #' @aliases gridExtra #' @docType package #' @author baptiste Auguie \email{baptiste.auguie@@gmail.com} #' @import gtable #' @import grid #' @references #' R Graphics by Paul Murrell (Chapman & Hall/CRC, August 2005) #' @keywords packagelibrary #' @seealso \code{\link{Grid}} #' function() NULL gridExtra/R/tableGrob.r0000644000175100001440000001637212551147541014524 0ustar hornikusers#' @aliases grid.table tableGrob ttheme_default, ttheme_minimal #' @title Graphical display of a textual table #' @describeIn tableGrob return a grob #' @description Create a gtable containing text grobs representing a character matrix #' @param d data.frame or matrix #' @param rows optional vector to specify row names #' @param cols optional vector to specify column names #' @param theme list of theme parameters #' @param ... further arguments to control the gtable #' @return a gtable #' @export #' @examples #' library(grid) #' d <- head(iris, 3) #' g <- tableGrob(d) #' grid.newpage() #' grid.draw(g) tableGrob <- function(d, rows=rownames(d), cols=colnames(d), theme = ttheme_default(), ...){ g <- gtable_table(d, name="core", fg_params = theme$core$fg_params, bg_params = theme$core$bg_params, padding=theme$core$padding, ...) if(!is.null(cols)){ gc <- gtable_table(t(cols), name="colhead", fg_params = theme$colhead$fg_params, bg_params = theme$colhead$bg_params, padding=theme$colhead$padding) g <- rbind_gtable(gc, g) } if(!is.null(rows)){ if(!is.null(cols)) # need to add dummy cell rows <- c("", rows) gr <- gtable_table(rows, name="rowhead", fg_params = theme$rowhead$fg_params, bg_params = theme$rowhead$bg_params, padding=theme$rowhead$padding) g <- cbind_gtable(gr, g) } g } #' @describeIn tableGrob draw a text table #' @inheritParams tableGrob #' @export grid.table <- function(...) grid.draw(tableGrob(...)) #' @describeIn tableGrob default theme for text tables #' @inheritParams tableGrob #' @importFrom utils modifyList ##' @export ttheme_default <- function(...){ core <- list(fg_fun = text_grob, fg_params = list(parse=FALSE, col="black"), bg_fun = rect_grob, bg_params = list(fill = c("grey95","grey90"), lwd=1.5, col="white"), padding = unit(c(4, 4), "mm")) colhead <- list(fg_fun = text_grob, fg_params = list(parse=FALSE, fontface=2L), bg_fun = rect_grob, bg_params = list(fill = c("grey80"), lwd=1.5, col="white"), padding = unit(c(4, 4), "mm")) rowhead <- list(fg_fun = text_grob, fg_params = list(parse=FALSE, fontface=3L), bg_fun = rect_grob, bg_params = list(fill=NA, col=NA), padding = unit(c(4, 4), "mm")) default <- list( core = core, colhead = colhead, rowhead= rowhead ) modifyList(default, list(...)) } #' @describeIn tableGrob minimalist theme for text tables #' @inheritParams tableGrob ##' @export ttheme_minimal <- function(...){ core <- list(fg_fun = text_grob, fg_params = list(parse=FALSE, col="black"), bg_fun = rect_grob, bg_params = list(fill = NA, col=NA), padding = unit(c(4, 4), "mm")) colhead <- list(fg_fun = text_grob, fg_params = list(parse=FALSE, fontface=2L), bg_fun = rect_grob, bg_params = list(fill = NA, col=NA), padding = unit(c(4, 4), "mm")) rowhead <- list(fg_fun = text_grob, fg_params = list(parse=FALSE, fontface=3L, hjust = 1, x = 0.95), bg_fun = rect_grob, bg_params = list(fill=NA, col=NA), padding = unit(c(4, 4), "mm")) default <- list( core = core, colhead = colhead, rowhead= rowhead ) modifyList(default, list(...)) } text_grob <- function(label, parse=FALSE, col = "black", fontsize = 12, cex = 1, fontfamily = "", fontface = 1L, lineheight = 1.2, alpha = 1, rot = 0, just = "centre", hjust = 0.5, vjust = 0.5, x = 0.5, y = 0.5){ if(parse){ label <- tryCatch(parse(text=label), error = function(e) label) } textGrob(label = label, x = x, y = y, just = just, hjust = hjust, vjust = vjust, rot = rot, gp = gpar(col = col, fontsize = fontsize, cex = cex, fontfamily = fontfamily, fontface = fontface, lineheight = lineheight, alpha = alpha)) } rect_grob <- function(fill = "white", col = "black", lty = "solid", lwd = 1, cex = 1, alpha = 1, lineend = "round", linejoin = "round", linemitre = 10, lex = 1){ rectGrob(width = unit(1,"npc") - unit(2, "scaledpts"), height = unit(1,"npc") - unit(2, "scaledpts"), gp = gpar(col = col, fill = fill, lty = lty, lwd = lwd, cex = cex, alpha = alpha, lineend = lineend, linejoin = linejoin, linemitre = linemitre, lex = lex, alpha = alpha)) } ## ## unexported helper functions ## gtable_table <- function(d, widths, heights, fg_fun = text_grob, fg_params = list(), bg_fun = rect_grob, bg_params = list(), padding = unit(c(4, 4), "mm"), name = "table", vp = NULL){ label_matrix <- as.matrix(d) nc <- ncol(label_matrix) nr <- nrow(label_matrix) n <- nc*nr fg_params <- lapply(fg_params, rep, length.out = n, each = nc) bg_params <- lapply(bg_params, rep, length.out = n, each = nc) fg_params <- data.frame(fg_params, label = as.vector(label_matrix), stringsAsFactors=FALSE) bg_params <- data.frame(bg_params, stringsAsFactors=FALSE) labels <- do.call(mapply, c(fg_params, list(FUN = fg_fun, SIMPLIFY=FALSE))) bkgds <- do.call(mapply, c(bg_params, list(FUN = bg_fun, SIMPLIFY=FALSE))) label_grobs <- matrix(labels, ncol = nc) ## some calculations of cell sizes if(missing(widths)) widths <- col_widths(label_grobs) + padding[1] if(missing(heights)) heights <- row_heights(label_grobs) + padding[2] ## place labels in a gtable g <- gtable_matrix(paste0(name, "-fg"), grobs=label_grobs, widths = widths, heights = heights, vp=vp) ## add the background g <- gtable_add_grob(g, bkgds, t=rep(seq_len(nr), each=nc), l=rep(seq_len(nc), nr), z=0, name=paste0(name, "-bg")) g } gridExtra/R/grob-utils.r0000644000175100001440000000615512550072616014707 0ustar hornikuserslatticeGrob <- function(p, ...){ grob(p=p, ..., cl="lattice") } #' @importFrom graphics plot #' @export drawDetails.lattice <- function(x, recording=FALSE){ stopifnot(requireNamespace("lattice", quietly = TRUE)) plot(x$p, newpage=FALSE) } row_heights <- function(m){ do.call(unit.c, apply(m, 1, function(l) max(do.call(unit.c, lapply(l, grobHeight))))) } col_widths <- function(m){ do.call(unit.c, apply(m, 2, function(l) max(do.call(unit.c, lapply(l, grobWidth))))) } insert.unit <- function (x, values, after = length(x)) { lengx <- length(x) if (lengx == 0) return(values) if (length(values) == 0) return(x) if (after <= 0) { unit.c(values, x) } else if (after >= lengx) { unit.c(x, values) } else { unit.c(x[1L:after], values, x[(after + 1L):lengx]) } } z_normalise <- function (x, i = 1) { x$layout$z <- rank(x$layout$z, ties.method = "first") + i - 1 x } z_arrange_gtables <- function (gtables, z) { if (length(gtables) != length(z)) { stop("'gtables' and 'z' must be the same length") } zmax <- 0 for (i in order(z)) { if (nrow(gtables[[i]]$layout) > 0) { gtables[[i]] <- z_normalise(gtables[[i]], zmax + 1) zmax <- max(gtables[[i]]$layout$z) } } gtables } rbind.gtable <- function(..., size = "max", z = NULL) { gtables <- list(...) if (!is.null(z)) { gtables <- z_arrange_gtables(gtables, z) } Reduce(function(x, y) rbind_gtable(x, y, size = size), gtables) } rbind_gtable <- function(x, y, size = "max") { stopifnot(ncol(x) == ncol(y)) if (nrow(x) == 0) return(y) if (nrow(y) == 0) return(x) y$layout$t <- y$layout$t + nrow(x) y$layout$b <- y$layout$b + nrow(x) x$layout <- rbind(x$layout, y$layout) x$heights <- insert.unit(x$heights, y$heights) x$rownames <- c(x$rownames, y$rownames) size <- match.arg(size, c("first", "last", "max", "min")) x$widths <- switch(size, first = x$widths, last = y$widths, min = unit.pmin(x$widths, y$widths), max = unit.pmax(x$widths, y$widths) ) x$grobs <- append(x$grobs, y$grobs) x } cbind.gtable <- function(..., size = "max", z = NULL) { gtables <- list(...) if (!is.null(z)) { gtables <- z_arrange_gtables(gtables, z) } Reduce(function(x, y) cbind_gtable(x, y, size = size), gtables) } cbind_gtable <- function(x, y, size = "max") { stopifnot(nrow(x) == nrow(y)) if (ncol(x) == 0) return(y) if (ncol(y) == 0) return(x) y$layout$l <- y$layout$l + ncol(x) y$layout$r <- y$layout$r + ncol(x) x$layout <- rbind(x$layout, y$layout) x$widths <- insert.unit(x$widths, y$widths) x$colnames <- c(x$colnames, y$colnames) size <- match.arg(size, c("first", "last", "max", "min")) x$heights <- switch(size, first = x$heights, last = y$heights, min = unit.pmin(x$heights, y$heights), max = unit.pmax(x$heights, y$heights)) x$grobs <- append(x$grobs, y$grobs) x } gridExtra/vignettes/0000755000175100001440000000000012551155163014235 5ustar hornikusersgridExtra/vignettes/ngonGrob.rmd0000644000175100001440000000305212550700715016512 0ustar hornikusers--- title: "Regular polygons and ellipses in grid graphics" author: "Baptiste Auguie" date: '`r Sys.Date()`' vignette: > %\VignetteEngine{knitr::rmarkdown} %\VignetteIndexEntry{ngonGrob: regular polygons and ellipses in grid graphics} output: knitr:::html_vignette: toc: yes --- ```{r setup, echo=FALSE, results='hide'} library(knitr) opts_chunk$set(message=FALSE, fig.width=4, fig.height=3) ``` The `gridExtra` package provides a basic implementation of regular polygons, `ngonGrob()/grid.ngon`, and a convenience function to draw ellipses, `ellipseGrob()/grid.ellipse()`. We illustrate below the basic usage of these vectorised functions. ## Basic usage ```{r basic} library(gridExtra) library(grid) library(grid) N <- 5 xy <- polygon_regular(N)*2 # draw multiple polygons g <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), n=seq_len(N)+2, gp=gpar(fill=1:N)) grid.newpage() grid.draw(g) ``` ## Rotated and stretched polygons ```{r rotated} g2 <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), n=seq_len(N)+2, rho=seq_len(N), phase=0, angle=pi/(seq_len(N)+2), size=1:N+5, gp=gpar(fill=1:N)) grid.newpage() grid.draw(g2) ``` ## Ellipses ```{r ellipse} g3 <- ellipseGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), angle=-2*seq(0,N-1)*pi/N+pi/2, size=5, rho=1/3, gp=gpar(fill=1:N)) grid.newpage() grid.draw(g3) ``` gridExtra/vignettes/arrangeGrob.rmd0000644000175100001440000000530712550703342017174 0ustar hornikusers--- title: "Arranging multiple grobs on a page" author: "Baptiste Auguie" date: '`r Sys.Date()`' vignette: > %\VignetteEngine{knitr::rmarkdown} %\VignetteIndexEntry{arrangeGrob: arranging multiple grobs on a page} output: knitr:::html_vignette: toc: yes --- ```{r setup, echo=FALSE, results='hide'} library(knitr) opts_chunk$set(message=FALSE, fig.width=4, fig.height=3) ``` The `grid` package provides low-level functions to create graphical objects (`grobs`), and position them on a page in specific `viewports`. The `gtable` package introduced a higher-level layout scheme, arguably more amenable to user-level interaction. With the `arrangeGrob/grid.arrange()` pair of functions, `gridExtra` builds upon `gtable` to arrange multiple grobs on a page. ## Basic usage In this example we mix a few grobs and plots, ```{r basic} library(gridExtra) library(grid) library(ggplot2) library(lattice) p <- qplot(1,1) p2 <- xyplot(1~1) r <- rectGrob(gp=gpar(fill="grey90")) t <- textGrob("text") grid.arrange(t, p, p2, r, ncol=2) ``` ## Title and/or annotations ```{r annotations} gs <- lapply(1:9, function(ii) grobTree(rectGrob(gp=gpar(fill=ii, alpha=0.5)), textGrob(ii))) grid.arrange(grobs=gs, ncol=4, top="top label", bottom="bottom\nlabel", left="left label", right="right label") grid.rect(gp=gpar(fill=NA)) ``` ## Complex layouts We can provide a matrix defining the layout, ```{r layout} lay <- rbind(c(1,1,1,2,3), c(1,1,1,4,5), c(6,7,8,9,9)) grid.arrange(grobs = gs, layout_matrix = lay) ``` All cells are of equal size by default, but users may pass explicity `widths` and/or `heights` in any valid grid units, or as relative numbers (interpreted as `null`), ```{r sizes, fig.height=2} grid.arrange(grobs=gs[1:3], ncol=2, widths = 1:2, heights=unit(c(1,10), c("in", "mm"))) ``` ## Nested layouts with `arrangeGrob` The `grid.arrange()` function draws on the device; for more complex layouts, we may want to store the gtable and combine it with other objects, e.g. forming nested layouts. To this end, use `arrangeGrob()`, ```{r grob} g1 <- arrangeGrob(grobs = gs, layout_matrix = t(lay)) g2 <- arrangeGrob(grobs = gs, layout_matrix = lay) grid.arrange(g1, g2, ncol=2) ``` ## Multiple pages output Finally, we may want to place grobs on multiple pages; the `marrangeGrob()` function provides a convenient interface for this, also compatible with `ggsave()`. ```{r marrange} set.seed(123) pl <- lapply(1:11, function(.x) qplot(1:10, rnorm(10), main=paste("plot", .x))) ml <- marrangeGrob(pl, nrow=2, ncol=2) ## non-interactive use, multipage pdf ## ggsave("multipage.pdf", ml) ## interactive use; calling `dev.new` multiple times ml ``` gridExtra/vignettes/tableGrob.rmd0000644000175100001440000001407212551031506016640 0ustar hornikusers--- title: "Displaying tables as grid graphics" author: "Baptiste Auguie" date: '`r Sys.Date()`' vignette: > %\VignetteEngine{knitr::rmarkdown} %\VignetteIndexEntry{tableGrob: displaying tables as grid graphics} output: knitr:::html_vignette: toc: yes --- ```{r setup, echo=FALSE, results='hide'} library(knitr) opts_chunk$set(message=FALSE, fig.width=4, fig.height=2) ``` Tabular data is usually formatted outside the graphics device, e.g via LaTeX, or html tables. However, in some cases it may be convenient to display *small* tables alongside graphics. A couple of packages offer this possibility with base graphics (`plotrix` for instance); the `gridExtra` provides the pair of `tableGrob/grid.table` functions for this purpose. ## Basic usage ```{r basic} library(gridExtra) library(grid) d <- head(iris[,1:3]) grid.table(d) ``` ## Spacing The spacing of each row/column is automatic, and will adjust to bigger cell contents. Plotmath notation may be used, with the `parse=TRUE` argument. Note that this is applied to individual strings of text, and reverts to standard text if parsing fails (this is useful when mixing multiline text with plotmath in different cells). ```{r annotations, fig.height=3} d[2,3] <- "this is very wwwwwide" d[1,2] <- "this\nis\ntall" colnames(d) <- c("alpha*integral(xdx,a,infinity)", "this text\nis high", 'alpha/beta') tt <- ttheme_default(colhead=list(fg_params = list(parse=TRUE))) grid.table(d, theme=tt) ``` ## Aesthetic formatting The formatting is controlled by *themes*, which are nested lists of graphical parameters. See `ttheme_default` and `ttheme_minimal` for two built-in examples. Changing a few parameters at a time amounts to modifying the list with the new values. ```{r theme, fig.width=8} tt1 <- ttheme_default() tt2 <- ttheme_minimal() tt3 <- ttheme_minimal( core=list(bg_params = list(fill = blues9, col=NA), fg_params=list(fontface=3)), colhead=list(fg_params=list(col="navyblue")), rowhead=list(fg_params=list(col="navyblue", fontface=2L))) grid.arrange( tableGrob(iris[1:4, 1:2], theme=tt1), tableGrob(iris[1:4, 1:2], theme=tt2), tableGrob(iris[1:4, 1:2], theme=tt3), nrow=1) ``` ## Text justification The text labels can be justified; the default is "centre" for the core, header, and row names. These settings can be adjusted by passing the relevant parameters of `textGrob` via the theme nested lists, ```{r justify, fig.width=8} tt1 <- ttheme_default() tt2 <- ttheme_default(core=list(fg_params=list(hjust=1, x=0.9)), rowhead=list(fg_params=list(hjust=1, x=0.95))) tt3 <- ttheme_default(core=list(fg_params=list(hjust=0, x=0.1)), rowhead=list(fg_params=list(hjust=0, x=0))) grid.arrange( tableGrob(mtcars[1:4, 1:2], theme=tt1), tableGrob(mtcars[1:4, 1:2], theme=tt2), tableGrob(mtcars[1:4, 1:2], theme=tt3), nrow=1) ``` ## Further gtable processing and integration Being based on `gtable`, the table can be further processed. In particular, we may edit the cell sizes to align with other content on the page. ```{r sizes, fig.width=8} g <- g2 <- tableGrob(iris[1:4, 1:3], cols = NULL, rows=NULL) g2$widths <- unit(rep(1/ncol(g2), ncol(g2)), "npc") grid.arrange(rectGrob(), rectGrob(), nrow=1) grid.arrange(g, g2, nrow=1, newpage = FALSE) ``` Other grobs such as separating lines may be added. ```{r separators} g <- tableGrob(iris[1:4, 1:3], theme=ttheme_minimal()) separators <- replicate(ncol(g) - 2, segmentsGrob(x1 = unit(0,"npc")), simplify=FALSE) g <- gtable::gtable_add_grob(g, grobs = separators, t = 1, b = nrow(g), l = seq_len(ncol(g)-2)+2) grid.draw(g) ``` We may also access and modify individual cells, e.g. to highlight a value. ```{r highlight} g <- tableGrob(iris[1:4, 1:3]) find_cell <- function(table, row, col, name="core-fg"){ l <- table$layout which(l$t==row & l$l==col & l$name==name) } ind <- find_cell(g, 3, 2, "core-fg") ind2 <- find_cell(g, 2, 3, "core-bg") g$grobs[ind][[1]][["gp"]] <- gpar(fontsize=15, fontface="bold") g$grobs[ind2][[1]][["gp"]] <- gpar(fill="darkolivegreen1", col = "darkolivegreen4", lwd=5) grid.draw(g) ``` ## Faster tables: an alternative grid function The `tableGrob` function can be very slow; unfortunately this is the price to pay for its versatility and easier implementation. We use individual `textGrob` and `rectGrob` elements for each cell, instead of relying on the vectorised implementation of these functions. The reason is practical: it is much easier to place, measure, and customise individual grobs, than modify the graphical parameters and positions of a single vectorised grob. An alternative function is presented below, using this vectorised approach, but lacking many of the customisations of `tableGrob`. ```{r ftable, fig.width=6} grid.ftable <- function(d, padding = unit(4, "mm"), ...) { nc <- ncol(d) nr <- nrow(d) ## character table with added row and column names extended_matrix <- cbind(c("", rownames(d)), rbind(colnames(d), as.matrix(d))) ## string width and height w <- apply(extended_matrix, 2, strwidth, "inch") h <- apply(extended_matrix, 2, strheight, "inch") widths <- apply(w, 2, max) heights <- apply(h, 1, max) padding <- convertUnit(padding, unitTo = "in", valueOnly = TRUE) x <- cumsum(widths + padding) - 0.5 * padding y <- cumsum(heights + padding) - padding rg <- rectGrob(x = unit(x - widths/2, "in"), y = unit(1, "npc") - unit(rep(y, each = nc + 1), "in"), width = unit(widths + padding, "in"), height = unit(heights + padding, "in")) tg <- textGrob(c(t(extended_matrix)), x = unit(x - widths/2, "in"), y = unit(1, "npc") - unit(rep(y, each = nc + 1), "in"), just = "center") g <- gTree(children = gList(rg, tg), ..., x = x, y = y, widths = widths, heights = heights) grid.draw(g) invisible(g) } grid.newpage() grid.ftable(head(iris, 4), gp = gpar(fill = rep(c("grey90", "grey95"), each = 6))) ``` gridExtra/README.md0000644000175100001440000000131512550167612013505 0ustar hornikusers[![DOI](https://zenodo.org/badge/5734/baptiste/gridextra.png)](http://dx.doi.org/10.5281/zenodo.11422) [![Build Status](https://travis-ci.org/baptiste/gridextra.svg?branch=master)](https://travis-ci.org/baptiste/gridextra) [![](http://www.r-pkg.org/badges/version/gridExtra)](http://cran.rstudio.com/web/packages/gridExtra/index.html) [![](http://cranlogs.r-pkg.org/badges/grand-total/gridExtra)](http://cran.rstudio.com/web/packages/gridExtra/index.html) Provides a number of user-level functions to work with "grid" graphics, notably to arrange multiple grid-based plots on a page, and draw tables. Earlier versions of the package contained more experimental graphical objects (grobs), that have now been removed.gridExtra/MD50000644000175100001440000000313012551253646012537 0ustar hornikusers170972406943f42756c9fa6ffc1e422f *DESCRIPTION a7754e546ee14a98c5021dc4e6957a88 *NAMESPACE 7db5232c72d337ff3b2b9337906ecabb *R/arrangeGrob.r 22e6d58f19b3df08f6d7b740100035b1 *R/gridExtra-package.r a141338e85da6557c82317af333108f9 *R/grob-utils.r bd51a8b649a49a3ea5399f2a9279323f *R/ngonGrob.r 0394e29fdb36b8e28209880d117cd509 *R/tableGrob.r 0f791edfac076bcb24a3fb6932271d86 *README.md 62da238e97b31431264b3f20a43e891d *TODO 1fbbbb5a43659e082d472263a04c4035 *build/vignette.rds e5ad57d16624d3c9bbe81483d42cfbd6 *inst/NEWS.md 975cac45b0dec63649eebc5f4af48051 *inst/doc/arrangeGrob.R dde089703558e7754207fcf10c77a07d *inst/doc/arrangeGrob.html 971bfe5b59b8317dbfbbbbeadbf5744a *inst/doc/arrangeGrob.rmd eb82672ccaa50856f92d457aee242332 *inst/doc/ngonGrob.R 10af0beb4250dfba6f484debdfcf1c64 *inst/doc/ngonGrob.html 9d209d89a159ec7a45cd1a5a8c71b6d6 *inst/doc/ngonGrob.rmd b1e08e2fe8ada609fdf2cb11236d5c17 *inst/doc/tableGrob.R 5c78ff7b9e664d4acee408bfe84848eb *inst/doc/tableGrob.html 399b49ca8729645e640efb0e9a0545cd *inst/doc/tableGrob.rmd 618723bd19c005badd6924545e8b0d06 *inst/tests/testthat.r 1c6c2aa851549cbd04b6fa0f8f778f5c *inst/tests/testthat/test-arrangeGrob.R 707773b71f1e28d5e38031757aa2a1d5 *inst/tests/testthat/test-tableGrob.R 7648283d17c4e6b5331fb1f564308443 *man/arrangeGrob.Rd 486bff38ccd3fcb185787160d0a33dc5 *man/gridExtra-package.Rd 7d9ad03456af2729afed995b79bc18e6 *man/ngonGrob.Rd 74d71f2d764b6025c5fe88bd1b4e0423 *man/tableGrob.Rd 971bfe5b59b8317dbfbbbbeadbf5744a *vignettes/arrangeGrob.rmd 9d209d89a159ec7a45cd1a5a8c71b6d6 *vignettes/ngonGrob.rmd 399b49ca8729645e640efb0e9a0545cd *vignettes/tableGrob.rmd gridExtra/build/0000755000175100001440000000000012551155163013324 5ustar hornikusersgridExtra/build/vignette.rds0000644000175100001440000000047412551155163015670 0ustar hornikusers‹…’¿NÃ0ÆÝ&-4¨Ôð V”¹* ªº^k“ZrËNUuãÅ!\R;ÿ„ij¿Ï¹ßÝ9ÙE„) ¦Ü+ s|–µ !YàúÆ€Êø«)ö‰É™³ïUV¨‘—°—Ý‹CÎs“Ò«*£ùI–BKN3<°´P¨†Œ»´_'¥†g' †êB^д£\J¡-·T($†ôQ¬Ëj{J)VK¸ÔU v”4ìyÙŸýXæÒÚß3ºéwˆŠû¨­3£–㻲E¹jWùcýç<Ä/K&=Ss?áÜ™áFHÕ³Q¶"x_oÜvâ›»YsÍóW{ûÆ/ç Z˜âœøbqý‹}a¨ªê{ÜÑA‚õy3bPBòi0ÕÏ/܃ñ‚¤gridExtra/DESCRIPTION0000644000175100001440000000167212551253646013746 0ustar hornikusersPackage: gridExtra Authors@R: c(person("Baptiste", "Auguie", email = "baptiste.auguie@gmail.com", role = c("aut", "cre")), person("Anton", "Antonov", email = "tonytonov@gmail.com", role = c("ctb"))) License: GPL (>= 2) Title: Miscellaneous Functions for "Grid" Graphics Type: Package Description: Provides a number of user-level functions to work with "grid" graphics, notably to arrange multiple grid-based plots on a page, and draw tables. Version: 2.0.0 URL: https://github.com/baptiste/gridextra VignetteBuilder: knitr Imports: gtable, grid, grDevices, graphics, utils Suggests: ggplot2, lattice, knitr, testthat Collate: grob-utils.r arrangeGrob.r gridExtra-package.r tableGrob.r ngonGrob.r NeedsCompilation: no Packaged: 2015-07-14 09:46:27 UTC; baptiste Author: Baptiste Auguie [aut, cre], Anton Antonov [ctb] Maintainer: Baptiste Auguie Repository: CRAN Date/Publication: 2015-07-14 20:40:38 gridExtra/man/0000755000175100001440000000000012551155163013000 5ustar hornikusersgridExtra/man/tableGrob.Rd0000644000175100001440000000211612551147545015175 0ustar hornikusers% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/tableGrob.r \name{tableGrob} \alias{grid.table} \alias{tableGrob} \alias{ttheme_default} \alias{ttheme_default,} \alias{ttheme_minimal} \title{Graphical display of a textual table} \usage{ tableGrob(d, rows = rownames(d), cols = colnames(d), theme = ttheme_default(), ...) grid.table(...) ttheme_default(...) ttheme_minimal(...) } \arguments{ \item{d}{data.frame or matrix} \item{rows}{optional vector to specify row names} \item{cols}{optional vector to specify column names} \item{theme}{list of theme parameters} \item{...}{further arguments to control the gtable} } \value{ a gtable } \description{ Create a gtable containing text grobs representing a character matrix } \section{Functions}{ \itemize{ \item \code{tableGrob}: return a grob \item \code{grid.table}: draw a text table \item \code{ttheme_default}: default theme for text tables \item \code{ttheme_minimal}: minimalist theme for text tables }} \examples{ library(grid) d <- head(iris, 3) g <- tableGrob(d) grid.newpage() grid.draw(g) } gridExtra/man/arrangeGrob.Rd0000644000175100001440000000443112551147257015527 0ustar hornikusers% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/arrangeGrob.r \name{arrangeGrob} \alias{arrangeGrob} \alias{grid.arrange} \alias{marrangeGrob} \title{Arrange multiple grobs on a page} \usage{ arrangeGrob(..., grobs = list(...), layout_matrix, vp = NULL, name = "arrange", as.table = TRUE, respect = FALSE, clip = "off", nrow = NULL, ncol = NULL, widths = NULL, heights = NULL, top = NULL, bottom = NULL, left = NULL, right = NULL, padding = unit(0.5, "line")) grid.arrange(..., newpage = TRUE) marrangeGrob(grobs, ncol, nrow, ..., top = quote(paste("page", g, "of", pages))) } \arguments{ \item{...}{grobs, gtables, ggplot or trellis objects} \item{grobs}{list of grobs} \item{layout_matrix}{optional layout} \item{vp}{viewport} \item{name}{argument of gtable} \item{as.table}{logical: bottom-left to top-right or top-left to bottom-right} \item{respect}{argument of gtable} \item{clip}{argument of gtable} \item{nrow}{argument of gtable} \item{ncol}{argument of gtable} \item{widths}{argument of gtable} \item{heights}{argument of gtable} \item{top}{optional string, or grob} \item{bottom}{optional string, or grob} \item{left}{optional string, or grob} \item{right}{optional string, or grob} \item{padding}{unit of length one, margin around annotations} \item{newpage}{open a new page} } \value{ arrangeGrob returns a gtable marrangeGrobm returns a list of class arrangelist } \description{ Set up a gtable layout to place multiple grobs on a page } \details{ Using marrangeGrob, if the layout specifies both nrow and ncol, the list of grobs can be split into multiple pages. On interactive devices print open new windows, whilst non-interactive devices such as pdf call grid.newpage() between the drawings. } \section{Functions}{ \itemize{ \item \code{arrangeGrob}: return a grob without drawing \item \code{grid.arrange}: draw on the current device \item \code{marrangeGrob}: interface to arrangeGrob that can dispatch on multiple pages }} \examples{ library(grid) grid.arrange(rectGrob(), rectGrob()) \dontrun{ library(ggplot2) pl <- lapply(1:11, function(.x) qplot(1:10,rnorm(10), main=paste("plot",.x))) ml <- marrangeGrob(pl, nrow=2, ncol=2) ## interactive use; open new devices ml ## non-interactive use, multipage pdf ggsave("multipage.pdf", ml) } } gridExtra/man/gridExtra-package.Rd0000644000175100001440000000113012551140732016600 0ustar hornikusers% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/gridExtra-package.r \docType{package} \name{gridExtra-package} \alias{gridExtra} \alias{gridExtra-package} \title{Miscellaneous Functions for "Grid" Graphics} \description{ Provides a number of user-level functions to work with "grid" graphics, notably to arrange multiple grid-based plots on a page, and draw tables. } \author{ baptiste Auguie \email{baptiste.auguie@gmail.com} } \references{ R Graphics by Paul Murrell (Chapman & Hall/CRC, August 2005) } \seealso{ \code{\link{Grid}} } \keyword{packagelibrary} gridExtra/man/ngonGrob.Rd0000644000175100001440000000446712551037130015046 0ustar hornikusers% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/ngonGrob.r \name{ngonGrob} \alias{ellipseGrob} \alias{grid.ellipse} \alias{grid.ngon} \alias{ngonGrob} \alias{polygon_regular} \title{Regular polygon grob} \usage{ ngonGrob(x, y, n = 5, size = 5, phase = pi/2, angle = 0, rho = 1, gp = gpar(colour = "black", fill = NA, linejoin = "mitre"), ..., position.units = "npc", size.units = "mm") grid.ngon(...) ellipseGrob(x, y, size = 5, angle = pi/4, rho = 1, n = 50, gp = gpar(colour = "black", fill = NA, linejoin = "mitre"), ..., position.units = "npc", size.units = "mm") grid.ellipse(...) polygon_regular(n = 5, phase = 0) } \arguments{ \item{x}{x unit} \item{y}{y unit} \item{n}{number of vertices} \item{size}{radius of circumscribing circle} \item{phase}{angle in radians of first point relative to x axis} \item{angle}{angle of polygon in radians} \item{rho}{aspect ratio} \item{gp}{gpar} \item{...}{further parameters passed to polygonGrob} \item{position.units}{default units for the positions} \item{size.units}{grid units for the sizes} } \value{ grob } \description{ Regular polygons with optional rotation, stretching, and aesthetic attributes } \section{Functions}{ \itemize{ \item \code{ngonGrob}: return a polygon grob \item \code{grid.ngon}: draw a polygon grob on the current device \item \code{ellipseGrob}: return an ellipse grob \item \code{grid.ellipse}: draw an ellipse grob \item \code{polygon_regular}: return the x,y coordinates of a regular polygon inscribed in the unit circle }} \examples{ library(grid) N <- 5 xy <- polygon_regular(N)*2 # draw multiple polygons g <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), n = seq_len(N) + 2, gp=gpar(fill=1:N)) grid.newpage() grid.draw(g) # rotated and stretched g2 <- ngonGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), n = seq_len(N) + 2, rho = seq_len(N), phase = 0, angle=pi/(seq_len(N)+2), size=1:N+5) grid.newpage() grid.draw(g2) # ellipse g3 <- ellipseGrob(unit(xy[,1],"cm") + unit(0.5,"npc"), unit(xy[,2],"cm") + unit(0.5,"npc"), angle=-2*seq(0,N-1)*pi/5+pi/2, size=5, rho=1/3) grid.newpage() grid.draw(g3) } gridExtra/.Rinstignore0000644000175100001440000000001112550064251014515 0ustar hornikuserstesting/