Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

r - grid.table and tableGrob in gridExtra package

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...