Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grid.table and tableGrob in gridExtra package

Tags:

r

gridextra

I am trying to format the table using gridExtra package. The gridExtra package I have is 2.0 and R version is 3.2.1

I was going through answers here on stackoverflow about the formatting and the suggested options seem to work only with older version of the package. For example,

grid.table(data, h.even.alpha = 1, h.odd.alpha = 0, 
           v.even.alpha = 1, v.odd.alpha = 1, 
           gpar.corefill, gpar.coretext) 

All of these options are shown as "unused arguments" in the latest version.

Searching further, I found that in new gridExtra package, formatting is defined probably inside theme, example -

tt <- ttheme_default(core=list(fg_params=list(hjust=1, x=0.95)), 
                     colhead=list(fg_params=list(col="brown"))

and then doing

grid.table(data, theme=tt). 

What I could not found was how these options inside theme is defined and how all the formatting which was possible in older version can now be done.

In particular, I am looking to do -

  1. Left justification of columns
  2. commas for big.marks (10000 as 10,000)
  3. different row colors for even and odd row numbers
  4. column header color
  5. not showing row names (something like row.names=FALSE)
like image 532
ashishkul Avatar asked Aug 03 '15 20:08

ashishkul


1 Answers

This recent answer shows how to alter the parameters, and Baptiste gives a link to further examples. As you notice in your question, to alter the formatting you use the theme argument; you can see what parameters to alter by looking at the output of ttheme_default()

# New theme paramters
myt <- ttheme_default(
         # Use hjust and x to left justify the text
         # Alternate the row fill colours
                 core = list(fg_params=list(hjust = 1, x=1),
                             bg_params=list(fill=c("yellow", "pink"))),

         # Change column header to white text and red background
                 colhead = list(fg_params=list(col="white"),
                                bg_params=list(fill="red"))
 )

# Example data - create some large numbers  
dat <- mtcars[1:5,1:5]
dat$mpg <- dat$mpg*1000

grid.newpage()
grid.draw(tableGrob(format(dat, big.mark=","), theme=myt, rows=NULL))

The big.mark argument of format is used to add the comma separator, and rownames are removed using the rows=NULL argument.

enter image description here

like image 140
user20650 Avatar answered Oct 12 '22 04:10

user20650