Kirsten Packer Kirsten Packer - 3 months ago 55
R Question

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

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!

Answer

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"