在排列的地块之间绘制 "grid"

Draw a "grid" between arranged plots

我正在处理 4 个不同的地块,我正在使用 ggpubr-package 中的 ggarrange() 将它们放在一个地块中。我准备了一个例子:

library(ggpubr)
library(ggplot2)

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() + ggtitle("Plot 1")
p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) + geom_point() + ggtitle("Plot 2")
p3 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Width)) + geom_point() + ggtitle("Plot 3")
p4 <- ggplot(iris, aes(x = Petal.Length, y = Sepal.Width)) + geom_point() + ggtitle("Plot 4") +
  facet_wrap(~Species)

plot.list <- list(p1, p2, p3, p4)

ggarrange(plotlist = plot.list)

输出:

我想在单个地块周围画一个边框,像这样:

有什么方法可以画出这个边框吗?谢谢!

grid.polygon() 非常手动,但我认为它可以解决问题:

使用 RStudio

library("ggpubr")
library(ggplot2)
library(gridExtra)
library(grid)
p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() + ggtitle("Plot 1")
p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) + geom_point() + ggtitle("Plot 2")
p3 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Width)) + geom_point() + ggtitle("Plot 3")
p4 <- ggplot(iris, aes(x = Petal.Length, y = Sepal.Width)) + geom_point() + ggtitle("Plot 4") +
  facet_wrap(~Species)

plot.list <- list(p1, p2, p3, p4)

ggarrange(plotlist = plot.list)
x = c(0, 0.5, 1, 0.5, 0.5, 0.5)
y = c(0.5, 0.5, 0.5,0, 0.5, 1)
id = c(1,1,1,2,2,2)
grid.polygon(x,y,id)

使用闪亮(编辑)

在shiny-app内进行时,需要使用annotation_custom()添加网格,如下:

    ggarrange(plotlist = plot.list) + 
    annotation_custom(
             grid.polygon(c(0, 0.5, 1, 0.5, 0.5, 0.5),
                          c(0.5, 0.5, 0.5,0, 0.5, 1), 
                          id = c(1,1,1,2,2,2), 
                          gp = gpar(lwd = 1.5)))

不确定这是否适合你,但你可以在你的个人地块周围加上边框。但是,这包括布局外部的边框。您的描述似乎并不反对,但您的示例图中只有内部网格线。

您可以在创建绘图时添加 theme 调用;我没有编辑情节创作,而是对列表中的每个情节进行编辑,然后再将它们粘在一起。

library(ggpubr)
library(ggplot2)

#### same plot creation here ######

plot.list <- lapply(list(p1, p2, p3, p4), 
                    function(p) p + theme(plot.background = element_rect(color = "black")))

ggarrange(plotlist = plot.list)