使用grid.layout精确定位图 [英] exact positioning of plots with grid.layout
问题描述
我有两个晶格图.我需要将一个放置在另一个之上,并且它们之间需要0.5英寸的垂直间距.我的想法是使用grid.layout
指定三行布局,中间的行高度恰好是0.5英寸.然后,我可以将一个图打印到最上面一行,将另一个图打印到最下面一行.
I have two lattice plots. I need to position one above the other, and I need exactly .5" of vertical space between them. My thought is to use grid.layout
to specify a three-row layout, with the middle row being exactly .5" high. Then I can print one plot to the top row and the other plot to the bottom row.
快要工作了.问题是我无法使中间行高度精确到0.5英寸.这是一个最小的示例:
It's almost working. The problem is that I can't get the middle row to be exactly .5" high. Here's a minimal example:
pdf(file='example.pdf', height=12)
# Create layout and viewports
masterLayout <- grid.layout(
nrow = 3,
ncol = 1,
heights = unit(c(1, .5, 1), c("null", "inches", "null")),
respect = matrix(c(0, 1, 0)))
vp1 <- viewport(layout.pos.row=1, just=c("center", "bottom"))
vp2 <- viewport(layout.pos.row=3, just=c("center", "top"))
# Create plots
plot1 <- xyplot(1 ~ 1, panel = function () grid.rect(gp=gpar(fill="black")))
plot2 <- xyplot(1 ~ 1, panel = function () grid.rect(gp=gpar(fill="red")))
# Push viewports and print plots
pushViewport(viewport(layout = masterLayout))
pushViewport(vp1)
print(plot1, newpage = FALSE)
upViewport()
pushViewport(vp2)
print(plot2, newpage = FALSE)
dev.off()
我在此示例中尝试了多种变体,但无法将图之间的距离固定为.5".有没有办法做到这一点?
I've tried many variations on this example, but I haven't been able to fix the distance between the plots at .5". Is there a way to do that?
更新:baptiste在下面的回答是好的.另请参阅 https://stat.ethz.ch的Deepayan Sarkar的答案. /pipermail/r-help/2012-June/316178.html .
Update: baptiste's answer below is good. See also Deepayan Sarkar's answer at https://stat.ethz.ch/pipermail/r-help/2012-June/316178.html.
推荐答案
尝试一下:
library(lattice)
# Create layout and viewports
masterLayout <- grid.layout(
nrow = 3,
ncol = 1,
heights = unit(c(1, .5, 1), c("null", "inches", "null")),
respect = matrix(c(0, 1, 0)))
vp1 <- viewport(layout.pos.row=1, name="vp1")
vp2 <- viewport(layout.pos.row=3, name="vp2")
vp3 <- viewport(layout.pos.row=2, name="spacer")
theme.novpadding <-
list(layout.heights =
list(top.padding = 0,
main.key.padding = 0,
key.axis.padding = 0,
axis.xlab.padding = 0,
xlab.key.padding = 0,
key.sub.padding = 0,
bottom.padding = 0),
layout.widths =
list(left.padding = 0,
key.ylab.padding = 0,
ylab.axis.padding = 0,
axis.key.padding = 0,
right.padding = 0))
# Create plots
plot1 <- xyplot(1 ~ 1, panel = function () grid.rect(gp=gpar(fill="black")),
scales=list(axs='i',draw=FALSE),
xlab=NULL,ylab=NULL,par.settings = theme.novpadding)
plot2 <- xyplot(1 ~ 1, panel = function () grid.rect(gp=gpar(fill="red")),
scales=list(axs='i',draw=FALSE),
xlab=NULL,ylab=NULL,par.settings = theme.novpadding)
grid.newpage()
pushViewport(vpTree(viewport(layout = masterLayout,name="master"), vpList(vp1, vp2, vp3)))
seekViewport("master")
print(plot1, draw.in = "vp1")
print(plot2, draw.in = "vp2")
seekViewport("spacer")
grid.rect()
这篇关于使用grid.layout精确定位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!