I am using a package (treemap
) that uses grid
package to produce a treemap. However, I would like to plot several of these treemaps together, to add different color schemes to these plots. tmPlot
function uses 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 plot several grid.newpage
objects to a same 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 solution to scale the plots based on GNI sum is not right. I might make another question about that later, once I figure out how to plot these treemaps in a same window.
EDIT: I think the answer to this question is "no". Currently you cannot save grid.newpage objects by name, neither can you save several of these on a page, because the function "erases the current device or moves to a new page" as said in the description. However, it is possible to find work arounds. tmPlot
package does not currently (as of 23 March, 2013) support viewport
s, but the development version does.
See Question&Answers more detail:
os