R lattice multiple plot page如何在页边距中放置文字

R lattice multiple plot page how to put text in page margin

我想在使用 R lattice 创建的绘图页面上放置一个标题。例如,我可以在一个页面上放置四个图,如下所示:

#load lattice
require(lattice). 
# data
a<-c(1,3,4)
b<-c(1,2,3)
# make plots
plt1<-xyplot(a~b,main="plt1")
plt2<-xyplot(a~b,main="plt2")
plt3<-xyplot(a~b,main="plt3")
plt4<-xyplot(a~b,main="plt4")
# plot plots
plot(plt1, split=c(1,1,2,2),newpage=FALSE)
plot(plt2, split=c(1,2,2,2),newpage=FALSE)
plot(plt3, split=c(2,1,2,2),newpage=FALSE)
plot(plt4, split=c(2,2,2,2),newpage=FALSE)

现在如何将标题 "My Page of Plots" 居中放置在 plt1 和 plt3 上方的上边距?

您可以使用 grid 推送视口并添加标题:

library(grid)
vp2 <- viewport(x = 0.5, y = 1, width = 1, height = .1, just = c("center", "top"))
pushViewport(vp2)
grid.rect(gp = gpar(vol = "blue")) # just to see dimensions/position of the viewport
grid.text("My Title", gp = gpar(cex = 2))

您必须稍微调整一下视口的位置和尺寸。理想情况下,您还可以为 lattice 调用添加上边距,这样您就可以为标题创建一些白色 space。