Christopher Peralta
Christopher Peralta

Reputation: 431

How can I store grobs in a list and pass them to grobTree()?

So I'm trying to make a list of grobs and then pass them into grobTree(), but my list items don't get read in as grobs by do.call().

Here's my code:

library(purrr)
library(grid)
library(gridExtra)
library(ggplot2)
qplot(displ, year, data = mpg)

title_segments <- c('Help ', 'me ', 'please', '!')
colors <- c('red', 'orange', 'green', 'blue')
nudge_x = 0

grobs <- NULL
grobs[1] <- list(gp = gpar(fontsize = 14, fontface = 'bold'))
grobs[2] <- list(textGrob(label = title_segments[1], name = "title1",  
                          x = unit(2.33 - nudge_x, "lines"), 
                          y = unit(-.5, "lines"), 
                          hjust = 0, vjust = 0, gp = gpar(col = colors[1])))

if(length(title_segments) > 1){ 
  x <- unit(2.24 - nudge_x, "lines")
  more_grobs <- pmap(list(title_segments[-1], colors[-1], 
seq_along(title_segments)[-1]), function(segment, color, i){
    grob <- textGrob(label = segment, name = paste0('title', i, sep = ''),
                          x = x + grobWidth(paste0('title', i - 1, sep = '')),     
                          y = unit(-.5, "lines"),
                          hjust = 0, vjust = 0, gp = gpar(col = color))
  })
}
grobs <- c(grobs, more_grobs)

grobs <- do.call(what = grobTree, args = grobs) ### ERROR HERE

# Turn off clipping and draw plot
gb <- ggplot_build(last_plot()) 
gt <- ggplot_gtable(gb) 
gt$layout$clip[gt$layout$name=="panel"] <- "off" 
gg <- arrangeGrob(gt, top = grobs, padding = unit(2.6, "line")) 
grid.newpage() 
grid.draw(gg) 

The error occurs when I get to the do.call() statement, because my list elements don't get read as grobs.

When I try this bit of code, then it evaluates to true.

var <- NULL
is.grob(var <- textGrob(label = title_segments[1], name = "title1",  
                      x = unit(2.33 - nudge_x, "lines"), 
                      y = unit(-.5, "lines"), 
                      hjust = 0, vjust = 0, gp = gpar(col = colors[1])))

When I try this bit, it evaluates to false

var2 <-NULL
var2[1] <- textGrob(label = title_segments[1], name = "title1",  
                      x = unit(2.33 - nudge_x, "lines"), 
                      y = unit(-.5, "lines"), 
                      hjust = 0, vjust = 0, gp = gpar(col = colors[1])))
is.grob(var2[1])

EDIT:: This is what I'm trying to achieve with the pmap function.

grobs <- grobTree(
gp = gpar(fontsize = 14, fontface = 'bold'),

textGrob(label = title_segments[1], name = "title1",
         x = unit(2.33 - nudge_x, "lines"),
         y = unit(-.5, "lines"),
         hjust = 0, vjust = 0, gp = gpar(col = colors[1])),

  if(length(title_segments) > 1){
    textGrob(label = title_segments[2], name = "title2",
           x = grobWidth("title1") + unit(2.24 - nudge_x, "lines"),
           y = unit(-.5, "lines"),
           hjust = 0, vjust = 0, gp = gpar(col = colors[2]))
  },

  if(length(title_segments) > 2){
    textGrob(label = title_segments[3], name = "title3",
           x = grobWidth("title1") + grobWidth("title2") + unit(2.24 - nudge_x, "lines"),
           y = unit(-.5, "lines"),
           hjust = 0, vjust = 0, gp = gpar(col = colors[3]))
  },
  if(length(title_segments) > 3){
    textGrob(label = title_segments[4], name = "title4",
           x = grobWidth("title1") + grobWidth("title2") + grobWidth("title3") +  unit(2.24 - nudge_x, "lines"),
           y = unit(-.5, "lines"),
           hjust = 0, vjust = 0, gp = gpar(col = colors[4]))
  },
  if(length(title_segments) > 4){
    textGrob(label = title_segments[5], name = "title5",
           x = grobWidth("title1") + grobWidth("title2") + grobWidth("title3") + grobWidth("title4") + unit(2.24 - nudge_x, "lines"),
           y = unit(-.5, "lines"),
           hjust = 0, vjust = 0, gp = gpar(col = colors[5]))
  }
) 

Upvotes: 5

Views: 1215

Answers (1)

Claus Wilke
Claus Wilke

Reputation: 17810

Let's attempt to answer the question as written. The question as I read it goes as follows:

This code works:

