4

数学記号を追加したい場合は、以前はパッケージをfacet_wrap次のように使用できました(例は here から取得)grid

library(ggplot2); library(grid)
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) 
    xlim(0, 2) + stat_binhex(na.rm = TRUE) + opts(aspect.ratio = 1)
d <- d + facet_wrap(~ color, ncol = 4)
grob <- ggplotGrob(d)
strip_elem <- grid.ls(getGrob(grob, "strip.text.x", grep=TRUE, global=TRUE))$name
grob <- editGrob(grob, strip_elem[1], label=expression(Y[1]))
grid.draw(grob)

次のエラーが発生するため、これは機能しなくなりました。

>  strip_elem <- grid.ls(getGrob(grob, "strip.text.x", grep=TRUE, global=TRUE))$name
Error in getGrob(grob, "strip.text.x", grep = TRUE, global = TRUE) : 
  It is only valid to get a child from a 'gTree'
> grob <- editGrob(grob, strip_elem[1], label=expression(Y[1]))
Error in editGrob(grob, strip_elem[1], label = expression(Y[1])) : 
  object 'strip_elem' not found

バージョン 0.9.2 で数学記号を追加するにはどうすればよいですか?

4

1 に答える 1

4

更新ここから入手できる facet_wrap_labeller関数を使用する方が簡単な場合があります。

library(ggplot2)
library(gtable)

d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
    xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1)
(d <- d + facet_wrap(~color, ncol = 4))    

facet_wrap_labeller <- function(gg.plot, labels=NULL) {
  require(gridExtra)

  g <- ggplotGrob(gg.plot)
  gg <- g$grobs      
  strips <- grep("strip_t", names(gg))

  for(ii in seq_along(labels))  {
    modgrob <- getGrob(gg[[strips[ii]]], "strip.text", 
                       grep=TRUE, global=TRUE)
    gg[[strips[ii]]]$children[[modgrob$name]] <- editGrob(modgrob,label=labels[ii])
  }

  g$grobs <- gg
  class(g) = c("arrange", "ggplot",class(g)) 
  return(g)
}

# Some arbitrary strip texts
StripTexts = expression(Y[1], E, F, G, H, I, J)

# Apply the facet_wrap_labeller function
g = facet_wrap_labeller(d, StripTexts)

# Draw it
g

ストリップテキストの選択を少し簡単にするためのマイナーな編集を含む 元のソリューション。

これは機能しますが、途中で少し厄介になります。とパッケージggplot2 0.9.2からの関数を使用します。もっと簡単な方法が必要です。これから何かを取り入れて、もっと簡単な解決策を見つけられることを願っています。gridgtable

手順は次のとおりです。

  • 元のプロットを描画します。
  • 変更する必要のあるストリップ要素を見つけます。
  • そのストリップ要素を抽出します。
  • 「D」をexpression(Y[1]);に変更します。
  • 変更したストリップ要素を元のプロットに挿入し直します。

    library(ggplot2); library(grid); library(gtable)
    d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
        xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1)
    (d <- d + facet_wrap(~color, ncol = 4))    
    # The aim is to change the strip text "D" to expression(Y[1])
    
    # Winston's accessor function:
    gt_getgrob <- function(gt, pattern) {
      idx <- grep(pattern, gt$layout$name)
      if (length(idx) > 1)
        stop("More than one match for pattern '", pattern, "'")
      gt$grobs[[idx]]
    }
    
    g = ggplotGrob(d)
    g$layout    # We want "strip_t-1"
    strip <- gt_getgrob(g, "t-1")        # Use the accessor function to extract it.
    str(strip)     # Locate the "D" label
    
    strip$children[[2]]$label = expression(Y[1])    # Change the label.
    grid.draw(strip)       # Yes, it's worked
    
    gt <- ggplot_gtable(ggplot_build(d))  
    gtable_show_layout(gt)       # Get the layout of the original plot.
    
    gt = gtable_add_grob(gt, strip, t=3, l=4, b=3, r=4)  # Insert the modified strip element into the plot.
    grid.newpage()
    grid.draw(gt)  
    

結果は次のとおりです。

ここに画像の説明を入力してください

于 2012-10-05T07:39:28.457 に答える