2016-04-30 9 views
3

プロットに特定の方法で注釈を付ける必要があります。答えhereに続いて、私はこれを考え出すことができ、指定された色のプロット領域外の図形で注釈を付ける

df = data.frame(y= rep(c(1:20, 1:10), 5), x=c(rep("A", 20), rep("B", 10), rep("C", 20), rep("D", 10), rep("E", 20), 
              rep("F", 10), rep("G", 20), rep("H", 10), rep("I", 20), rep("J", 10)), 
      g= c(rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10),rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10), 
       rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10),rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10), 
       rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10))) 

p <- ggplot(df, aes(factor(x), y)) + geom_boxplot()+   # Base plot 
    theme(plot.margin = unit(c(3,1,1,1), "lines"), plot.background= element_rect(color= "transparent")) # Make room for the grob 
for (i in 1:length(df$g)) { 
    p <- p + annotation_custom(
    grob = textGrob(label = df$g[i], hjust = 0, gp = gpar(cex = 1.5)), 
    xmin = df$x[i],  # Vertical position of the textGrob 
    xmax = df$x[i], 
    ymin = 22,   # Note: The grobs are positioned outside the plot area 
    ymax = 22) 
}  

gt <- ggplot_gtable(ggplot_build(p)) 
gt$layout$clip[gt$layout$name == "panel"] <- "off" 
grid.draw(gt) 

これは、このプロットを生成します。

注釈では2個でなく、1ではなく青色の「+」記号ではなく、サイズ0.6 ptの青い三角形が必要です。ここで私を助けてくれますか?

答えて

4

textGrob機能の代わりにpointsGrobを使用する必要があります。テキストラベルの代わりにポイントを追加する必要がある行は、次のようになります。

pointsGrob(pch = ifelse(df$g[i]==1,3,17), gp = gpar(cex = 0.6,col=ifelse(df$g[i]==1,"red","blue"))) 

ここで私は$ \ $三角形の形状のため+ポイントと17のための3を使用しています。全体のコードは

df = data.frame(y= rep(c(1:20, 1:10), 5), x=c(rep("A", 20), rep("B", 10), rep("C", 20), rep("D", 10), rep("E", 20), 
               rep("F", 10), rep("G", 20), rep("H", 10), rep("I", 20), rep("J", 10)), 
       g= c(rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10),rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10), 
        rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10),rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10), 
        rep(sample(1:2, 1), 20), rep(sample(1:2, 1), 10))) 

p <- ggplot(df, aes(factor(x), y)) + geom_boxplot()+   # Base plot 
    theme(plot.margin = unit(c(3,1,1,1), "lines"), plot.background= element_rect(color= "transparent")) # Make room for the grob 
for (i in 1:length(df$g)) { 
    p <- p + annotation_custom(
    grob = pointsGrob(pch = ifelse(df$g[i]==1,3,17), gp = gpar(cex = 0.6,col=ifelse(df$g[i]==1,"red","blue"))), 
    xmin = df$x[i],  # Vertical position of the textGrob 
    xmax = df$x[i], 
    ymin = 22,   # Note: The grobs are positioned outside the plot area 
    ymax = 22) 
}  

gt <- ggplot_gtable(ggplot_build(p)) 
gt$layout$clip[gt$layout$name == "panel"] <- "off" 
grid.draw(gt) 

の下に表示され、生成されたプロットは、私はこのことができます願ってい

enter image description here

下に表示されます。

+0

ありがとうございます!まさに私が望んでいたもの。 :) –

1

他のレイヤーに望ましくない副作用が生じる可能性があるため、クリッピングをオフにすることは本当に最後の手段になります。ここでは、gtableに行を追加してgrobを必要な位置に配置する方が簡単です。より複雑なグロブが必要だった、または単に注釈とプロット間の一貫性のマッピングと同じやり方で、1でしplace a ggplot plot panelを確保するために、場合

p <- ggplot(df, aes(factor(x), y)) + 
     geom_boxplot() + ggtitle("this plot has a title") 


library(grid) 
library(gtable) 

gb <- ggplot_build(p) 

# get the axis positions 
xpos <- gb$panel$ranges[[1]][["x.major"]] 

g <- ggplot_gtable(gb) 
g <- gtable_add_rows(g, unit(1,"line"), 2) 
gl <- pointsGrob(xpos, rep(0.5, length(xpos)), pch=seq_along(xpos), 
       default.units = "npc") 
g <- gtable_add_grob(g, gl, t=3, l=4) 
grid.draw(g) 

enter image description here

+0

ありがとう。今のところ私はゴアの答えを使用しましたが、私はあなたの将来を念頭に置いておきます。 :) –

関連する問題