7

我正在尝试制作一张伦敦的插图以及一张更大的英国地图。我正在使用包“tmap”,我发现它是一个很好的包,并且特别容易转移到使用 ggplot2 一段时间。但是,关于如何使用 tmap 生成插图的文档有点不清楚。参考手册描述了如何使用以下方法生成插图:

    save_tm(...insets_tm = NULL, insets_vp = NULL) 

但不清楚应该如何使用第二个命令 insets_vp。我只找到了一个示例,它实际上提供了使用 tmap 生成 insetmap 的建议语法:

    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. 我首先为英国 (UK_Im_Sec) 和伦敦 (London_Im_Sec) 创建了两个 tmap 对象多边形和点:

    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. 然后我尝试保存一个结合了两个对象的pdf:

    save_tmap(UK_Im_Sec,insets_tm = LDN_Im_Sec,filename="ZRMdlNoRg_SEC_-3to-5SDs_ImmobResids_FINAL.pdf", dpi=600)
    

这会打印 pdf,但仅限于英国地图。所以,

  1. 我尝试将 insets_vp 添加到代码中:

    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. 然后我尝试将 print(x, viewport=(x=,y=,h=,w=) 的建议语法与 insets_vp 结合起来,如下所示:

    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 问题没有任何内容。

这是我在这里的第一个问题,因此对在提出问题时出现的任何错误表示歉意。

4

2 回答 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)

我将很快更新美国 choropleth 演示,并举个save_tmap例子。

于 2017-02-07T10:58:46.717 回答
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改编了这个

于 2017-02-07T10:37:04.043 回答