【问题标题】:Modify space after adding annotate for multiplot panels为多图面板添加注释后修改空间
【发布时间】:2019-07-18 19:47:30
【问题描述】:

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

剧情如下:

我该怎么做:

  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))

【问题讨论】:

    标签: r ggplot2 gridextra annotate cowplot


    【解决方案1】:

    因此,对于您的问题,这将是一个半纯 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)
    

    如下所示:

    由于 data.frames 中的NAs,我收到了一些警告,但通常不应该发生这种情况。如果您不喜欢这些条带(面板标题类似于深灰色框中的内容),您可以在绘图代码中简单地调整主题:+ 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)
    

    但是请注意,rbind()ing 一起 plotgrobs 要求它们具有相同数量的列,因此您不能在一个情节中省略图例/指南,而不是另一个情节。不过,您可以在 gtable 中删除其中一个。

    【讨论】:

    • 为了摆脱我使用的灰色框 theme(strip.background = element_blank(), strip.text = element_blank())facet_wrap(~ panel, ncol = 2, scales='free') 让您可以重复轴。
    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2015-10-18
    • 1970-01-01
    • 2013-02-21
    • 2012-02-27
    相关资源
    最近更新 更多