因此,我試圖製作一個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代碼的特定問題。 –