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.


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.


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


# install.packages("gridExtra", repos="")


One can customize several parameters,

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

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

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

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

  • 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)

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