使用指定颜色的绘图区域外的形状进行注释

时间:2016-04-30 10:39:49

标签: r plot ggplot2 annotate

我需要以某种方式注释我的情节。根据答案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)

这会生成此图

我希望蓝色三角形的尺寸为0.6磅,而不是注释中的2,蓝色的“+”符号代替1.我能帮助我吗?

2 个答案:

答案 0 :(得分: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表示$ \ triangle $ shape。整个代码如下所示

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

我希望这会有所帮助。

答案 1 :(得分:2)

关闭剪切应该是最后的手段,因为它可能会对其他图层产生不必要的副作用。在这里,可以更容易向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,或者只是为了确保注释和绘图之间的一致映射,可以enter image description here以相同的方式。