Two font faces in grid.tables

与世无争的帅哥 提交于 2019-12-24 09:09:53

问题


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)



来源:https://stackoverflow.com/questions/48266712/two-font-faces-in-grid-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!