Is it possible to include more than one tm_compass() on a map created by tmap?
I know it's probably unlikely that you'd need to, but say you wanted to show off the different compass types. Using nz from the spData package I tried adding each new compass as an additional layer, but it seems only the first one is included on the map.
library(spData)
library(tmap)
tm_shape(nz)+
tm_fill()+
tm_compass(type = 'arrow', position = c(0.1, 0.9))+
tm_compass(type = '4star', position = c(0.1, 0.8))+
tm_compass(type = '8star', position = c(0.1, 0.7))+
tm_compass(type = 'radar', position = c(0.1, 0.6))+
tm_compass(type = 'rose', position = c(0.1, 0.5))
If arrow isn't included, then 4star takes it's place:
tm_shape(nz)+
tm_fill()+
# tm_compass(type = 'arrow', position = c(0.1, 0.9))+
tm_compass(type = '4star', position = c(0.1, 0.8))+
tm_compass(type = '8star', position = c(0.1, 0.7))+
tm_compass(type = 'radar', position = c(0.1, 0.6))+
tm_compass(type = 'rose', position = c(0.1, 0.5))
Interesting question. As you point out, in normal use it is unlikely that one would need to display multiple compasses for the same map and that is probably why the default behavior of the tmap library does not handle this case.
That said, it is still possible to add all five tmap compasses to the same map using some workarounds! So, please find below the general "strategy":
Building 5 maps, each with one of the tmap compasses. Then convert these maps into 'grob' objects using the tmap::tmap_grob() function to extract the compasses with the help of the getGrob() function from the base R library grid.
Visualizing the compasses (without and with labels) using the cowplot library
Building the final map with the five compasses using the cowplot library
NB: when running the reprex just below, don't worry about the rendering of the different plots that will be displayed in your plotting device (as the rendering depends on the aspect ratio of the device); what matters is the rendering of the maps saved in the .png files.
Reprex
STEP 1 - EXTRACT EACH COMPASS TYPE FROM FIVE 'DUMMY' MAPS AS 'GROB' OBJECTS
library(tmap)
library(spData)
library(grid)
# Get a list named 'maps' containing 5 maps, each one with a different compass
compass_type <- c("arrow", "4star", "8star", "radar", "rose")
maps <- lapply(compass_type,
function(x)
tm_shape(nz) +
tm_fill() +
tm_compass(type = x, position = c(0.1, 0.7)))
# Get a list named 'compasses' containing the 5 compasses as 'grob' objects
compasses <- lapply(maps, function(x) getGrob(tmap_grob(x), gPath("compass")))
STEP 2 - VISUALIZATION OF THE FIVE tmap COMPASSES
Without labels
library(cowplot)
compasses_only <- plot_grid(plotlist = compasses,
nrow = 3,
ncol = 2,
scale = 0.8,
byrow = TRUE)
compasses_only
# Save the plot (need to install the 'rstudioapi' library)
# NB: adjust 'width' and 'height' params to get the best possible rendering
rstudioapi::savePlotAsImage(
"tmap_compasses.png", # add the path if different of the working directory
format = "png", # other possible formats: "jpeg", "bmp", "tiff", "emf", "svg", "eps"
width = 780,
height = 965
)
With labels
compasses_only_labeled <- plot_grid(plotlist = compasses,
nrow = 3,
ncol = 2,
scale = 0.8,
labels = compass_type,
label_size = 10,
label_x = c(0.42, 0.43, 0.43, 0.42, 0.44),
label_y = 0.1,
byrow = TRUE)
compasses_only_labeled
# Save the plot (need to install the 'rstudioapi' library)
# NB: adjust 'width' and 'height' params to get the best possible rendering
rstudioapi::savePlotAsImage(
"tmap_compasses_labeled.png",
format = "png",
width = 780,
height = 965
)
STEP 3 - BUILDING THE MAP WITH THE FIVE COMPASSES
# Get a 'nz' map (without any compass) as 'grob' object
nz_map <- tmap_grob(tm_shape(nz) + tm_fill())
# Convert 'compasses_only" into 'grob' object
compasses_only <- cowplot::as_grob(compasses_only)
# Build the map with the 5 compasses
map_with_compasses_2 <- ggdraw() +
draw_grob(nz_map) +
draw_grob(compasses_only,
width = 0.23, height = 0.65,
x = 0.28, y = 0.35,
hjust = 0,
vjust = 0,
halign = 0.5,
valign = 0.5,
scale = 0.8)
map_with_compasses_2
# Save the plot (need to install the 'rstudioapi' library)
# NB: adjust 'width' and 'height' params to get the best possible rendering
rstudioapi::savePlotAsImage(
"map_with_compasses_2.png",
format = "png",
width = 1500,
height = 900
)
Created on 2022-01-31 by the reprex package (v2.0.1)
Related
I want to create venn diagrams to emphasize that groups (circles) are completely located inside one another, i.e., there are no elements in the inner circles that are not simutanously in outer circles.
I've used ggvenn and arrived at these results:
colonias <- c("colônias")
possessoes <- c("possessões", colonias)
dominios <- c("domínios", possessoes, colonias)
ggvenn(tipologia_britanica,
show_elements = T,
label_sep = "\n",
fill_color = brewer.pal(name="Dark2", n=3),
fill_alpha = 0.6,
stroke_size = 0.2,
stroke_alpha = 0.2,
set_name_size = 5,
text_size = 5)
The result is tchnically correct because it show that "colonias" are common to all three groups and that "possessoes" are common to both "possessoes" and "dominios". But graphically I would like te groups to be completely inside one another to show that are no elements in "colonias" that are not common to all three, and in "possessoes" that are not common to "dominios". I'm not sure that ggvenn package is capable of plotting that.
One way may use the package eulerr.
However, your question isn't very clear so I let you play with the package
See the example below :
library(eulerr)
fit <- euler(c("A" = 10, "B" = 10, "A&B" = 8, "A&B&C"=3))
plot(fit,
fills = list(fill = c("red", "steelblue4","green"), alpha = 0.5),
labels = list(col = "black", font = 4),quantities = T)
I don't think ggvenn allows a plot with this kind of relationship. However, it's not terribly difficult to draw it yourself with ggplot and geom_circle from ggforce
ggplot(data.frame(group = c("domínios", "possessões", "colônias"),
r = c(3, 2, 1)),
aes(x0 = 3 - r, y0 = 0, fill = factor(group, group))) +
geom_circle(aes(r = r), alpha = 1) +
geom_text(aes(x = c(0, 1, 2), y = c(2.3, 1.3, 0), label = group),
size = 8) +
scale_fill_manual(values = c('#77bca2', '#e1926b', '#a09cc8'),
guide = 'none') +
coord_equal() +
theme_void()
I am using image() and contour() to create a "heatmap" of probabilities - for example:
I was asked to change the labels such that they "do not overlap the lines, and the lines are unbroken." After consulting ?contour(), I tried changing to method = "edge" and method = "simple", but both fail print the labels (although the lines are unbroken), and cant seem to find posts regarding similar issues elsewhere.
Any advice on how to manipulate the labels to appear adjacent to (not on top of) unbroken lines would be much appreciated. I would prefer base R but also would welcome options from more flexible packages or alternative base R functions.
Minimal code to recreate example figure is here:
# Generate Data
Rs <- seq(0.02, 1.0, 0.005)
ks <- 10 ^ seq(-2.3, 0.5, 0.005)
prob <- function(Y,R,k) {
exp(lgamma(k*Y+Y-1) - lgamma(k*Y) - lgamma(Y+1) + (Y-1) * log(R/k) - (k*Y+Y-1) * log(1+R/k))
}
P05 <- matrix(NA, ncol = length(ks), nrow = length(Rs))
for(i in 1:length(Rs)) {
for(j in 1:length(ks)) {
P05[i,j] <- 1 - sum(prob(1:(5 - 1), Rs[i], ks[j]))
}
}
colfunc <- colorRampPalette(c("grey25", "grey90"))
lbreaks <- c(-1e-10, 1e-5, 1e-3, 5e-3, 1e-2, 2e-2, 5e-2, 1e-1, 1.5e-1, 1)
## Create Figure
image(Rs, ks, P05,
log="y", col = rev(colfunc(length(lbreaks)-1)), breaks = lbreaks, zlim = lbreaks,
ylim = c(min(ks), 2), xlim = c(0,1))
contour(Rs, ks, P05, levels = lbreaks, labcex = 1, add = TRUE)
There is an easy(ish) way to do this in ggplot, using the geomtextpath package.
First, convert your matrix to an x, y, z data frame:
df <- expand.grid(Rs = Rs, ks = ks)
df$z <- c(P05)
Now plot a filled contour, and then geom_textcontour. By default the text will break the lines, as in contour, but if you set the vjust above one or below zero the lines will close up as they don't need to break for the text.
I've added a few theme and scale elements to match the aesthetic of the base graphics function. Note the text and line size, color etc remain independently adjustable.
library(geomtextpath)
ggplot(df, aes(Rs, ks, z = z)) +
geom_contour_filled(breaks = lbreaks) +
geom_textcontour(breaks = lbreaks, color = 'black', size = 5,
aes(label = stat(level)), vjust = 1.2) +
scale_y_log10(breaks = c(0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2),
expand = c(0, 0)) +
scale_fill_manual(values = rev(colfunc(9)), guide = 'none') +
scale_x_continuous(expand = c(0, 0)) +
theme_classic(base_size = 16) +
theme(axis.text.y = element_text(angle = 90, hjust = 0.5),
axis.ticks.length.y = unit(3, 'mm'),
plot.margin = margin(20, 20, 20, 20))
The contour function is mostly written in C, and as far as I can see, it doesn't support the kinds of labels you want.
So I think there are two ways to do this, neither of which is very appealing:
Modify the source to the function. You can see the start of the labelling code here. I think you would need to rebuild R to incorporate your changes; it's not easy to move a function from a base package to a contributed package.
Draw the plot with no labels, and add them manually after the fact. You could add them using text(), or produce an output file and use an external program to edit the output file.
I am trying to do something similar to what is described in the blog here but using R with ggtree, ggmap, and ggplot2.
I want to be able to combine the plots of the phylogenetic tree and the map showing the sampling locations of the tips on a geographical map, and link the tips to the sampling locations by segments. That would allow to see ie. if some clusters appears to specific geographical locations (ie north, south of an area) and would allow also to display different data with tips colors/symbols. This would be at first used as exploratory graphs, but this can also be used later on for publication ...
I would like to use the gg* libraries (ggplot2, ggtree, ggmap ...) to do that, because then it is easy to modify plots to display different variables. Here is a dummy script to describe how I do that so far. I do the tree and map plot separately and combine them. I want also to be able to have a common legend for the two plots. I am stuck after combining, I do not find out how to link the points from the tree plot to the points on the map plot with segments.
Anyone with ideas / possible solutions on how to do that or an alternative approach ?
Here is the dummy dataset to illustrate for creating the plots
library(patchwork)
library(ggpubr)
library(ggtree)
library(tidyverse)
library(ggmap)
library(ggplot2)
mytree <- ggtree::rtree(100)
mymap <- ggmap::get_map(c(left = 0.903, bottom = 44.56, right = 6.72, top = 49.38),
scale = 4, maptype = "terrain",
source = "stamen",
color = "bw")
save(mymap, file = "dummy_map.Rdata")
ggmap require API key to create the map (sorry I cannot share the API key, but you can make one for free on google cloud). I saved the map object and its downloadable from here.
# Loading the map
load("dummy_map.Rdata")
# creating dummy metadata
mytree_data <- tidytree::as_tibble(mytree)
mymetadata <- mytree_data %>%
dplyr::filter(!is.na(label)) %>%
tibble::add_column(year = sample(seq(1990, 2020, by = 1), 100, replace = T),
lon = sample(seq(0.91, 6.7, by = 0.01), 100, replace = T),
lat = sample(seq(44.56, 49.38, by = 0.01), 100, replace = T)) %>%
dplyr::rename(id = label) %>%
dplyr::select(id, year, lat, lon)
# plotting the phylogenetic tree
# phylogenetic tree example
mytree_plot <-
ggtree::ggtree(mytree, layout = "rectangular", ladderize = T, lwd = .2) %<+%
mymetadata +
geom_tippoint(aes(color = year), size = 1, show.legend = T) +
scale_color_gradient(low='red', high="blue", space = "Lab",
limits = c(NA, NA), na.value = "black",
n.breaks = 8,
guide = "colorbar") +
geom_tiplab(aes(label = label), size = 1, offset = -1E-10) +
geom_treescale(fontsize = 2, linesize = 0.5, offset = 1) +
theme(legend.position = c(0.9,0.15),
legend.title = element_text(size = 8),
legend.text = element_text(size = 6),
plot.title = element_text(hjust = 1))
mytree_plot
For some reason, I have to add the theme to be able to see the legend for the points, it is not created automatically. This should not occur. If anyone see what I am doing wrong here please let me know.
Then I add the sampling locations on the map, and deactivate the legend that is common with the tree legend
mymap_plot <- ggmap(mymap, n_pix = 340, darken = c(0.6, "white"))+
geom_point(data = mymetadata,
aes(x = lon, y = lat, color = year),
size = 2, alpha = .8, na.rm = T) +
scale_color_gradient(low='red', high="blue", space = "Lab",
limits = c(NA, NA),
n.breaks = 8,
guide = "colorbar") +
guides(color = F)
mymap_plot
Then I combine the tree plot and the map plot together. I tried with "patchwork" and "ggpubr" packages.
So far it appear easier to combine plots and draw a single legend with ggpubr, so this is currently my first choice at combining plot
# combining plots with patchwork
combined_plot <- mytree_plot + mymap_plot
# combining plots with ggpubr
# which I like better because it allows to combine the legends which is usefull
# when more variables are used ie shape for uncertainty location
other_combined <- ggarrange(mytree_plot, mymap_plot,
ncol = 2,
labels = c("A", "B"),
align = "hv",
legend = "bottom",
common.legend = T)
Here is the combined plot of the phylogenetic tree (ggtree) and the map (ggmap) obtained with ggpubr.
I am stuck at this point.
I need a way to add segments between corresponding points at the tips of the tree to the corresponding sampling locations of each tip on the map
Any solutions/ideas on how I could do that?
As shown in the map below, how can I place the legend to the last cell in the grid?
The code I used is
psp1 <- tm_shape(province) +
tm_borders(col = 'black') +
tm_shape(county) +
tm_polygons(col = 'estimate', title = 'Changes in %', style = 'fixed', palette = brewer.pal(n = 6, name = 'Spectral'),
breaks = c(-15, -10, -5, 0, 5, 10, 15), legend.hist = F) +
tm_facets('warming', ncol = 2) +
tm_shape(province) +
tm_borders(col = 'black') +
tm_compass(north = 0, type = 'arrow', show.labels =0, position = c('right','top')) +
tm_layout(legend.format = list(fun = function(x) formatC(x, digits = 1, format = "f")),
fontface = 'bold',
legend.text.size = 1.3,
legend.width = 0.2,
legend.title.size = 1.5,
panel.label.size = 1.5,
panel.label.fontface = 'bold')
The data can be found from here. Thanks.
My answer comes perhaps a little late... (almost one year after your request!) Anyway, your question is very interesting and I hope this answer will be useful, either now or for future projects, for you or other SO users (by the way, thanks for having kept your input data accessible for a year ;-)).
As far as I know, it is not possible to solve your problem by using the tm_facets() function of the tmap library. So I suggest a slightly different "strategy" (still using the tmap library) to get what you are looking for.
It is articulated in two steps:
Build the maps and the legend manually... fortunately, not quite manually since the solution I suggest uses one custom function (i.e. make_graph()) that is run through a Map() function.
Edit the map mosaic with the legend using the R base grid library. Again, the implementation is made easier by the use of one custom function (i.e. Maps_setup()) run through a Map() function.
So, please find below a reprex that details the approach.
Reprex
STEP 1 - BUILDING THE MAPS AND THE LEGEND
library(sf)
library(tmap)
library(RColorBrewer)
# Import data
province <- st_read("province.shp")
county <- st_read("county.shp")
# Split the 'sf' object 'county' into a list of five 'sf' objects corresponding
# to the five warming scenarios (i.e. the first five facets of the final figure)
county_warm_list <- split(county , f = county$warming)
# Build the function 'make_graph' to generate the maps
make_graph <- function(x,y){
results <- tm_shape(x,
is.master = TRUE) +
tm_polygons(col = 'estimate',
title = 'Changes in %',
style = 'fixed',
palette = brewer.pal(n = 6, name = 'Spectral'),
breaks = c(-15, -10, -5, 0, 5, 10, 15),
legend.hist = FALSE,
midpoint = 0) +
tm_shape(province) +
tm_borders(col = 'black') +
tm_compass(north = 0,
type = 'arrow',
show.labels = 0,
position = c(0.93, 0.87),
size = 1.2) +
tm_layout(legend.show = FALSE,
# NB: the use of the 'get_asp_ratio()' function enables
# to optimize the size of each map inside its own facet:
asp = tmaptools::get_asp_ratio(x),
panel.labels = y,
panel.label.size = 0.8,
panel.label.fontface = 'bold',
inner.margins = c(0.02, 0.02, 0.02, 0.02))
return(results)
}
# Run the 'make_graph()' function through the list of the five 'sf' objects (i.e.
# 'county_warm_list') to generate the maps with their respective title using
# the 'Map()' function
map_titles <- names(county_warm_list)
Maps_list <- Map(make_graph, county_warm_list, map_titles)
# Build the legend using only the object "county"
Maps_legend <- tm_shape(county) +
tm_polygons(col = 'estimate',
title = 'Changes in %',
style = 'fixed',
palette = brewer.pal(n = 6, name = 'Spectral'),
breaks = c(-15, -10, -5, 0, 5, 10, 15),
legend.hist = FALSE,
midpoint = 0) +
tm_layout(legend.only = TRUE,
legend.position = c("center", "center"),
legend.format = list(fun = function(x) formatC(x, digits = 1, format = "f")),
fontface = 'bold',
legend.text.size = 1.3,
legend.width = 0.2,
legend.title.size = 1.5)
# Add the legend to 'Maps_list'
Maps_list$Legend <- Maps_legend
At the end of this first step, you get a list (i.e. Maps_list) containing 6 elements (i.e. five maps and one legend).
STEP 2 - LAYOUT OF THE MOSAIC OF MAPS WITH THE LEGEND
library(grid)
grid.newpage()
# Build the function 'Maps_setup' to set up the layout
Maps_setup <- function(x,y,z){
pushViewport(viewport(layout = grid.layout(nrow = 3, ncol = 2,
widths = unit(7.32, "cm"),
heights = unit(5, "cm"))))
setup <- print(x, vp = viewport(layout.pos.row = y, layout.pos.col = z))
return(setup)
}
# Run the 'Maps_setup()' function through the six objects of 'Maps_list' (i.e.
# 5 maps + 1 legend) to place the maps and the legend on the page using
# the 'Map()' function
# The 'pos_row' and 'pos_col' vectors are used to indicate where to place the
# maps as the 'Maps_setup()' function works through the list
pos_row <- rep(1:3, each = 2)
pos_col <- rep(1:2, times = 3)
Final_Results <- Map(Maps_setup, Maps_list, pos_row, pos_col)
Created on 2022-01-25 by the reprex package (v2.0.1)
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.
You'll need to load the grid package. So, this should work
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)
I'll update the US choropleth demo soon, with a save_tmap example.
The last chart in the tmap demo documentation has insets of Alaska and Hawaii. Below is the code, I've just removed the fill variable:
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)
After setting up the shapefiles, here is where the viewport windows are added to the plot:
# 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))
Source: I've adapted this from https://github.com/mtennekes/tmap/tree/master/demo/USChoropleth