在已布置的地块之间绘制“网格”

bre*_*usn 6 r ggplot2 ggpubr

我正在使用4个不同的图,并且正在使用ggpubr软件包中的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)
Run Code Online (Sandbox Code Playgroud)

输出: 在此处输入图片说明

我想在单个图上画一个边框,如下所示:

在此处输入图片说明

有没有办法画这个边界?谢谢!

Car*_*tes 5

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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明 使用 Shiny(编辑)

在闪亮的应用程序中执行此操作时,需要使用添加网格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)))
Run Code Online (Sandbox Code Playgroud)


cam*_*lle 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)
Run Code Online (Sandbox Code Playgroud)