减少grid.arrange图之间的空间 [英] reduce space between grid.arrange plots

查看:169
本文介绍了减少grid.arrange图之间的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个关于网格排列的问题这里,并得到了一个很棒的回应。我想现在缩小地块之间的空间,但是出现错误。首先,我介绍可以工作的代码,然后提供错误代码(我试过的)。我实际上找不到> grid.arrange 并始终假定它来自 gridExtra ,但我可能不正确。 / p>

如此2部分:


  1. 如何使用网格排列缩小图之间的空间

  2. 在哪里可以找到关于 grid.arrange 的文档(Baptiste我知道你维护gridExtra,所以请纠正我的想法或使用包如果我没有按照预期的方式使用它)。
  3. b
    良好的代码不良空间

      require(ggplot2); require(gridExtra)
    A < - ggplot(CO2,aes(x = Plant) )+ geom_bar()+
    coord_flip()+ ylab()
    B <-ggplot(CO2,aes(x = Type))+ geom_bar()+ coord_flip()


    gA< - ggplot_gtable(ggplot_build(A))
    gB< - ggplot_gtable(ggplot_build(B))
    maxWidth = grid :: unit.pmax(gA $ widths [2:3],gB $ widths [2:3])
    gA $ widths [2:3]< - as.list(maxWidth)
    gB $ widths [2:3]< - as.list (maxWidth)
    grid.arrange(gA,gB,ncol = 1)

    Bad code(my try)

      require(ggplot2); require(gridExtra)
    A< ; - ggplot(CO2,aes(x = Plant))+ geom_bar()+
    coord_flip()+ ylab()+ theme(plot.margin = unit(1,cm))
    )ggplot_gtable(ggplot_build(a))
    $ g


    $ ggplot_gtable(ggplot_build(A))
    gB < - ggplot_gtable(ggplot_build(B))
    maxWidth = grid :: unit.pmax(gA $ widths [2:3],gB $ widths [2:3])
    gA $ widths [2:3]< - as.list(maxWidth)
    gB $ widths [2:3]< - as.list(maxWidth)
    grid.arrange(gA,gB,ncol = 1 )

    错误:

     `[.unit`(主题$ plot.margin,2)中的错误:
    索引超出范围(单位子集)
    $ b

    解决方案

    我误解了ggplot:

     需要(GGPLOT2);要求(gridExtra) 
    A <-ggplot(CO2,aes(x = Plant))+ geom_bar()+
    coord_flip()+ ylab()+ theme(plot.margin = unit(c(1, 1,-1,1),lines))
    B < - ggplot(CO2,aes(x = Type))+ geom_bar()+ coord_flip()+
    theme(plot.margin =单元(rep(.5,4),lines))


    gA< - ggplot_gtable(ggplot_build(A))
    gB< - ggplot_gtable(ggplot_build (B))
    maxWidth = grid :: unit.pmax(gA $ widths [2:3],gB $ widths [2:3])
    gA $ widths [2:3]< - as.list(maxWidth)
    gB $ widths [2:3]< - as.list(maxWidth)
    grid.arrange(gA,gB,ncol = 1)


    I asked a question HERE about grid arrange and got a terrific response. I want to reduce the space between plots now but get an error. First I present the code that works and then the error code (what I tried). I can't actually find grid.arrange and have always assumed it comes from gridExtra but I may be incorrect.

    so 2 parts:

    1. How can I reduce space between plots with grid arrange
    2. Where can I find documentation about grid.arrange (Baptiste I know you maintain gridExtra so please correct my thinking or use of the package if I'm not using it in the way it was intended.)

    Good code bad space

    require(ggplot2);require(gridExtra)
    A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +
        coord_flip() + ylab("")
    B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() 
    
    
     gA <- ggplot_gtable(ggplot_build(A))
     gB <- ggplot_gtable(ggplot_build(B))
     maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3])
     gA$widths[2:3] <- as.list(maxWidth)
     gB$widths[2:3] <- as.list(maxWidth)
     grid.arrange(gA, gB, ncol=1)
    

    Bad code (my try)

    require(ggplot2);require(gridExtra)
    A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +
        coord_flip() + ylab("") + theme(plot.margin= unit(1, "cm"))
    B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() 
    
    
     gA <- ggplot_gtable(ggplot_build(A))
     gB <- ggplot_gtable(ggplot_build(B))
     maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3])
     gA$widths[2:3] <- as.list(maxWidth)
     gB$widths[2:3] <- as.list(maxWidth)
     grid.arrange(gA, gB, ncol=1)
    

    The error:

    Error in `[.unit`(theme$plot.margin, 2) : 
      Index out of bounds (unit subsetting)
    

    解决方案

    I was misunderstanding ggplot:

    require(ggplot2);require(gridExtra)
    A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +
        coord_flip() + ylab("") + theme(plot.margin= unit(c(1, 1, -1, 1), "lines"))
    B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() + 
        theme(plot.margin= unit(rep(.5, 4), "lines"))
    
    
     gA <- ggplot_gtable(ggplot_build(A))
     gB <- ggplot_gtable(ggplot_build(B))
     maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3])
     gA$widths[2:3] <- as.list(maxWidth)
     gB$widths[2:3] <- as.list(maxWidth)
     grid.arrange(gA, gB, ncol=1)
    

    这篇关于减少grid.arrange图之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