그래서 grobs 목록을 작성한 다음 grobTree()
에 전달하려고 시도하지만 내 목록 항목이 do.call()
에 의해 grobs로 읽히지 않습니다. 내 목록 요소가 grobs과 같이되지 않기 때문에,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()
문에 도착하면 오류가 발생합니다 :
여기 내 코드입니다.
이 코드를 시도하면 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])))
, 그것은 거짓
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 버전을 게시했다. –
효과적인 방법으로 루프되지 않은 코드를 생성하는 특정 문제를 해결하는 답변을 게시했습니다. –