I wish to set background color of first column of my tableGrob myTable. I would like the color of (row1, col1) to be red and (row2, col1) to be blue. I am only seeing gray background. using gridExtra_0.9.1, data.table_1.9.4, ggplot2_1.0.1
library(ggplot2)
library(data.table)
library(gridExtra)
GetCellNumber <- function(table,irow,jcol) {
rows = NROW(table)
cellnumber <- (jcol)*(rows+1) + irow +1
cellnumber
}
ColorTable <- function(table, sdata,colors) {
for(irow in 1:NROW(sdata)) {
irowColor <- colors[irow]
jcol <- 1
cell <- GetCellNumber(sdata,irow,jcol)
table$lg$lgf[[cell]]$gp$fill <- irowColor
}
table
}
testTable <- function(dt) {
myTable <- tableGrob(dt, rows = NULL,
par.coretext = gpar(fontsize=10),
gpar.coltext = gpar(cex=0.7, fontface = "bold"),
gpar.coretext = gpar(cex=0.7) ,
gpar.colfill = gpar(fill="grey90", col="gray30", lwd=0.2),
gpar.corefill = gpar(fill="grey90", col="gray30", lwd=0.2),
show.rownames = FALSE)
myTable <- ColorTable(myTable, dt, c("red", "blue"))
myTable
}
dt <- data.frame(customer=c("yahoo", "cnn"), metricname=c("cpu","cpu"))
summary.table <- testTable(dt)
bottom.view <- arrangeGrob(summary.table, widths = c(1), ncol=1,
main= textGrob("test",
gp = gpar(fontsize=10, fontface="italic")))
print(bottom.view)