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

Annotate with shapes outside plot area in specified color

我需要以某种方式注释我的情节。根据答案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 pt 而不是 2 的蓝色三角形和蓝色“+”符号而不是 1。你能帮我吗?

您必须使用 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$ 形状。整个代码如下所示

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)

生成的图如下所示

希望对您有所帮助。

关闭裁剪确实应该是最后的手段,因为它可能会对其他图层产生不必要的副作用。这里可能更容易在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)

如果需要更复杂的 grob,或者只是为了确保注释和绘图之间的一致映射,可以place a ggplot plot panel采用相同的方式。