exact positioning of plots with grid.layout

偶尔善良 提交于 2019-12-11 04:58:11

问题


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.

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()

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?

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.


回答1:


Try this:

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()



来源:https://stackoverflow.com/questions/11069202/exact-positioning-of-plots-with-grid-layout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!