edwin
edwin

Reputation: 71

How to add the grid in a table with white backround in r

I want to produce a table with the gridExtra-Package. This table should have a white background with grid pattern. The following table has white backround but no grid.

y<-iris[1:4,1:5]

hj <- matrix(c(0.90,  0.90, 0.90, 0.90, 0.90), ncol=ncol(y), nrow=nrow(y), byrow=TRUE)
x <- matrix(c( 0.90, 0.90, 0.90, 0.90, 0.90), ncol=ncol(y), nrow=nrow(y), byrow=TRUE)
colours <- matrix("white", nrow(y), ncol(y))
colours[1:nrow(colours), 1] <- "#e9f1e9"

tt1 <- ttheme_default(core=list(fg_params=list(hjust = as.vector(hj),
            x = as.vector(x), fontface=c(rep("plain",ncol(y)))), bg_params = list(fill =colours , col=NA)))
tab<-tableGrob(y, rows = NULL, theme = tt1)
grid.newpage()
grid.draw(tab)

In contrast this table has the desired grid but no white backround color.

g <- tableGrob(iris[1:4, 1:3])
grid.newpage()
grid.draw(g)

How to connect this two tables to have both?

Upvotes: 2

Views: 469

Answers (1)

Ian Campbell
Ian Campbell

Reputation: 24848

Is this what you're looking for? Change col= in the bg_params list to "black".

Edit

And now with swapped background colors. Per your comment.

library(grid)
library(gridExtra)
y<-iris[1:4,1:5]
hj <- matrix(c(0.90,  0.90, 0.90, 0.90, 0.90), ncol=ncol(y), nrow=nrow(y), byrow=TRUE)
x <- matrix(c( 0.90, 0.90, 0.90, 0.90, 0.90), ncol=ncol(y), nrow=nrow(y), byrow=TRUE)
colours <- matrix("#e9f1e9", nrow(y), ncol(y))
colours[1:nrow(colours), 1] <- "white"
tt1 <- ttheme_default(core=list(fg_params=list(hjust = as.vector(hj),
            x = as.vector(x), fontface=c(rep("plain",ncol(y)))), bg_params = list(fill =colours , col="black")))
tab<-tableGrob(y, rows = NULL, theme = tt1)
grid.newpage()
grid.draw(tab)

Plot

Upvotes: 2

Related Questions