Multiple R treemaps on a single page with scaling

浪尽此生 提交于 2019-12-23 03:11:01

问题


I'm trying to put multiple treemaps on a single page. Each tree map is for a subset of the data and it would be useful to see them all at once. The following code creates a treemap for each subset but they each are on their own page.

Question 1) Is there a way to put all of the treemaps on one page? Question 2) Is there a way to scale the overall size each treemap making some larger and some smaller?

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)

回答1:


the treemap function accepts a "vp" argument which is a grid viewport.

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)



回答2:


Thanks for your question, and Ido, thanks for your correct answer.

There was indeed a small bug that required two additional popViewports. Apart from that, you don't need to push vp (and pop it afterwards). The bug is fixed in the current github version.

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)

UPDATE: to come back to your second question: the sizes of the subplots are controlled by the viewport. For example:

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

In addition, you can specify the treemap argument aspRatio to control the ratio between width and height.



来源:https://stackoverflow.com/questions/18400771/multiple-r-treemaps-on-a-single-page-with-scaling

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