将2个tmap对象并排绘制

时间:2015-12-17 21:20:39

标签: r graphics choropleth tmap

示例:

我想并排绘制两个tmap图,这些图是由此代码生成的。

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)

plot1plot2可以作为单独的独立情节工作: enter image description here

问题:

我有问题将两个地块放在一起。我试过了:

grid.arrange(plot1, plot2)错误Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, : input must be grob!。我认为这应该有用(使用gridExtra)因为tmap似乎是基于grid graphics system

同样par(mfrow=c(1,2))不起作用,因为它只显示一个图(猜测这是相关的tmap图不符合base graphics system)。

问题:

如何并排绘制plot1plot2对象(ncol=2)?

更新

关于提出的基于网格的解决方案,我得到重叠的图,而不是两列排列的图。

enter image description here

> 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 

3 个答案:

答案 0 :(得分:5)

好问题!

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)

2)使用grid包定义视口:

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

另一件事,你可以使用tm_shape内的边界框参数,而不是使用负内边距裁剪形状:

tm_shape(World, projection = "merc", xlim=c(-2e6, 6.5e6), ylim=c(-4e6, 8.5e6)) + 
  tm_borders(alpha = 0.3, lwd=2)

它生成相同的地图,但代码更清晰。

答案 1 :(得分:3)

tmap_arrange()

https://cran.r-project.org/web/packages/tmap/tmap.pdf

data(World)
w1 <- qtm(World, projection = "eck4", title="Eckert IV")
w2 <- qtm(World, projection = "merc", title="Mercator")
w3 <- qtm(World, projection = "wintri", title="Winkel-Tripel")
w4 <- qtm(World, projection = "robin", title="Robinsin")

current.mode <- tmap_mode("plot")
tmap_arrange(w1, w2, w3, w4)
tmap_mode(current.mode)

答案 2 :(得分:0)

我想知道在此期间是否可以并排绘制以下示例中的5幅地图而不切换到tm_facets(ncol=5)

data(NLD_prov, NLD_muni)

tmap_mode("plot")
tm_shape(NLD_muni) +
tm_fill(c("pop_0_14", "pop_15_24", "pop_25_44", "pop_45_64", "pop_65plus"),
        style="kmeans", 
        palette=list("Oranges", "Greens", "Blues", "Purples", "Greys"),
        title=c("Population 0 to 14", "Population 15 to 24", "Population 25 to 44",
                "Population 45 to 64", "Population 65 and older")) +
tm_shape(NLD_prov) +
tm_borders() +
tm_format_NLD(frame = TRUE, asp=0)