在为多绘图面板添加注释后修改空间

时间:2019-07-18 19:47:30

标签: r ggplot2 gridextra annotate cowplot

我有一个多图面板,其中有我想修改的注释(图外)。

这是情节: enter image description here

我如何:

  1. 将标题(I.和II。)移到最左边吗?
  2. 更改标题周围的空白量?
  3. 改变情节周围的空白空间吗?

(首选ggplot()解决方案)

这是获取以上图表的代码(改编自OP)(删除了theme不需要的代码,等等):

    panelA <- data.frame(
        Stage = c("Juvenile", "Juvenile", "Yearling", "Juvenile", "Juvenile", "Yearling","Juvenile", "Juvenile", "Yearling","Juvenile", "Juvenile", "Yearling"),
        Individual = c ("A", "A", "A","B", "B", "B","C", "C", "C","D", "D", "D"),
        Score = c(  1.4, 1.2, NA,0.4, 0.6, 0.5,-0.3, -0.5, -0.4,-1.4, -1.2, NA))

    A<-ggplot(panelA, aes(x = Stage, y = Score, color =Individual, group= Individual)) + 
      geom_point() + 
     geom_line()+
      geom_smooth(method=lm, se=F, fullrange=TRUE)

    panelB <- data.frame(
        Stage = c("Juvenile", "Juvenile", "Yearling", "Juvenile", "Juvenile", "Yearling","Juvenile", "Juvenile", "Yearling", "Juvenile", "Juvenile", "Yearling"),
        Individual = c ("A", "A", "A","B", "B", "B","C", "C", "C","D", "D", "D"),
        Score = c(  1.4, 1.2, 1.3,0.4, 0.6, NA,-0.3, -0.5, NA,-1.4, -1.2, -1.3))

    B<-ggplot(panelB, aes(x = Stage, y = Score, color =Individual, group= Individual)) + 
      geom_point() +
      geom_line()+
       geom_smooth(method=lm, se=F, fullrange=TRUE)

    library(ggplot2)
    library(gridExtra)
    library(RGraphics)
    library(cowplot)

    grid.newpage()
    # Create layout : nrow = 4, ncol = 2
    pushViewport(viewport(layout = grid.layout(4, 2)))
    # A helper function to define a region on the layout
    define_region <- function(row, col){
      viewport(layout.pos.row = row, layout.pos.col = col)
    } 

#text I want to annotate
    t1 <- ggdraw() + draw_label("I. Effects on variance components", fontface='bold')
    t2 <- ggdraw() + draw_label("II. Effects on means (mediated via plasticity)", fontface='bold')

    # Arrange the plots

    print(t1, vp=define_region(1, 1))
    print(A, vp = define_region(2, 1))
    print(B, vp=define_region(2, 2))
    print(t2, vp = define_region(3, 1))
    print(A, vp=define_region(4, 1))
    print(B, vp = define_region(4, 2))

1 个答案:

答案 0 :(得分:0)

因此,这里将是一种半纯的ggplot解决方案,不需要这些额外的软件包,除了ggplot已经依赖的一个软件包(另一个ggplot已经依赖的注释)之外。

要在水平方向上减小两个面板之间的空间,可以使用构面,而不是复制粘贴整个图,包括多余的坐标轴,空格和诸如此类:

AB <- ggplot(mapping = aes(Stage, Score, colour = Individual, group = Individual)) +
  geom_point(data = cbind(panelA, panel = "A")) +
  geom_point(data = cbind(panelB, panel = "B")) +
  geom_line(data = cbind(panelA, panel = "A")) +
  geom_line(data = cbind(panelB, panel = "B")) +
  facet_wrap(~ panel, ncol = 2)

现在要减少面板中从点到边缘的空间,您可以按某些比例调整expand参数。您可以根据需要设置较小或较大的0.1值:

AB <- AB + scale_x_discrete(expand = c(0,0.1))

在实际情况下,您可能不需要两次绘制相同的图,但是在您举一个示例的情况下,您将垂直绘制相同的图,因此我将继续介绍。现在,我们结合这些图:

top <- AB + ggtitle("I. Effects on variance components")
bottom <- AB + ggtitle("II. Effects on means (mediated via plasticity)")
combined <- rbind(ggplotGrob(top), ggplotGrob(bottom), size = "first")

但是由于我们已经通过ggplotGrob()将图转换为gtables,所以现在需要网格语法来绘制图:

grid::grid.newpage(); grid::grid.draw(combined)

如下所示:

enter image description here

由于data.frame中的NA,我收到了一些警告,但是通常不应该发生这种情况。如果您不喜欢这些条带(面板标题类似深灰色框中的内容),则可以简单地调整主题:在绘图代码中使用+ theme(strip.background = element_blank(), strip.text = element_blank())

您可以如下添加自定义注释:

combined <- gtable::gtable_add_grob(
  combined,
  grid::textGrob("Here be some annotation", x = 1, hjust = 1),
  t = 16, l = 9 # top/left positions of where to insert this
)
grid::grid.newpage(); grid::grid.draw(combined)

enter image description here

但是请注意,rbind()组合在一起的绘图小节要求它们具有相等的列数,因此您不能在一个绘图中省略图例/指南,而在另一个绘图中则不能省略。不过,您可以在gtable中删除其中之一。