如何在同一页面上绘制网格图? [英] How to plot grid plots on a same page?
问题描述
我正在使用使用grid
软件包的软件包(treemap
)来生成树图.但是,我想将其中几个树图绘制在一起,以添加
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
我了解我基于GNI总和缩放地块的解决方案是不正确的.一旦弄清楚如何在同一窗口中绘制这些树形图,我可能会对此再提一个问题.
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.
编辑:我认为该问题的答案是"否".当前,您不能按名称保存grid.newpage对象,也不能在页面上保存其中的几个对象,因为如说明.但是,可以找到解决方法. tmPlot
软件包当前(截至2013年3月23日)不支持viewport
,但开发版本支持.
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.
推荐答案
感谢您的提问. tmPlot的输出确实不是保存的图.
Thanks for your question. The output of tmPlot is indeed not a saved plot.
在下一次更新中,我将添加参数vp,通过该参数可以指定要插入的视口.只有在未指定视口的情况下,才会调用grid.newpage.
In the next update I will add argument vp, by which a viewport can be specified to draw in. Only if it is not specified, grid.newpage is called.
更新:您可以在 https://github.com/mtennekes/treemap
以布赖恩·汉森(Bryan Hanson)为例:
To apply the example of Bryan 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))
这篇关于如何在同一页面上绘制网格图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!