Two font faces in grid.tables

2019-07-25 22:30发布

问题:

I have created a grid.table object to display a dataframe in PowerBi, below there is my code:

library(reshape)
library(gridExtra)
library(grid)
mydf <- data.frame(id = c(1:5), value = c("A","B","C","D","E"))
mytheme <- ttheme_default(base_size = 10,
                          core=list(fg_params=list(hjust=0, x=0.01),
                                    bg_params=list(fill=c("white", "lightgrey"))))

grid.table(mydf,cols = NULL, theme = mytheme, rows = NULL)

and this is my output:

I would like to style the font of the output so that only the first column has the font in bold, does anyone knows how to achive this?

Thanks

回答1:

grid.table() is just a wrapper for grid.draw(tableGrob(...))

You can get your desired results with some Grob surgery:

library(grid)
library(gridExtra)

mydf <- data.frame(id = c(1:5), value = c("A","B","C","D","E"))

mytheme <- ttheme_default(base_size = 10, 
                          core = list(fg_params=list(hjust=0, x=0.01),
                                      bg_params=list(fill=c("white", "lightgrey"))))

Make the tableGrob:

tg <- tableGrob(mydf, cols = NULL, theme = mytheme, rows = NULL)

Edit the tableGrob (column 1 is first 5 slots):

for (i in 1:5) {
  tg$grobs[[i]] <- editGrob(tg$grobs[[i]], gp=gpar(fontface="bold"))
}

I like to use a new page for examples , but you can remove it since grid.table() doesn't use it either:

grid.newpage()
grid.draw(tg)