rgridgridextra

Shade the first column of the table with "grid.table"


I want values 7, 8, 9 and 10 to also appear shaded darker in the table below. I have used

grid.table(public)

enter image description here

enter image description here

enter image description here

myt <- ttheme_default(
        colhead = list(fg_params=list(col="black"),
                     bg_params=list(fill="grey")),
         rowhead = list(fg_params=list(col="black", fontface="bold",hjust=0.5, x=0.5),
                       bg_params=list(fill="grey")))



grid.table(cbind("X\\Y"=rownames(public)[1:4], public[1:4,1:4]),rows=NULL)

grid::grid.newpage()
grid.table(public[1:4,1:4], theme=myt)

NEW Now, I can't manage to make the first column bold

 myt <- ttheme_default(
            colhead = list(fg_params=list(col="black"),
                         bg_params=list(fill="grey")),
             rowhead = list(fg_params=list(col="black", fontface="bold",hjust=0.5, x=0.5),
                           bg_params=list(fill="grey")))
    
    
    
    
    grid::grid.newpage()
    grid.table(public[1:4,1:4], theme=myt)
    
    
    rs = tableGrob(cbind("X\\Y"=rownames(public)[1:4]), rows=NULL, theme=ttheme_default(core=list(bg_params=list(fill="grey"))))
    
    grid::grid.draw(gtable_combine(rs, tableGrob(public[1:4, 1:4], rows=NULL)))

enter image description here

enter image description here

Perfect with these codes:

grid::grid.newpage()
grid.table(public[1:4,1:4], theme=myt)

rs = tableGrob(cbind("X\\Y"=rownames(public)[1:4]), 
               rows=NULL, 
               theme=ttheme_default(core=list(bg_params=list(fill="grey"), 
                                              fg_params=list(fontface="bold"))))

grid::grid.draw(gtable_combine(rs, tableGrob(public[1:4, 1:4], rows=NULL)))

Solution

  • Those numbers are the row names (you can see them with grid.table(iris[1:4,1:4])), and so you need to change the relevant theme elements, which are the rowhead parameters.

    For example,

    library(gridExtra)
    
    # New theme paramters
    myt <- ttheme_default(
             rowhead = list(fg_params=list(col="white"),
                            bg_params=list(fill="red")))
    
    grid::grid.newpage()
    grid.table(iris[1:4,1:4], theme=myt)
    

    Details can be found in the package vignette


    From comments;

    Is it possible to put in the upper left square, X\Y and keep the shading in the first column?

    There are a few ways to do this. First, grab the row names and form a tableGrob and format this how you would like, the second join to the tableGrob with the data (note that the rows names are now suppressed with rows=NULL). e.g.

    # row names & formatting
    rs = tableGrob(data.frame("x\\y"=rownames(iris)[1:4], check.names=FALSE), rows=NULL, 
                   theme=ttheme_default(core=list(bg_params=list(fill="grey70"))))
    
    # combine and draw
    grid::grid.draw(
             gtable_combine(rs, tableGrob(iris[1:4, 1:4], rows=NULL))
             ) 
    

    From comments: Now, I cannot make the first column bold. I don't know where to put fontface="bold".

    rs = tableGrob(data.frame("x\\y"=rownames(iris)[1:4], check.names=FALSE), 
                   rows=NULL, 
                   theme=ttheme_default(core=list(bg_params=list(fill="grey70"), 
                                                  fg_params=list(fontface="bold"))))