How to build a grid on one page?

I use the package ( treemap ), which uses the grid package to create the treemap. However, I would like to build several of these treemaps together to add different color schemes to these graphs. The tmPlot function uses the grid.newpage function, which clears the graphics window. I have not found a way to save grid.newpage objects, as you can do for ggplot2 objects. Is there a way to build multiple grid.newpage objects in one window?

 ## Example library(treemap) # load Gross national income data data(GNI2010) size <- aggregate(GNI ~ continent, GNI2010, sum) size <- size[with(size, order(GNI, decreasing = T)),] cont <- size$continent widths <- c(sum(size[c(1,3,5),]$GNI), sum(size$GNI) - sum(size[c(1,3,5),]$GNI)) heights <- c(sum(size[c(1,2),]$GNI), sum(size[c(3,4),]$GNI), sum(size[c(5,6),]$GNI)) palettes <- c("Greens", "Blues", "Reds", "Oranges", "Purples", "Greys") i <- 1 # This is to be replaced by for loop x <- subset(GNI2010, continent == cont[i], cex = 5) # create treemap layout(matrix(1:6, 3, byrow = TRUE), widths = widths, heights = heights) x1 <- tmPlot(x, index=c("iso3"), vSize="population", vColor="GNI", type="value", title = "", position.legend = "none", palette = palettes[i]) grid.text(cont[i], 0.5, 0.5, gp=gpar(fontsize=20, font = 2, col = "white")) ## x1 is does not make a plot as such and tmPlot overwrites layout 

I understand that my decision to scale graphs based on the GNI amount is incorrect. I can ask another question about this later, as soon as I figure out how to build these treemaps in one window.

EDIT: I think the answer to this is no . Currently, you cannot save grid.newpage objects by name, and you cannot save some of them on the page because the function โ€œerases the current device or goes to a new pageโ€, as the description says. However, one can find work around. tmPlot package currently (as of March 23, 2013) does not support viewport s, but a development version.

+2
source share
2 answers

Thanks for your question. The output of tmPlot is really not a saved plot.

In the next update, I will add the vp argument, with which you can specify the viewing area for drawing. Only if it is not specified, grid.newpage is called.

UPDATE: you can check and test the development version https://github.com/mtennekes/treemap

To apply the example of Brian Hanson:

 vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y) grid.newpage() pushViewport(viewport(layout = grid.layout(1, 2))) tmPlot(GNI2010, index="continent", vSize="population", vColor="GNI", type="value", vp = vplayout(1,1)) tmPlot(GNI2010, index=c("continent", "iso3"), vSize="population", vColor="GNI", type="value", vp = vplayout(1,2)) 
+2
source

Here's a very flexible approach to any grid graphics:

 vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y) grid.newpage() pushViewport(viewport(layout = grid.layout(1, 2))) print(a, vp = vplayout(1,1)) print(b, vp = vplayout(1,2)) 

Where a and b are your saved plot objects. So, check each chart individually ahead of time, save them as a, b, ... , then draw them as described above.

Oh, and if tmPlot always does grid.newpage , then check if it has a new.page argument that you can set to FALSE , or make a copy of the function and comment out newpage .

+1
source

All Articles