2016-04-30 24 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) 

這會產生該地塊

我想在註釋和藍色的「+」號,而不是1.尺寸0.6磅,而不是2藍色三角形能否請你幫我在這裏?

回答

4

你將不得不使用pointsGrob代替textGrob功能。您需要添加點而不是文本標籤的行將在下面。

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

這裏我使用3 +點和17 $ \ $三角形狀。整個代碼如下所示

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。

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

如果需要更復雜的GROB,或只是爲了確保,人們可以以同樣的方式place a ggplot plot panel註解和劇情之間的一致映射。

+0

謝謝。現在我用阿戈爾的答案,但我會繼續記住未來的方式。 :) –

相關問題