2

I'd like to plot a simple grid. Here and there I found that people use grid.table.

I have managed to display my data in the simplest way possible like so:

library("gridExtra")
cmSmall = 5
cmMed = 7
cmBig = 15

df = matrix(ncol=4,nrow=2)
df[1,] = c('Size',"small","medium","large")
df[2,] = c('cm',cmSmall, cmMed, cmBig)
df = as.data.frame(df)


grid.table(df, show.rownames=FALSE, show.colnames=FALSE)

This is how it looks:

Now, I would like the following:

  1. Put 'Size' and 'cm' in bold

  2. Add a title (with two rows) for this table (should I somehow add this to a plot?!)

Any idea how to do it?

Many thanks!

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
N.M
  • 685
  • 1
  • 9
  • 22
  • This can really help with item (2): http://stackoverflow.com/questions/11774703/adding-text-to-a-grid-table-plot – N.M Jan 08 '14 at 11:18

2 Answers2

4

You can do 1 in a hacky way by moving that column to the row names, displaying it and modifying the styling of the rownames:

grid.table(`rownames<-`(df[-1],df[[1]]), gpar.rowtext = gpar(fontface = "bold"),
 gpar.rowfill = gpar(fill = "grey95", col = "white"), show.colnames=FALSE)
James
  • 65,548
  • 14
  • 155
  • 193
  • +1 This is probably the simplest way to proceed at this stage; getting **gridExtra** to work with the new **grid** functionality requires too much hacking for your typical[so] answer. – Gavin Simpson Jan 07 '14 at 18:11
  • Thank you, James! This indeed solves item 1. Is there a simple way to add item 2? If gridExtra is the wrong way to go, I really don't mind trying something else... I am new to R and that's the first thing I found - might not be ideal... – N.M Jan 07 '14 at 21:27
  • Have a look at this question for the title: http://stackoverflow.com/q/10776139/269476 – James Jan 09 '14 at 08:25
1

This is more an extended comment than a solution, but exists to explain why part 1 of the question is quite difficult at the moment---

1 at least is actually very difficult and, as chance would have it, is discussed by Paul Murrell in the latest issue of the R Journal (PDF).

The problem boils down to this:

R> grid.table(df, show.rownames=FALSE, show.colnames=FALSE)
R> grid.ls()
GRID.table.1

At this point, grid.table() has created a table grob, which only gets completed with grobs that form the actual table at draw time. This is achieved via an appropriate drawDetails() method for the. But until the table is actually drawn, there is no way to edit the individual grobs that are used to draw the table; they don't exist yet.

R versions >= 3.0.0 introduced some new functions and a new way of working that would allow what you want, but grid.table() would need to be changed to use the new functionality in the grid package. Effectively this would boil down to writing a makeContent() method for the table grob, which could then be forced using grid.force() to populate it with the individual grobs need to draw the table. At that point you'd be able to use grid.edit() to make "Size" and "cm" bold.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453