问题描述
示例:
我想绘制两个
问题:
我有问题并列放置两个地块。我试过:
grid.arrange(plot1,plot2)通过错误 Error in arrangeGrob(...,as.table = as.table,clip = clip,main = main,:input必须是grob!。我认为这应该起作用(使用
> sessionInfo()
R版本3.1.2(2014-10-31)
平台:x86_64-apple-darwin13.4.0(64位)
语言环境:
[1] de_DE.UTF-8 / de_DE.UTF-8 / de_DE.UTF-8 / C / de_DE.UTF-8 / de_DE.UTF-8
附加基本包:
[1] stats graphics grDevices utils datasets methods base
其他附加软件包:
[1] tmap_1.0 dplyr_0.4.3 sp_1.1-1
已加载通过命名空间(并未附加):
[1] assertthat_0.1 class_7.3-11 classInt_0.1-23 colorspace_1.2-6 DBI_0.3.1 digest_0.6.8 e1071_1.6-4 ggplot2_1.0.1
[9] grid_3.1.2 gridBase_0.4-7 gtable_0.1.2 lattice_0.20-29 magrittr_1.5 MASS_7.3-35 munsell_0.4.2 parallel_3.1.2
[17] plyr_1.8.3 proto_0.3-10 R6_2.1.1 raster_2.3-40 RColorBrewer_1.1-2 Rcpp_0.12.2 reshape2_1.4.1 rgdal_0.8-16
[25] rgeos_0.3-11 scales_0.3.0 stringi_1.0-1 stringr_1。 0.0 tools_3.1.2
好的问题!
grid.arrange 不支持tmap图(还有?),它支持 ggplot2
有两种选择:
1)使用小倍数将两个值分配给审美(请参阅 tm_facets 的示例)。你的情节不使用美学,但你可以欺骗它,如下所示:
tm_shape(World,projection =merc) +
tm_fill(col = c(white,white))+
tm_layout(,inner.margins = c(-1.72,-2.05,-0.75,-1.56))+
tm_borders(alpha = 0.3,lwd = 2)
使用<$ c
library(grid)
grid。$ c $ grid> 包来定义视口: newpage()
pushViewport(viewport(layout = grid.layout(1,2)))
print(plot1,vp = viewport(layout.pos.col = 1))
print plot2,vp = viewport(layout.pos.col = 2))
另一件事,也可以使用 tm_shape 中的边界框参数:
tm_shape(World,projection =merc,xlim = c(-2e6,6.5e6),ylim = c(-4e6,8.5e6))+
tm_borders(alpha = 0.3,lwd = 2)
它生成相同的地图,但它的代码一个小清洁工。
Example:
I want to plot two tmap plots side by side, which are generated by this code.
library(tmap) library(gridExtra) data(World) plot1= tm_shape(World, projection = "merc") + tm_layout("", inner.margins=c(-1.72, -2.05, -0.75, -1.56)) + tm_borders(alpha = 0.3, lwd=2) plot2= tm_shape(World, projection = "merc") + tm_layout("", inner.margins=c(-1.72, -2.05, -0.75, -1.56)) + tm_borders(alpha = 0.3, lwd=2)
plot1 and plot2 work fine as single stand-alone plots:
Problem:
I have problems to put both plots side-by-side. I tried:
grid.arrange(plot1, plot2) throughs an error Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, : input must be grob!. I thought that this should work (using gridExtra) as tmap seems to be based on the grid graphics system.
Also par(mfrow=c(1,2)) does not work as it shows only one plot (guess this is related as tmap plot does not follow the base graphics system).
Question:
How can I plot both objects plot1 and plot2 side-by-side (ncol=2)?
Update:
Regarding the proposed grid-based solution, I get overlapping plots instead of two column arranged plots.
> sessionInfo() R version 3.1.2 (2014-10-31) Platform: x86_64-apple-darwin13.4.0 (64-bit) locale: [1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] tmap_1.0 dplyr_0.4.3 sp_1.1-1 loaded via a namespace (and not attached): [1] assertthat_0.1 class_7.3-11 classInt_0.1-23 colorspace_1.2-6 DBI_0.3.1 digest_0.6.8 e1071_1.6-4 ggplot2_1.0.1 [9] grid_3.1.2 gridBase_0.4-7 gtable_0.1.2 lattice_0.20-29 magrittr_1.5 MASS_7.3-35 munsell_0.4.2 parallel_3.1.2 [17] plyr_1.8.3 proto_0.3-10 R6_2.1.1 raster_2.3-40 RColorBrewer_1.1-2 Rcpp_0.12.2 reshape2_1.4.1 rgdal_0.8-16 [25] rgeos_0.3-11 scales_0.3.0 stringi_1.0-1 stringr_1.0.0 tools_3.1.2
Nice question!
grid.arrange doesn't support tmap plots (yet?) in the same way it supports ggplot2 plots.
There are two options:
1) Use small multiples by assigning two values to an aesthetic (see examples from tm_facets). Your plots don't use aesthetics, but you can trick this as follows:
tm_shape(World, projection = "merc") + tm_fill(col=c("white", "white")) + tm_layout("", inner.margins=c(-1.72, -2.05, -0.75, -1.56)) + tm_borders(alpha = 0.3, lwd=2)
2) Use the grid package to define viewports:
library(grid) grid.newpage() pushViewport(viewport(layout=grid.layout(1,2))) print(plot1, vp=viewport(layout.pos.col = 1)) print(plot2, vp=viewport(layout.pos.col = 2))
Another thing, rather than clipping the shape with negative inner margins, you could also use the bounding box arguments inside tm_shape:
tm_shape(World, projection = "merc", xlim=c(-2e6, 6.5e6), ylim=c(-4e6, 8.5e6)) + tm_borders(alpha = 0.3, lwd=2)
It produces the same map, but it the code a little cleaner.
这篇关于并排绘制2个tmap对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!