4

我使用 ggplot 创建了一系列图并将它们打印在网格中。我想在整个网格周围添加一个边框,这样当我将图像插入演示文稿时,它就会被明确定义。

这是我正在使用的网格和打印命令的创建。我已经导入了所有必要的包。

library(grid)
pushViewport(viewport(gp=gpar(col="white"), 
             layout = grid.layout(4, 2, heights = unit(c(0.5,5,5,5), "null"))))  
grid.text("Summer Clear Day- August 2, 2012", 
          vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))
grid.frame(gp=gpar(col="white"), draw=TRUE)
print(tot, vp = viewport(layout.pos.row = 2, layout.pos.col = 1))
print(gh, vp = viewport(layout.pos.row = 2, layout.pos.col = 2))
print(dir, vp = viewport(layout.pos.row = 3, layout.pos.col = 1))
print(ir, vp = viewport(layout.pos.row = 3, layout.pos.col = 2))
print(dh, vp = viewport(layout.pos.row = 4, layout.pos.col = 1))
print(uv, vp = viewport(layout.pos.row = 4, layout.pos.col = 2))

grid.frame 似乎没有做任何事情。我的背景是黑色的,所以我希望在它周围创建一个白色边框。

4

2 回答 2

5

例如,您可以使用grid.rectviewport 在绘制你的情节之前,我会把它放在一个单独的地方。我是说:

  1. 我推第一个视口,绘制矩形(框架)
  2. 我推动第二个视口,我创造了你所有的情节。
  3. 我弹出我的 2 个视口

在此处输入图像描述

pushViewport(plotViewport(c(5,5,5,5)))
grid.rect()
grid.rect(width=unit(1, "npc")-unit(0.5,'lines'),
          height=unit(1, "npc")-unit(0.5,'lines'))
pushViewport(plotViewport(c(1,1,1,1),
                      layout = grid.layout(4, 2, 
                                           heights =     unit(c(1,5,5,5), "null")))) 
grid.text("Summer Clear Day- August 2, 2012", 
          vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))
k1 <- ggplot(mtcars, aes(factor(cyl), mpg))  + geom_boxplot()

print(k1, vp = viewport(layout.pos.row = 2, layout.pos.col = 1))
print(k1, vp = viewport(layout.pos.row = 2, layout.pos.col = 2))
print(k1, vp = viewport(layout.pos.row = 3, layout.pos.col = 1))
print(k1, vp = viewport(layout.pos.row = 3, layout.pos.col = 2))
print(k1, vp = viewport(layout.pos.row = 4, layout.pos.col = 1))
print(k1, vp = viewport(layout.pos.row = 4, layout.pos.col = 2))
upViewport(2)
于 2013-03-18T20:11:11.990 回答
5

@agstudy 答案的精简版:

  require(gridExtra)
  grid.draw(grobTree(arrangeGrob(k1, k1, k1, k1, k1, k1, ncol=2,
                       main="Summer Clear Day- August 2, 2012"), 
                     rectGrob(gp=gpar(lwd=2, fill=NA))))
于 2013-03-18T20:27:56.933 回答