2017-12-18 6 views
2

だから私は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()ステートメントに到達したときにエラーが発生します。

は、ここに私のコードです。

私はこのコードのビットをしようとすると、それが真と評価されます。

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]) 

EDITと評価さ::これは私が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])) 
    } 
) 
+0

grobsは、[1]、明らかではありませんgrobs – baptiste

+0

Grobs [1]はgrobTree()のgp引数であると考えられています。これは私が多色プロットのタイトルを作るために取り組んでいる機能の一部です。私は、編集のコードのunloopedバージョンを掲載しました。 –

+0

効率的な方法でループのないコードを生成するという特定の問題を解決する答えを投稿しました。 –

答えて

1

質問に答えるようにしましょう。質問私は次のようにそれが行く読んで:

このコードは動作します:

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 <- 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 

何が起こっているのですか?

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

割り当てgrobs[1] <-は、リスト要素のためのgp = ...の命名を削除し、そのための機能grobTree()は、最初の引数はグロブではないことを理解していない:その答えは、問題が最初の二行にあるということです。修正は簡単です。これらの2行を次のように置き換えます。

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

do.call()行でエラーが発生することはありません。しかし、pmap()呼び出しが最初からn番目までのすべてのgrob幅の合計を作成しないため、単語の間隔はまだ正しくありません。代わりに、前のgrobのgrob幅のみを使用します。この問題は、最高の再帰関数で解決され、私は思う:定義されたこの機能を

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)) 
    } 
} 

は、全体の再現性の例は次のようになります。

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

関連する問題