Mapping with the tmap package
Setup
# Activating packages
suppressWarnings(suppressMessages({
library(RCurl)
library(tidyverse) # Modern data science workflow
library(sf) # Encodes spatial vector data
library(tmap)
library(RColorBrewer) # ColorBrewer Palettes
library(ggplot2) # Modern visualization tools
library(patchwork) # make it simple to combine separate ggplots
library(plotly) # Create interactive web graphics from 'ggplot2'
library(ggrepel) ## For displaying labels on ggplot2 object
}))
# Adjustments
knitr::opts_chunk$set(fig.align = 'center')
options(warn = -1)
options(scipen = 10000)
options(repr.plot.width = 6, repr.plot.height = 6)
Import data
#download.file("https://geodacenter.github.io/data-and-lab/data/nepal.zip",destfile="nepal.zip",method="libcurl")
#unzip("nepal.zip")
gdf <- read_sf("nepal.shp")
glimpse(gdf)
Transform data
st_crs(gdf)
gdf$area <- st_area(gdf)
Quick map
qtm(gdf, fill = "povindex")
Simple map
tm_shape(gdf) +
tm_fill("povindex") +
tm_layout(frame = FALSE)
Outside legend
tm_shape(gdf) +
tm_fill("povindex") +
tm_layout(legend.outside = TRUE, frame = FALSE)
Color palettes
tm_shape(gdf) +
tm_fill("povindex", palette = "-RdYlBu") +
tm_layout(frame = FALSE)
Add labels
tm_shape(gdf) +
tm_fill("povindex", palette = "-RdYlBu") +
tm_layout(frame = FALSE) +
tm_text("district", size = "area", auto.placement = T, legend.size.show = F)
Add selected labels
tm_shape(gdf) +
tm_fill("povindex", palette = "-RdYlBu") +
tm_layout(frame = FALSE) +
tm_shape(gdf %>% filter(povindex > 35)) +
tm_text("district", size = "area", auto.placement = T, legend.size.show = F)
Add scale bar
tm_shape(gdf) +
tm_fill("povindex", palette = "-RdYlBu") +
tm_layout(frame = FALSE) +
tm_shape(gdf %>% filter(povindex > 35)) +
tm_text("district", size = "area", auto.placement = T, legend.size.show = F) +
tm_scale_bar(position = c("LEFT", "BOTTOM"))
Quantile map
tm_shape(gdf) +
tm_fill("povindex", palette = "-RdYlBu", style = "quantile", n = 5) +
tm_layout(frame = FALSE) +
tm_shape(gdf %>% filter(povindex > 35)) +
tm_text("district", size = "area", auto.placement = T, legend.size.show = F) +
tm_scale_bar(position = c("LEFT", "BOTTOM"))
Add histogram
tm_shape(gdf) +
tm_fill("povindex", palette = "-RdYlBu", style = "quantile", n = 5, legend.hist = TRUE) +
tm_layout(frame = FALSE, legend.outside = TRUE) +
tm_shape(gdf %>% filter(povindex > 35)) +
tm_text("district", size = "area", auto.placement = T, legend.size.show = F) +
tm_scale_bar(position = c("LEFT", "BOTTOM")) +
tm_borders(alpha=0.2)
Add compass
tm_shape(gdf) +
tm_fill("povindex", palette = "-RdYlBu", style = "quantile", n = 5, legend.hist = TRUE) +
tm_layout(frame = FALSE, legend.outside = TRUE) +
tm_shape(gdf %>% filter(povindex > 35)) +
tm_text("district", size = "area", auto.placement = T, legend.size.show = F) +
tm_scale_bar(position = c("LEFT", "BOTTOM")) +
tm_borders(alpha=0.2) +
tm_compass(type = "8star",
position = c("RIGHT", "TOP"),
show.labels = 2,
text.size = 0.35)
Edit the layout
tm_shape(gdf) +
tm_fill("povindex", title = "Poverty index", palette = "-RdYlBu", style = "quantile", n = 5, legend.hist = TRUE) +
tm_shape(gdf %>% filter(povindex > 35)) +
tm_text("district", size = "area", auto.placement = T, legend.size.show = F) +
tm_scale_bar(position = c("LEFT", "BOTTOM")) +
tm_borders(alpha=0.2) +
tm_compass(type = "8star",
position = c("RIGHT", "TOP"),
show.labels = 2,
text.size = 0.35) +
tm_layout(legend.text.size = 0.7,
legend.title.size = 1,
legend.position = c("right", "bottom"),
legend.outside = TRUE,
frame = FALSE
)
tm_shape(gdf) +
tm_fill("povindex", title = "Poverty index", palette = "-RdYlBu", style = "quantile", n = 5, legend.hist = TRUE) +
tm_borders(alpha=0.3) +
tm_shape(gdf %>% filter(povindex > 41.79)) +
tm_text("district", size = "area", root = 3, auto.placement = T, legend.size.show = F) +
tm_scale_bar(position = c("LEFT", "BOTTOM")) +
tm_compass(type = "8star",
position = c("RIGHT", "TOP"),
show.labels = 2,
text.size = 0.35) +
tm_layout(legend.text.size = 0.7,
legend.title.size = 1,
legend.position = c("right", "bottom"),
legend.outside = TRUE,
frame = FALSE,
inner.margins = c(0.01, 0.01, 0.01, 0.01)
)