I am trying to produce an inset map of London alongside a larger map of the UK. I'm using the package "tmap" which I have found to be an excellent package and particularly easy to move to having used ggplot2 for a while. However, the documentation on how to produce an inset map using tmap is a bit unclear. The reference manual describes how it should be possible to produce an inset map using:
save_tm(...insets_tm = NULL, insets_vp = NULL)
but it is not clear how the second command, insets_vp, should be used. I have only found one example which actually provides suggested syntax for producing an insetmap using tmap:
alaska <- tm_shape(shp_alaska) + … print(alaska, vp=viewport(x=.1,
y=.15, width=.2, height=.3))
See here for the source of the above code. This doesn't actually show how the map of the USA and Alaska/Hawaii are combined. As for my own attempts at coding, I have tried the following (dplyr, magrittr, rgdal, GISTools, RColorBrewer, tmap are all loaded, R vn 3.3.2, RStudio 1.0.136):
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)
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)
This prints the pdf but only with the map of the UK. So,
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)
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
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"
I know that other people have had difficulty producing inset maps in other packages and that there are questions that have already been asked and resolved for other packages, notably in ggplot (I can't link to the questions because of limits on links), but as far as I know there is nothing on this particular tmap issue.
This is my first question here so apologies for any errors in laying out the question.