gpt4 book ai didi

减少 grid.arrange 图之间的空间

转载 作者:行者123 更新时间:2023-12-04 02:10:16 27 4
gpt4 key购买 nike

我问了一个问题 HERE关于网格排列并得到了极好的反响。我现在想减少图之间的空间,但出现错误。首先,我展示了有效的代码,然后是错误代码(我尝试过的)。我实际上找不到 grid.arrange并且一直假设它来自 gridExtra但我可能是不正确的。

所以2部分:

  • 如何减少网格排列的图之间的空间
  • 我在哪里可以找到关于 grid.arrange 的文档(Baptiste 我知道你维护 gridExtra 所以如果我没有按照预期的方式使用它,请纠正我的想法或使用它。)

  • 好代码坏空间
    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)

    错误代码(我的尝试)
    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)

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

    最佳答案

    我误解了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 图之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13299496/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com