【问题标题】:Producing an inset map with the tmap package in R在 R 中使用 tmap 包生成插图
【发布时间】:2017-06-24 12:08:03
【问题描述】:

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

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

请参阅此处查看source of the above code。这实际上并没有显示美国和阿拉斯加/夏威夷的地图是如何组合的。至于我自己的编码尝试,我尝试了以下(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 问题没有任何内容。

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

【问题讨论】:

    标签: r gis viewport tmap


    【解决方案1】:

    您需要加载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,从马的嘴里(至少形象地说)
    • 您好,非常感谢您这么快回复我,我试过了,但在尝试安装网格时收到以下错误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
    • 我怀疑问题可能出在正在使用的 RI 版本上,尤其是最新(和最近更新的网格版本)是为较新版本的 R 构建的。[stat.ethz.ch/R-manual/R-devel/library/grid/DESCRIPTION]This是在您必须要求 IT 更新软件的计算机上工作的问题!如果这能解决问题,会通知您
    • 这也可能与基础 R 中包含网格有关? [链接] (stackoverflow.com/questions/29349398/r-package-grid-disappeared)
    • 万一有人还在关注这个,grid 现在可以工作了——我认为上面列出的包之一是以某种方式屏蔽视口工作。 save_tmap 代码在仅加载 tmap 时运行平稳插入仍然没有实际显示在 pdf 上。我尝试了各种 x/y/w/h 排列,但它们似乎没有任何区别......
    【解决方案2】:

    tmap demo documentation 中的最后一张图表插入了阿拉斯加和夏威夷。下面是代码,我刚刚删除了填充变量:

    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 页面,没想到在那里看!
    猜你喜欢
    • 2019-03-09
    • 2021-03-17
    • 1970-01-01
    • 2020-04-06
    • 2018-04-06
    • 2021-05-08
    • 1970-01-01
    • 2017-06-25
    • 2021-11-03
    相关资源
    最近更新 更多