感谢您的提问,Ido,感谢您的正确回答。
确实有一个小错误需要两个额外的 popViewports。除此之外,您不需要推送 vp (然后弹出它)。该错误已在当前 github 版本中修复。
require(grid)
grid.newpage()
data(business)
numVars <- c("turnover", "turnover.prev", "employees", "employees.prev")
pushViewport(viewport(layout=grid.layout(length(numVars), 1)))
do_treemap <- function(ind){
vp <- viewport(layout.pos.col=1, layout.pos.row=ind)
treemap(business, index=c("NACE1", "NACE2"), vSize=numVars[ind],
type="index",vp=vp)
upViewport(2) # Needed in version 2.0.1 due to bug. Not needed in later versions (i.e. github)
}
lapply(seq_along(numVars), do_treemap)
更新:回到你的第二个问题:子图的大小由视口控制。例如:
require(grid)
grid.newpage()
data(business)
numVars <- c("turnover", "turnover.prev", "employees", "employees.prev")
pushViewport(viewport(layout=grid.layout(2, 2, widths=c(0.4,0.6), heights=c(0.7,0.3))))
for (i in 1:2) {
for (j in 1:2) {
vp <- viewport(layout.pos.col=i, layout.pos.row=j)
treemap(business, index=c("NACE1", "NACE2"), vSize=numVars[i+(j-1)*2],
type="index",vp=vp)
upViewport(2) # Needed in version 2.0.1 due to bug. Not needed in later versions (i.e. github)
}
}
此外,您可以指定树形图参数 aspRatio 来控制宽度和高度之间的比率。