如何在gridExtra中通过arrange.grid创建的图表周围添加边框,其中包含一组ggplot2散点图

Kon*_*rad 13 r border scatter-plot ggplot2 gridextra

我正在使用以下代码:

# Libs
require(ggplot2); require(gridExtra); require(grid)

# Generate separate charts
chrts_list_scts <- list()

# Data
data("mtcars")

# A
chrts_list_scts$a <- ggplot(mtcars) +
  geom_point(size = 2, aes(x = mpg, y = disp,
                           colour = as.factor(cyl))) +
  geom_smooth(aes(x = mpg, y = disp),
              method = "auto") +
  xlab("MPG") +
  ylab("Disp") +
  theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none")

# B
chrts_list_scts$b <- ggplot(mtcars) +
  geom_point(size = 2, aes(x = mpg, y = drat,
                           colour = as.factor(cyl))) +
  geom_smooth(aes(x = mpg, y = drat),
              method = "auto") +
  xlab("MPG") +
  ylab("Drat") +
  theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none")

# C
chrts_list_scts$c <- ggplot(mtcars) +
  geom_point(size = 2, aes(x = mpg, y = qsec,
                           colour = as.factor(cyl))) +
  geom_smooth(aes(x = mpg, y = qsec),
              method = "auto") +
  xlab("MPG") +
  ylab("QSEC") +
  guides(colour = guide_legend(title = "cyl")) +
  theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "bottom",
        legend.key = element_rect(colour = NA))

# Arrange grid
png(filename = "chrts.PNG", width = 6,
    height = 10, units = 'in', res = 300)
title_text <- c("mtcars")
chrts_list_scts$all_scts <- grid.arrange(chrts_list_scts$a,
                                         chrts_list_scts$b,
                                         chrts_list_scts$c,
                                         top =
                                           textGrob(label = title_text,
                                                    gp = gpar(
                                                      fontsize = 14,
                                                      font = 2)))
dev.off()
rm(title_text)
Run Code Online (Sandbox Code Playgroud)

要生成以下图表:

第一张图表

我有兴趣在该图表周围添加边框,如下图所示:

有边框

尝试

我尝试通过添加polygonGrob代码来解决此请求:

chrts_list_scts$all_scts <- grid.arrange(chrts_list_scts$dep_work,
                                         chrts_list_scts$chld_work,
                                         chrts_list_scts$pens,
                                         polygonGrob(x = c(0,0.5,1.05),
                                                     y = c(0,0.5,1.05)
                                                     ),
                                         top =
                                           textGrob(label = title_text,
                                                    gp = gpar(
                                                      fontsize = 14,
                                                      font = 2)))
Run Code Online (Sandbox Code Playgroud)

但这会生成一个无意义的图表,底部有一条线.我看了一下关于SO的类似讨论,但我不清楚如何找到一个有效的解决方案.

方面要求

除了生成边框,我想:

  1. 能够对边界美学进行一些控制,比如改变边框的大小和颜色.
  2. 理想情况下,我想封装这个解决方案arrange.grid呼叫.所以在对象上chrts_list_scts$all_scts有所有元素,包括图表和整齐的边框.

我很乐意接受仅针对边界的主要要求的解决方案,如果有一个建议的解决方案与剩余的两个点相匹配,它将更好.

G. *_*eck 11

1)使用问题中提供的链接的虹膜示例(但进一步简化),只需添加最后一行.修改gpar(...)组件(可能还有widthheight)以获得不同的美学效果.(这不会封装在grid.arrange通话中.)

library(ggplot2)
library(grid)
library(gridExtra)

g <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point()
grid.arrange(g, g, ncol=2)


# next line adds border
grid.rect(width = .98, height = .98, gp = gpar(lwd = 2, col = "blue", fill = NA))
Run Code Online (Sandbox Code Playgroud)

(继剧情后)

截图

2)这是解决方案(1)的变体,其中在gt正面通过创建凹槽来封装gTree中的图形和边界.另一方面,它确实涉及一些额外的复杂性:

grid.newpage()
ga <- arrangeGrob(g, g, ncol = 2)
gb <- rectGrob(height = .98, width = .98, gp = gpar(lwd = 2, col = "blue", fill = NA)) # border, no fill
gt <- gTree(children = gList(ga, gb))
grid.draw(gt)
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢您提供出色的解决方案.在某些时候,我将`fill = NA`添加到语句`grid.rect(width = .98,height = 1,gp = gpar(lwd = 1,col ="black",fill = NA))`得到一个覆盖图表的白色方块(在图表定义后添加`grid.rect`行之后). (2认同)