2

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)))
Amelia
  • 111
  • 8

1 Answers1

1

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"))))
user20650
  • 24,654
  • 5
  • 56
  • 91
  • [This](https://stackoverflow.com/questions/31796219/grid-table-and-tablegrob-in-gridextra-package/31798975#31798975) answer shows how to alternate colours ... or it may just find it easier to add the rownames as a column and let grid.table do it all for you – user20650 Apr 19 '23 at 09:29
  • 1
    It was perfect! – Amelia Apr 19 '23 at 11:02
  • And to make it even better, it is possible to put in the upper left square, X\Y? – Amelia Apr 19 '23 at 11:19
  • @Amelia ; quickest way is probably appending the rownames to the dataframe e.g. `grid.table(cbind("x\\y"=rownames(iris)[1:4], iris[1:4,1:4]), rows=NULL)` – user20650 Apr 19 '23 at 11:39
  • It looks very nice, but the shading in the first column disappears. I write above the codes and how it looks like. – Amelia Apr 19 '23 at 12:02
  • Now, I cannot make the first column bold. I don't know where to put fontface="bold". Everything is detailed above. – Amelia Apr 19 '23 at 14:55
  • @Amelia ; updated answer. fyi if you run `ttheme_default()` you will see the nested list structure of where/how to update parameters. (i assume those starting `fg` is foreground, and `bg` background) – user20650 Apr 19 '23 at 15:16
  • 1
    Works perfectly with rs = tableGrob(cbind("X"=rownames(public)[1:4]), rows=NULL, theme=ttheme_default(core=list(bg_params=list(fill="grey"), fg_params=list(fontface="bold")))) Thank you very much!, although it is not allowed to say it. – Amelia Apr 19 '23 at 15:28