私のtableGrob myTableの最初の列の背景色を設定したいと思います。私は、(row1、col1)の色を赤、(row2、col1)を青にしたいと思います。私は灰色の背景だけを見ている。 gridExtra_0.9.1、data.table_1.9.4、ggplot2_1.0.1設定tableGrobの背景色をR
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)
最新のgridExtraで試してください(構文は変更されましたが、wikiには多くの例があります) – baptiste