Creating a map insert with tmap package in R

I am trying to create an insert map of London along with a larger map of the UK. I use the "tmap" package, which I found to be a great package, and it is especially easy to switch to using ggplot2 for a while. However, the documentation on how to create an insert map using tmap is a bit unclear. The reference guide describes how you can create an insert map using:

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

but it is unclear how to use the second command, insets_vp. I found only one example that actually offers the suggested syntax for creating an insert using tmap:

    alaska <- tm_shape(shp_alaska) + … print(alaska, vp=viewport(x=.1, 
    y=.15, width=.2, height=.3)) 

See the source of the above code here . This does not actually show how the maps of the USA and Alaska / Hawaii are combined. As for my own encoding attempts, I tried the following: all downloaded files (dplyr, magrittr, rgdal, GISTools, RColorBrewer, tmap loaded, R vn 3.3.2, RStudio 1.0.136):

  • First, I create two polygons of tmap objects and points for the entire 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)
    
  • Then I try to save a pdf file that combines both objects:

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

It prints pdf, but only with a map of the UK. In this way,

  1. I am trying to add insets_vp to 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)
    

But this gives the following error code:

    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. Then I will try to combine the suggested syntax for printing (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 .

, .

+4
2

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.

+3

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)

, :

# 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

+2

Source: https://habr.com/ru/post/1669024/


All Articles