【问题标题】:Issue with multiple graphs in one page, R一页中有多个图表的问题,R
【发布时间】:2022-01-17 16:11:07
【问题描述】:

我正在寻找一种方法来对齐 R 中一个页面中的多个图,同时保持相同的面板尺寸和彼此之间的距离。我总共有四个地块,两个是用 ggplot 创建的,两个是用 levelplot 创建的。我尝试了以下方法,但面板放置不均。水平图(c 和 d)看起来更小,并且它们之间的距离很大。任何想法如何解决这个问题?

附:我知道我的代码不可重现,但我的栅格相当大,很抱歉。希望这个问题听起来很熟悉!

plot_grid(a,b,c,d,  ncol=2,nrow = 2, rel_heights = c(1/4, 1/4, 1/4,1/4))

ggarrange(a, b, c,d,ncol = 2, nrow = 2)

ggarrange(a, b, c,d,ncol = 2, nrow = 2,heights = c(1, 1, 1,1))

【问题讨论】:

    标签: r plot panel


    【解决方案1】:

    我从lattice手册中举了一个例子。

    library(lattice)
    
    x <- seq(pi/4, 5 * pi, length = 100)
    y <- seq(pi/4, 5 * pi, length = 100)
    r <- as.vector(sqrt(outer(x^2, y^2, "+")))
    grid <- expand.grid(x=x, y=y)
    grid$z <- cos(r^2) * exp(-r/(pi^3))
    plo1<- levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="",
          ylab="", main="My title", colorkey = FALSE)
    

    以及用 ggplot2 制作的情节

    library(ggplot2)
    
    plo2 <- ggplot(mpg, aes(displ, hwy)) + 
            geom_point() + 
            ggtitle("My title") 
    

    接下来,library(ggpubr)

    ggarrange(plo2, plo1, plo2, plo1, ncol = 2, nrow = 2)
    

    ...我认为它看起来不错。

    也许,您应该稍微修改一下您的情节(调整边距、图例等)?

    我认为,将 ggplot2 对象适配到 levelplot 会更好。

    你觉得怎么样?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 2020-11-22
      • 1970-01-01
      • 2018-07-14
      相关资源
      最近更新 更多