我使用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似乎没有做任何事情。我的背景是黑色的,所以我希望在它周围创建一个白色边框。
答案 0 :(得分:5)
例如,您可以使用grid.rect
。在绘制你的情节之前,我会把它放在一个单独的viewport
中。我的意思是:
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)
答案 1 :(得分: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))))