grobs <- grobTree(
  gp = gpar(fontsize = 14, fontface = 'bold'),

  textGrob(label = title_segments[1], name = "title1",
           x = unit(2.33 - nudge_x, "lines"),
           y = unit(-.5, "lines"),
           hjust = 0, vjust = 0, gp = gpar(col = colors[1])),

  if(length(title_segments) > 1){
    textGrob(label = title_segments[2], name = "title2",
             x = grobWidth("title1") + unit(2.24 - nudge_x, "lines"),
             y = unit(-.5, "lines"),
             hjust = 0, vjust = 0, gp = gpar(col = colors[2]))
  },

  if(length(title_segments) > 2){
    textGrob(label = title_segments[3], name = "title3",
             x = grobWidth("title1") + grobWidth("title2") + unit(2.24 - nudge_x, "lines"),
             y = unit(-.5, "lines"),
             hjust = 0, vjust = 0, gp = gpar(col = colors[3]))
  },
  if(length(title_segments) > 3){
    textGrob(label = title_segments[4], name = "title4",
             x = grobWidth("title1") + grobWidth("title2") + grobWidth("title3") +  unit(2.24 - nudge_x, "lines"),
             y = unit(-.5, "lines"),
             hjust = 0, vjust = 0, gp = gpar(col = colors[4]))
  },
  if(length(title_segments) > 4){
    textGrob(label = title_segments[5], name = "title5",
             x = grobWidth("title1") + grobWidth("title2") + grobWidth("title3") + grobWidth("title4") + unit(2.24 - nudge_x, "lines"),
             y = unit(-.5, "lines"),
             hjust = 0, vjust = 0, gp = gpar(col = colors[5]))
  }
) 

However, this code, which is meant as a computational recreation of the previous code, does not:

grobs <- NULL
grobs[1] <- list(gp = gpar(fontsize = 14, fontface = 'bold'))
grobs[2] <- list(textGrob(label = title_segments[1], name = "title1",  
                          x = unit(2.33 - nudge_x, "lines"), 
                          y = unit(-.5, "lines"), 
                          hjust = 0, vjust = 0, gp = gpar(col = colors[1])))

if(length(title_segments) > 1){ 
  x <- unit(2.24 - nudge_x, "lines")
  more_grobs <- pmap(list(title_segments[-1], colors[-1], 
seq_along(title_segments)[-1]), function(segment, color, i){
    grob <- textGrob(label = segment, name = paste0('title', i, sep = ''),
                          x = x + grobWidth(paste0('title', i - 1, sep = '')),     
                          y = unit(-.5, "lines"),
                          hjust = 0, vjust = 0, gp = gpar(col = color))
  })
}
grobs <- c(grobs, more_grobs)

grobs <- do.call(what = grobTree, args = grobs) ### ERROR HERE

What's going on? The answer is that the problem lies in the first two lines:

grobs <- NULL
grobs[1] <- list(gp = gpar(fontsize = 14, fontface = 'bold'))

The assignment grobs[1] <- removes the naming of gp = ... for the list element, and therefore the function grobTree() doesn't understand that the first argument is not a grob. The fix is simple. Replace those two lines with:

grobs <- list(gp = gpar(fontsize = 14, fontface = 'bold'))

Now things work, sort of. The do.call() line does not cause an error anymore. However, the spacing of the words is still not right, because the pmap() call doesn't create a sum of all the grob widths from the first to the nth. Instead it only uses the grob width of the previous grob. This problem is best solved with a recursive function, I think:

make_grobs <- function(words, colors, x, y, hjust = 0, vjust = 0, i = 0) {
  n <- length(words)
  colors <- rep_len(colors, n)
  name <- paste0('title', i)
  grob <- textGrob(label = words[1], name = name,
                   x = x, y = y, hjust = hjust, vjust = vjust,
                   gp = gpar(col = colors[1]))
  if (n == 1) {
    list(grob)
  }
  else {
    c(list(grob),
      make_grobs(words[-1], colors[-1],
                 x + grobWidth(grob), y, hjust, vjust, i + 1))
  }
}

With this function defined, the entire reproducible example becomes:

library(purrr)
library(grid)
library(gridExtra)
library(ggplot2)

title_segments <- c('Help ', 'me ', 'please', '!')
colors <- c('red', 'orange', 'green', 'blue')
nudge_x = 0

grobs <- do.call(what = grobTree, 
                 args = c(make_grobs(title_segments, colors,
                                     x = unit(2.33 - nudge_x, "lines"),
                                     y = unit(-.5, "lines")),
                          list(gp = gpar(fontsize = 14, fontface = 'bold'))))

qplot(displ, year, data = mpg)
gb <- ggplot_build(last_plot()) 
gt <- ggplot_gtable(gb) 
gt$layout$clip[gt$layout$name=="panel"] <- "off" 
gg <- arrangeGrob(gt, top = grobs, padding = unit(2.6, "line")) 
grid.newpage() 
grid.draw(gg) 

enter image description here

Upvotes: 3

Related Questions