为嵌套图隐藏的调整后的标签

时间:2018-04-26 16:01:57

标签: r ggplot2 cowplot

如何在嵌套的plot_grid图中制作调整后的标签,而不是隐藏在其他图的下方?

这很好用:

# label 'b' is visible on top of figure a
plot_grid(ggdraw(), ggdraw(), nrow=2, labels=c("a", "b"), hjust=c(-0.5, -5), vjust=c(1,-2))

enter image description here

但不是这样:

# label 'b' is invisible below figure a.
plot_grid(ggdraw(), 
      plot_grid(ggdraw(), ggdraw(),
         nrow=2, rel_heights = c(0.4, 0.6), labels=c("b", "c"), hjust=c(-5,-0.5), vjust=c(0.5,0)), 
      nrow=2, rel_heights = c(0.33, 0.66))

enter image description here

1 个答案:

答案 0 :(得分:1)

这是剪辑问题。 plot_grid()使用ggplot绘制网格,ggplot剪辑位于绘图面板之外的内容。你的截止信件部分落在情节小组之外:

p1 <- ggdraw() + theme(plot.background = element_rect(fill = "#FF000080", color = NA))
p2 <- ggdraw() + theme(plot.background = element_rect(fill = "#00FF0080", color = NA))
p3 <- ggdraw() + theme(plot.background = element_rect(fill = "#0000FF80", color = NA))

row <- plot_grid(p1, p2, nrow=2, rel_heights = c(0.4, 0.6),
          labels=c("b", "c"), hjust=c(-5,-0.5), vjust=c(0.5,0))
plot_grid(p3, row, nrow=2, rel_heights = c(0.33, 0.66))

enter image description here

一种解决方案是禁用此剪辑:

row_grob <- ggplotGrob(row)
index <- grep("panel", row_grob$layout$name)
row_grob$layout$clip[index] = "off"

plot_grid(p3, row_grob,
          nrow=2, rel_heights = c(0.33, 0.66))

enter image description here

或者,您可以在使用draw_label()组装整个绘图网格后绘制标签。