Customizing font colour of one column for a tableGrob in R?

自作多情 提交于 2019-12-02 01:09:37

问题


I would like to customise the font colour of one particular column in my tableGrob.

Here is the original table, and this is what I would like the table to look like with the zeros in the fifth column changed to "white"

I have followed baptise's instructions here: How do I customize particular columns for a tableGrob in R? with no success.

Here is my simple dataframe:

count <- data.frame("day17" = c(17, 4, 4, 4, 3, 2), 
"day27" = c(27, 4, 5, 5, 5, 1), "day37" = c(37, 5, 5, 4, 4, 3), 
"day47" = c(47, 2, 1, 3, 0, 0), "day57" = c("Time (d)", 0, 0, 0, 0, 0))

Following baptiste's example above, I have tried to specify the colours for the fifth column:

colours <- matrix(c("black", "white", "white", "white", "white", "white"), ncol=1, nrow=nrow(count), byrow=FALSE)

and here is the code to produce the table:

table_theme <- ttheme_minimal(core = list(fg_params=list(col=(colours))))
grid.newpage() 
table <- tableGrob(count, theme = table_theme, rows=NULL, cols=NULL)  
grid.draw(table)

This code is still changing the colours on a row basis not on a column basis. Any help on this matter would be much appreciated.

I am new to stack-overflow and this is my first question, please forgive me if the answer is actually an error in the code like missing brackets etc!


回答1:


colours are recycled columnwise, so if you want different colours for different columns you need to pass a full matrix of colours, e.g.

colours <- matrix("black", nrow(count), ncol(count))
colours[2:nrow(colours), ncol(colours)] <- "white"


来源:https://stackoverflow.com/questions/38974476/customizing-font-colour-of-one-column-for-a-tablegrob-in-r

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