我正在尝试在英国的大地图旁边制作伦敦的嵌入地图。 我使用的是“tmap”包,我发现这是一个非常优秀的包,并且在使用了ggplot2一段时间后移动起来特别容易。 但是,有关如何使用tmap生成嵌入地图的文档有点不清楚。 参考手册描述了如何使用以下内容生成嵌入地图:
save_tm(...insets_tm = NULL, insets_vp = NULL)
但第二个命令insets_vp的使用方式不太清楚。我只找到了一个实际提供使用tmap生成插图语法建议的示例:
alaska <- tm_shape(shp_alaska) + … print(alaska, vp=viewport(x=.1,
y=.15, width=.2, height=.3))
请点击这里查看以上代码的来源。但是,它并没有展示如何将美国地图和阿拉斯加/夏威夷地图合并在一起。至于我的编程尝试,我已经尝试了以下方法(dplyr、magrittr、rgdal、GISTools、RColorBrewer和tmap都已加载,R vn 3.3.2,RStudio 1.0.136):
I first create two tmap objects polygon and points for all of the UK (UK_Im_Sec) and London (London_Im_Sec):
UK_Im_Sec<-tm_shape(UKNI_LA_ll, is.master = TRUE)+ tm_borders(lwd=0.25)+ tm_shape(Immobile_residuals)+ tm_dots(col="Sec_Name", style="cat", palette="Set1", title="Socio-economic background (NS-SEC)")+ tm_layout(title="Mapping outlier residuals - non-predicted 'immobility' (Social class)", title.size = 3.0, title.position=c("center","TOP"),legend.outside = TRUE, legend.outside.position = "right",frame = FALSE) LDN_Im_Sec<-tm_shape(Immobile_resids_LDN)+ tm_dots(col="Sec_Name", style="cat", palette="Set1", size = 0.25,title="Socio-economic background (NS-SEC)")+ tm_shape(LDN_Poly, is.master = TRUE)+ tm_borders(lwd=0.25)+ tm_text(text="NAME", size = 0.6, auto.placement=TRUE)+ tm_layout("London",title.position = c("center", "BOTTOM"),legend.outside = TRUE, legend.outside.position = "right", frame = FALSE)
I then try to save out a pdf which combines both objects:
save_tmap(UK_Im_Sec,insets_tm = LDN_Im_Sec,filename="ZRMdlNoRg_SEC_-3to-5SDs_ImmobResids_FINAL.pdf", dpi=600)
这将打印PDF文件,但仅包含英国地图。因此,
I try and add insets_vp into the code:
save_tmap(UK_Im_Sec,insets_tm = LDN_Im_Sec,insets_vp=UK_Im_Sec, filename="ZRMdlNoRg_SEC_-3to-5SDs_ImmobResids_FINAL.pdf", dpi=600)
Error in save_tmap(UK_Im_Sec, insets_tm = LDN_Im_Sec, insets_vp = UK_Im_Sec, :
Insets and/or its viewports not in the correct format
I then try to combine the suggested syntax for print(x, viewport=(x=,y=,h=,w=) with insets_vp, as follows:
save_tmap(UK_Im_Sec,insets_tm = LDN_Im_Sec,insets_vp=viewport(x=2, y=.15, width=.2, height=.3), filename="ZRMdlNoRg_SEC_-3to-5SDs_ImmobResids_FINAL.pdf", dpi=600) Error in inherits(insets_vp, "viewport") : could not find function "viewport"
`Warning in install.packages :
package ‘grid’ is not available (for R version 3.3.2) Warning in install.packages : package ‘grid’ is a base package, and should not be updated` - SJPGsave_tmap
代码可以平稳运行,但嵌入框仍然无法真正显示在pdf上。我已经尝试了各种x / y / w / h排列组合,但它们似乎没有任何区别... - SJPG