0

Using the solution supplied by AkselA. in How to formatting numbers by column in a table (tableGrob) I've tried to increase the font size in the table.

From this website, I believe the solution is in the "Accessing existing grobs in the table" section using:

g$grobs[ind][[1]][["gp"]] <- gpar(fontsize=15, fontface="bold").

But when I try to change the code I keep getting errors.

user2946746
  • 1,740
  • 3
  • 21
  • 36
  • What errors are you receiving? Also what value does `ind` represent? – Matt Jewett May 30 '17 at 17:37
  • Oh very sorry. I didn't notice the website link didn't get uploaded as well. The 'ind' is from an example here: https://github.com/baptiste/gridextra/wiki/tableGrob I was trying to integrate that example to adjust the text size. – user2946746 May 30 '17 at 18:22
  • This code does not generate any errors for me. Could you be more specific as to which errors you are receiving? – Matt Jewett May 30 '17 at 18:52
  • I have no idea how to change that line of code to apply to the full table. It's used for an select cell. If you change it to g$grobs[][[]][["gp"]] <- gpar(fontsize=25, fontface="bold") i get Error in `*tmp*`[[]] : invalid subscript type 'symbol'. If I put g$grobs[][[1]][["gp"]] <- gpar(fontsize=25, fontface="bold") it doesn't give an error but the font size looks to be the same. – user2946746 May 30 '17 at 20:21

1 Answers1

0

I was able to figure out the answer to this question. I was able to use the solution provided by '42-' in Text alignment and font size in gtable

Changing his code fragment and adding it to the end of the referenced question.

g$grobs[] <- 
 lapply(g$grobs[], 
 function(x) modifyList( x, list(gp=list(fontsize=25, cex=1) ) ) )

The full code would be:

    library(gtable)
library(grid)
library(gridExtra)
library(zoo)

data(iris)
iris <- iris[1:4, 1:3]
rownames(iris) <- as.character(as.yearmon(
  seq(as.Date("2000/1/1"), as.Date("2000/4/1"), by = "month")))
iris$RankColumn <- 1:nrow(iris)

# a simple function to scale each row or column to the range [0, 1]
# will convert characters to numerics if in a sensible format
norm <- function(x, mar=2) {
    rnames <- rownames(x)
    x <- apply(x, 2, as.numeric)
    x <- apply(x, mar, function(y){(y-min(y))/(max(y)-min(y))})
    rownames(x) <- rnames
    x
}

# function to pad with zero
# by default does not pad integers
zeropad <- function(x, nz=1, exc.int=TRUE) {
    if (is.integer(x) & exc.int) {
        x
    } else { 
        sprintf(paste0("%.", nz, "f"), x)
    }
}

bluecol <- colorRamp(c("#3366EE", "#AABBFF", "#DDDDFF"))(norm(iris))
bluecol <- rgb(bluecol[, 1], bluecol[, 2], bluecol[, 3], max=255)

tt <- ttheme_default(core=list(bg_params=list(fill=bluecol)))

# convert floats to zero-padded characters
iris[1:ncol(iris)] <- sapply(iris, zeropad, 2)

g <- tableGrob(iris, theme=tt)
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 2, b = nrow(g), l = 1, r = ncol(g))
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 1, l = 1, r = ncol(g))


g$grobs[] <- 
   lapply(g$grobs[], 
     function(x) modifyList( x, list(gp=list(fontsize=25, cex=1) ) ) )


plot.new()
grid.draw(g)
user2946746
  • 1,740
  • 3
  • 21
  • 36