使用R中的tmap包制作插图地图

7

我正在尝试在英国的大地图旁边制作伦敦的嵌入地图。 我使用的是“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):

  1. 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)
    
  2. 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文件,但仅包含英国地图。因此,

  1. 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
  1. 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"
    
我知道其他人在其他软件包中制作插图时遇到了困难,并且已经有关于其他软件包的问题被提出并解决,尤其是在ggplot中(由于链接限制,我无法链接到这些问题),但据我所知,在这个特定的tmap问题上还没有任何信息。这是我在这里的第一个问题,如果布局有任何错误,请见谅。
2个回答

5
您需要加载 grid 包。所以,应该可以这样操作。
library(grid)
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)

我将很快更新美国区域地图演示,加入 save_tmap 的示例。


好的,@SJPG,这就是从马嘴里说出来的(至少在比喻上)。 - Phil
非常感谢您能如此迅速地回复我。我尝试了这个方法,但在安装grid时遇到了以下错误: `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` - SJPG
我怀疑问题很可能在于我使用的 R 版本,尤其是最新版本(也是最近更新的版本)的 grid 是为较新的 R 版本构建的。[https://stat.ethz.ch/R-manual/R-devel/library/grid/DESCRIPTION] 在一台必须要求 IT 更新软件的计算机上工作时就会遇到这个问题!如果解决了问题,我会让你知道的。 - SJPG
这可能也与grid被包含在基本R中有关?[链接](https://dev59.com/qV0b5IYBdhLWcg3wT_tr) - SJPG
如果还有人在关注这个问题,现在网格是正常工作的 - 我认为上面列出的包之一在某种程度上屏蔽了视口的工作。当仅加载tmap时,save_tmap代码可以平稳运行,但嵌入框仍然无法真正显示在pdf上。我已经尝试了各种x / y / w / h排列组合,但它们似乎没有任何区别... - SJPG
一年后再次关注这个帖子,我理解得没错吧,演示已经更新了(澄清了2017年2月的任何混淆),而回答这个问题的人只是使用了那个演示?无论如何,演示非常清晰,而这个答案则有些简略。也许可以直接在答案中添加链接? - user3386170

3
tmap演示文档 的最后一个图表中,有阿拉斯加和夏威夷的插图。下面是代码,我只删除了填充变量:
library("readxl")
library("maptools")
library("grid")
library("tmap")
library("tmaptools")

# function to obtain US county shape
get_US_county_2010_shape <- function() {
  dir <- tempdir()
  download.file("http://www2.census.gov/geo/tiger/GENZ2010/gz_2010_us_050_00_20m.zip", destfile = file.path(dir, "gz_2010_us_050_00_20m.zip"))
  unzip(file.path(dir, "gz_2010_us_050_00_20m.zip"), exdir = dir)
  read_shape(file.path(dir, "gz_2010_us_050_00_20m.shp"))
}

# obtain US county shape
US <- get_US_county_2010_shape()

# split shape 
US_cont <- US[!(US$STATE %in% c("02","15","72")),]  
US_AK <- US[US$STATE == "02", ]
US_HI <- US[US$STATE == "15",]

# create state boundaries
US_states <- unionSpatialPolygons(US_cont, IDs=US_cont$STATE)

设置完shapefile后,这里是将视口窗口添加到绘图中的位置:

# change back to the plotting mode
tmap_mode("plot")

# plot contiguous US
tm_shape(US_cont, projection=2163) +
  tm_polygons(border.col = "grey50", border.alpha = .5, title = "", showNA = TRUE) +
  tm_shape(US_states) +
  tm_borders(lwd=1, col = "black", alpha = .5) +
  tm_credits("Data @ Unites States Department of Agriculture\nShape @ Unites States Census Bureau", position = c("right", "bottom")) +
  tm_layout(title.position = c("center", "top"), 
            legend.position = c("right", "bottom"), 
            frame = FALSE, 
            inner.margins = c(0.1, 0.1, 0.05, 0.05))

# Alaska inset
m_AK <- tm_shape(US_AK, projection = 3338) +
  tm_polygons(border.col = "grey50", border.alpha = .5, breaks = seq(10, 50, by = 5)) +
  tm_layout("Alaska", legend.show = FALSE, bg.color = NA, title.size = 0.8, frame = FALSE)

# Hawaii inset
m_HI <- tm_shape(US_HI, projection = 3759) +
  tm_polygons(border.col = "grey50", border.alpha = .5, breaks=seq(10, 50, by = 5)) +
  tm_layout(legend.show = FALSE, bg.color=NA, title.position = c("LEFT", "BOTTOM"), title.size = 0.8, frame=FALSE)

# print insets
print(m_AK, vp=viewport(x= 0.15, y= 0.15, width= 0.3, height= 0.3))
print(m_HI, vp=viewport(x= 0.4, y= 0.1, width= 0.2, height= 0.1))

来源:我从https://github.com/mtennekes/tmap/tree/master/demo/USChoropleth进行了修改。


非常感谢您的帮助!我会在今天下午尝试着解决这个问题,并且告诉您我的进展情况。很抱歉我没有查看您提供的Github页面,我没有想到去那里看! - SJPG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接