因此,我试图制作一个grobs列表,然后将它们传递到grobTree()
,但我的列表项目不会作为grob通过do.call()
读入。如何将grobs存储在列表中并将它们传递给grobTree()?
这里是我的代码:
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)
当我到了do.call()
语句时发生错误,因为我的列表中的元素没有得到解读为grobs。
当我尝试这一点的代码,然后评估为真。
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])))
当我尝试这一点,它的计算结果为假
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])
编辑::这就是我想要实现与PMAP功能。
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]))
}
)
grobs [1]显然不是grob – baptiste
Grobs [1]应该是grobTree()的gp参数。这是我正在制作多色情节标题的一部分功能。我在编辑中发布了代码的unlooped版本。 –
我发布了一个答案,以解决以高效方式生成unlooped代码的特定问题。 –