我正在尝试制作一张伦敦的插图和一张更大的英国地图。我正在使用包" 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):
我首先为所有英国(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)
然后我尝试保存一个结合了两个对象的pdf:
save_tmap(UK_Im_Sec,insets_tm = LDN_Im_Sec,filename="ZRMdlNoRg_SEC_-3to-5SDs_ImmobResids_FINAL.pdf", dpi=600)
这将打印pdf,但仅显示英国地图。所以,
我尝试将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
然后我尝试将print(x,view =(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问题没有任何内容。
这是我在这里的第一个问题,所以对于提出问题时的任何错误表示歉意。
答案 0 :(得分:3)
您需要加载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
示例更新美国的等值线演示。
答案 1 :(得分: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
改编了这个