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. |
Authors: | Baptiste Auguie [aut, cre], Anton Antonov [ctb] |
Maintainer: | Baptiste Auguie <[email protected]> |
License: | GPL (>= 2) |
Version: | 2.3 |
Built: | 2024-12-21 06:30:58 UTC |
Source: | CRAN |
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.
baptiste Auguie [email protected]
R Graphics by Paul Murrell (Chapman & Hall/CRC, August 2005)
Set up a gtable layout to place multiple grobs on a page.
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, layout_matrix = matrix(seq_len(nrow * ncol), nrow = nrow, ncol = ncol), top = quote(paste("page", g, "of", npages)))
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, layout_matrix = matrix(seq_len(nrow * ncol), nrow = nrow, ncol = ncol), top = quote(paste("page", g, "of", npages)))
... |
grobs, gtables, ggplot or trellis objects |
grobs |
list of grobs |
layout_matrix |
optional layout |
vp |
viewport |
name |
argument of gtable |
as.table |
logical: bottom-left to top-right (TRUE) or top-left to bottom-right (FALSE) |
respect |
argument of gtable |
clip |
argument of gtable |
nrow |
argument of gtable |
ncol |
argument of gtable |
widths |
argument of gtable |
heights |
argument of gtable |
top |
optional string, or grob |
bottom |
optional string, or grob |
left |
optional string, or grob |
right |
optional string, or grob |
padding |
unit of length one, margin around annotations |
newpage |
open a new page |
Using marrangeGrob, if the layout specifies both nrow and ncol, the list of grobs can be split into multiple pages. On interactive devices print opens new windows, whilst non-interactive devices such as pdf call grid.newpage() between the drawings.
arrangeGrob returns a gtable.
marrangeGrob returns a list of class arrangelist
arrangeGrob
: return a grob without drawing
grid.arrange
: draw on the current device
marrangeGrob
: interface to arrangeGrob that can dispatch on multiple pages
library(grid) grid.arrange(rectGrob(), rectGrob()) ## Not run: library(ggplot2) 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; open new devices ml ## End(Not run)
library(grid) grid.arrange(rectGrob(), rectGrob()) ## Not run: library(ggplot2) 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; open new devices ml ## End(Not run)
Combine gtables based on row/column names.
gtable_combine(..., along = 1L, join = "outer") combine(..., along = 1L, join = "outer")
gtable_combine(..., along = 1L, join = "outer") combine(..., along = 1L, join = "outer")
... |
gtables |
along |
dimension to align along, |
join |
when x and y have different names, how should the difference be resolved?
|
rbind gtables
cbind gtables
gtable_rbind(..., size = "max", z = NULL) gtable_cbind(..., size = "max", z = NULL)
gtable_rbind(..., size = "max", z = NULL) gtable_cbind(..., size = "max", z = NULL)
... |
gtables |
size |
how should the widths be calculated?
|
z |
optional z level |
Regular polygons with optional rotation, stretching, and aesthetic attributes.
ngonGrob(x, y, n = 5, size = 5, phase = pi/2, angle = 0, ar = 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, ar = 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)
ngonGrob(x, y, n = 5, size = 5, phase = pi/2, angle = 0, ar = 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, ar = 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)
x |
x unit |
y |
y unit |
n |
number of vertices |
size |
radius of circumscribing circle |
phase |
angle in radians of first point relative to x axis |
angle |
angle of polygon in radians |
ar |
aspect ratio |
gp |
gpar |
... |
further parameters passed to polygonGrob |
position.units |
default units for the positions |
size.units |
grid units for the sizes |
A grob.
ngonGrob
: return a polygon grob
grid.ngon
: draw a polygon grob on the current device
ellipseGrob
: return an ellipse grob
grid.ellipse
: draw an ellipse grob
polygon_regular
: return the x,y coordinates of a regular polygon inscribed in the unit circle
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, ar = 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, ar = 1/3) grid.newpage() grid.draw(g3)
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, ar = 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, ar = 1/3) grid.newpage() grid.draw(g3)
Create a gtable containing text grobs representing a character matrix.
tableGrob(d, rows = rownames(d), cols = colnames(d), theme = ttheme_default(), vp = NULL, ...) grid.table(...) ttheme_default(base_size = 12, base_colour = "black", base_family = "", parse = FALSE, padding = unit(c(4, 4), "mm"), ...) ttheme_minimal(base_size = 12, base_colour = "black", base_family = "", parse = FALSE, padding = unit(c(4, 4), "mm"), ...)
tableGrob(d, rows = rownames(d), cols = colnames(d), theme = ttheme_default(), vp = NULL, ...) grid.table(...) ttheme_default(base_size = 12, base_colour = "black", base_family = "", parse = FALSE, padding = unit(c(4, 4), "mm"), ...) ttheme_minimal(base_size = 12, base_colour = "black", base_family = "", parse = FALSE, padding = unit(c(4, 4), "mm"), ...)
d |
data.frame or matrix |
rows |
optional vector to specify row names |
cols |
optional vector to specify column names |
theme |
list of theme parameters |
vp |
optional viewport |
... |
further arguments to control the gtable |
base_size |
default font size |
base_colour |
default font colour |
base_family |
default font family |
parse |
logical, default behaviour for parsing text as plotmath |
padding |
length-2 unit vector specifying the horizontal and vertical padding of text within each cell |
A gtable.
tableGrob
: return a grob
grid.table
: draw a text table
ttheme_default
: default theme for text tables
ttheme_minimal
: minimalist theme for text tables
library(grid) d <- head(iris, 3) g <- tableGrob(d) grid.newpage() grid.draw(g)
library(grid) d <- head(iris, 3) g <- tableGrob(d) grid.newpage() grid.draw(g)