There were a number of times this past semester that I wanted to highlight a part of a table to draw more attention to it.
One package that solves this problem pretty easily is gridExtra
. This is a package that allows you to highlight rows, columns, and cells within a display table.
An important note, however, is that you can’t see how the table looks until you knit it.
The below examples were re-used from GridExtra
help in R:
Basic tables look pretty good:
# NOTE THIS STUFF DOESN'T SHOW UNTIL YOU KNIT!
library(gridExtra)
library(grid)
d <- head(iris[,1:3])
grid.table(d)
Table with the last row hightlighted a different color:
t1 <- ttheme_default(core=list(
fg_params=list(fontface=c(rep("plain", 4), "bold.italic")),
bg_params = list(fill=c(rep(c("grey95", "grey90"),
length.out=4), "#6BAED6"),
alpha = rep(c(1,0.5), each=5))
))
grid.table(iris[1:5, 1:3], theme = t1)
g <- tableGrob(iris[1:4, 1:3])
find_cell <- function(table, row, col, name="core-fg"){
l <- table$layout
which(l$t==row & l$l==col & l$name==name)
}
Table with a single cell in bold and another single cell with green highlights:
ind <- find_cell(g, 3, 2, "core-fg")
ind2 <- find_cell(g, 2, 3, "core-bg")
g$grobs[ind][[1]][["gp"]] <- gpar(fontsize=15, fontface="bold")
g$grobs[ind2][[1]][["gp"]] <- gpar(fill="darkolivegreen1",
col = "darkolivegreen4", lwd=5)
grid.draw(g)
Very happy with this package and I encourage you to check it out!