Adding a table to a Grid plot

In base graphics, the textplot() function from the gplots package allows the user to add some text output in a plot. Methods are defined for character vectors and data.frames,...

The purpose of this page is to develop a similar function using the Grid graphics system.

Alternatives

One might want to consider other options,

  • use the gridBase package to use the textplot() function within Grid viewports. The alignment of the table is however difficult.
  • Use LaTeX (e.g. via Sweave) to produce the table.

Implementation

Note: this is an alpha version. Feel free to improve it.

Usage

library(gridExtra)
# install.packages("gridExtra", repos="http://R-Forge.R-project.org")
# http://code.google.com/p/gridextra/
 
grid.table(head(iris))

produces,

http://gridextra.googlecode.com/files/tableIris.png

One can customize several parameters,

  • text
grid.table(head(iris), gpar.coretext = gpar(fontsize=8,col="slateblue",fontface="italic"))

http://gridextra.googlecode.com/files/tableIrisText.png

  • fill
grid.table(head(iris), gpar.corefill = gpar(fill="slateblue",alpha=0.5), h.even.alpha = 0.5)

http://gridextra.googlecode.com/files/tableIrisFill.png

  • borders
grid.table(head(iris), gpar.colfill = gpar(fill=NA,col=NA), gpar.rowfill = gpar(fill=NA,col=NA), h.even.alpha = 0)

http://gridextra.googlecode.com/files/tableIrisBorder.png

  • automatic spacing
d = head(iris)
names(d)[2] <- "very tall\n name"
grid.table(d, equal.height=TRUE)

http://gridextra.googlecode.com/files/tableIrisSpacing.png

  • row and column names
e = expression(alpha,"testing very large width", hat(beta), integral(f(x)*dx, a, b), NA) 
 
grid.table(head(iris), cols = e, show.rownames = FALSE, rows=NULL)

http://gridextra.googlecode.com/files/tableIrisNames.png

 
tips/graphics-grid/table.txt · Last modified: 2009/10/01
 
Recent changes RSS feed R Wiki powered by Driven by DokuWiki and optimized for Firefox Creative Commons License