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)
Rows: 75
Columns: 62
$ id <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
$ name_1 <chr> NA, NA, NA, NA, "Central", "Central", "Central", "Central",…
$ name_2 <chr> "Dhaualagiri", "Dhaualagiri", "Dhaualagiri", "Dhaualagiri",…
$ district <chr> "Baglung", "Mustang", "Myagdi", "Parbat", "Bhaktapur", "Dha…
$ depecprov <dbl> 27.01, 31.51, 30.03, 21.84, 22.58, 20.85, 31.00, 21.14, 23.…
$ povindex <dbl> 27.33, 31.16, 28.54, 24.62, 19.43, 33.38, 22.45, 27.34, 19.…
$ pcinc <dbl> 354, 1189, 636, 627, 854, 607, 1710, 866, 1172, 672, 941, 6…
$ pcincppp <dbl> 573, 1922, 1028, 1013, 1379, 982, 2764, 1399, 1894, 1086, 1…
$ pcincmp <dbl> 25613, 85957, 45986, 45289, 61686, 43899, 123601, 62559, 84…
$ malkids <dbl> 42.9, 54.7, 47.5, 26.2, 26.9, 26.3, 32.0, 21.7, 16.2, 37.1,…
$ lif40 <dbl> 8.52, 12.02, 7.27, 6.11, 3.31, 7.68, 4.39, 6.23, 4.03, 6.77…
$ nosafh20 <dbl> 11.12, 8.32, 12.56, 17.47, 18.25, 15.39, 30.00, 20.58, 30.4…
$ population <dbl> 250065, 11585, 109598, 145657, 296705, 333978, 1688131, 375…
$ boyg1_5 <dbl> 29144, 784, 9035, 11202, 16533, 29829, 89918, 30818, 29250,…
$ girlg1_5 <dbl> 31123, 806, 9033, 11280, 15271, 30285, 82314, 31600, 27764,…
$ kids1_5 <dbl> 60267, 1590, 18068, 22482, 31804, 60114, 172232, 62418, 570…
$ schoolcnt <dbl> 554, 70, 273, 369, 355, 634, 1370, 696, 488, 521, 113, 556,…
$ schlpkid <dbl> 9.1924, 44.0252, 15.1096, 16.4131, 11.1621, 10.5466, 7.9544…
$ schlppop <dbl> 2.2154240, 6.0422961, 2.4909214, 2.5333489, 1.1964746, 1.89…
$ ad_illit <dbl> 34.46, 38.65, 34.84, 32.43, 21.87, 46.74, 15.96, 37.23, 20.…
$ ad_ilgt50 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0,…
$ lon <dbl> 83.25244, 83.85990, 83.46196, 83.68089, 85.44291, 84.96012,…
$ lat <dbl> 28.34731, 28.98117, 28.55296, 28.21102, 27.67735, 27.95042,…
$ AGCAMT <dbl> 704485, 2533456, 295044, 238956, 5695063, 7807044, 6250229,…
$ BANKCAMT <dbl> 98032, 98032, 98032, 0, 98032, 98032, 6352080, 98032, 98032…
$ BUDGCAMT <dbl> 9323778, 55210, 0, 6449556, 0, 6449556, 52836, 6449556, 644…
$ COMMCAMT <dbl> 0, 0, 0, 0, 0, 0, 9691197, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ CONCAMT <dbl> 0, 0, 0, 0, 0, 0, 104620, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ EDUCAMT <dbl> 1217123, 0, 1331276, 1217123, 1273393, 1217123, 1614288, 47…
$ ENGYCAMT <dbl> 56577, 21253596, 56577, 0, 56577, 7955164, 59044601, 56577,…
$ ENVCAMT <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 26000, 0, 0, 0, 0, 0, 0, 79898, 0, …
$ FORCAMT <dbl> 21669449, 0, 21669449, 729985, 0, 860192, 860192, 1590177, …
$ GOVCAMT <dbl> 849317, 849317, 849317, 0, 849317, 849317, 3702913, 930627,…
$ HEALTCAMT <dbl> 4920055, 3285714, 3360434, 88581, 1651450, 2892165, 9648776…
$ HUMCAMT <dbl> 226433, 226433, 226433, 0, 226433, 615668, 5471538, 226433,…
$ INDCAMT <dbl> 258924, 0, 258924, 258924, 0, 0, 258924, 258924, 1738638, 2…
$ MULTCAMT <dbl> 16031258, 5910390, 15116745, 7495790, 2716080, 5090534, 224…
$ SOCCAMT <dbl> 1498129, 817820, 1124252, 0, 1920034, 2412571, 34259058, 29…
$ TOURCAMT <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ TRANCAMT <dbl> 18761, 18761, 2528014, 0, 16257397, 9544263, 40248170, 0, 0…
$ WATCAMT <dbl> 5449613, 0, 1034734, 812993, 63379645, 1034734, 104702400, …
$ TOTCAMT <dbl> 62321934, 35048729, 47949231, 17291908, 94123421, 46826363,…
$ AGDAMT <dbl> 629281, 789869, 262560, 195383, 1978569, 4166167, 2456422, …
$ BANKDAMT <dbl> 73155, 73155, 73155, 0, 73155, 73155, 4415928, 73155, 73155…
$ BUDGDAMT <dbl> 4395199, 35584, 0, 3014861, 0, 3014861, 58072, 3014861, 301…
$ COMMDAMT <dbl> 0, 0, 0, 0, 0, 0, 831804, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ CONDAMT <dbl> 0, 0, 0, 0, 0, 0, 97750, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ EDUDAMT <dbl> 760055, 0, 801375, 760055, 817527, 760055, 1203194, 2435956…
$ ENGYDAMT <dbl> 60340, 6431800, 60340, 19220, 60340, 458388, 32773472, 6034…
$ ENVDAMT <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 9518, 0, 0, 0, 0, 0, 0, 70015, 0, 0…
$ FORDAMT <dbl> 10818205, 0, 10818205, 644705, 0, 646439, 646439, 1291144, …
$ GOVDAMT <dbl> 308327, 308327, 308327, 0, 308327, 308327, 2945694, 373817,…
$ HEALTDAMT <dbl> 3584409, 2253911, 2279653, 26574, 1338957, 1799237, 8137874…
$ HUMDAMT <dbl> 172736, 172736, 172736, 0, 172736, 424586, 6663563, 172736,…
$ INDDAMT <dbl> 369324, 0, 369324, 369324, 0, 0, 369324, 369324, 497277, 36…
$ MULTDAMT <dbl> 8050858, 3712798, 11784759, 4741985, 2421424, 4346258, 2406…
$ SOCDAMT <dbl> 1308661, 633763, 964089, 0, 1652237, 1932228, 24252732, 251…
$ TOURDAMT <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ TRANDAMT <dbl> 15421, 15421, 2688597, 0, 16431544, 7429680, 19526280, 0, 0…
$ WATDAMT <dbl> 1996315, 0, 854545, 667767, 23818495, 854545, 38039544, 362…
$ TOTDAMT <dbl> 32542286, 14427364, 31437665, 10439874, 49073311, 26213926,…
$ geometry <POLYGON [°]> POLYGON ((83.10834 28.6202,..., POLYGON ((83.99726 …
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)
)