1

我正在尝试将多个树形图放在一个页面上。每个树状图都用于数据的一个子集,一次查看它们会很有用。以下代码为每个子集创建了一个树形图,但它们都在自己的页面上。

问题 1) 有没有办法将所有树形图放在一个页面上?问题 2)有没有办法缩放每个树形图的整体大小,使一些更大和一些更小?

library(treemap)
library(plyr)

numSubsets = sapply(df[myIndexColumn], function(x) length(unique(x)))
par(mfrow=c(1, numSubsets))
do_treemap <- function(mySubset)
{

  t <- paste("Subset Number",mySubset$subset_num[1])
  treemap(mySubset, index=c("Level 1","Level 2"), vSize="sizeVar", vColor="colorVar", title=t)
}

ddply(df, .variables=c("subset_num"), .fun=do_treemap)
4

2 回答 2

2

treemap 函数接受一个“vp”参数,它是一个网格视口。

grid.newpage()
grid.rect()
pushViewport(viewport(layout=grid.layout(3, 1)))


do_treemap <- function(ind){
    vp <- viewport(layout.pos.col=1, layout.pos.row=ind)
    pushViewport(vp)
    treemap(business, index=c("NACE1", "NACE2", "NACE3"), vSize="turnover", type="index",vp=vp)
    popViewport()
    popViewport() #treemap doees not seem to pop corretly
    popViewport() #and one more!
}

lapply(1:3, do_treemap)
于 2013-08-23T11:29:23.543 回答
1

感谢您的提问,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 来控制宽度和高度之间的比率。

于 2013-08-24T06:51:27.640 回答