Exploring basic cartography using sf and tmap

R Spatial Analysis

EDS 223: Spatial Analysis - Assignment 1

Mia Forsline
2021-10-07

Learning Goals:

Load necessary packages

show
knitr::opts_chunk$set(echo = TRUE, 
                      message = FALSE,
                      warning = FALSE,
                      include = TRUE)

library(RColorBrewer)
library(sf)
library(spData)
library(spDataLarge)
library(tidyverse)
library(tmap)
library(dplyr)

#display.brewer.all(colorblindFriendly = T) to view color-blind friendly palettes 

Load, subset, and explore Asia data from spData world data

show
world_asia <- world[world[["continent"]] == "Asia", ]

plot(world_asia, max.plot = 10) #multi-plot of all attributes
show
plot(world_asia["gdpPercap"], #plot a specific attribute 
     key.pos = 4) #set the color key position (1=below, 2=left, 3=above and 4=right):
show
plot(world_asia["gdpPercap"], 
     key.pos = 1, 
     axes = TRUE, #add latitude/longitude tick marks 
     key.width = lcm(1.3), 
     key.length = 1.0)

Specify class intervals

show
plot(world_asia["gdpPercap"], nbreaks = 10)
show
#nbreaks specifies the number of breaks
#breaks uses a vector to specify break values or the break style 
plot(world_asia["gdpPercap"], breaks = "jenks")

Add a graticule if necessary

show
plot(world_asia["gdpPercap"], graticule = TRUE, key.pos = NULL, axes = TRUE)

Histogram of gdpPercap to choose breaks style

show
class(world_asia$gdpPercap) #numeric class = continuous numeric variable 
[1] "numeric"
show
#create a histogram, which shows a long right tail and strong skew 
graphics::hist(world_asia$gdpPercap, 
     breaks = 10)

Plot finished Asia map

show
#create new bounding box 
bbox_new <- st_bbox(c(xmin = 26.04335, xmax = 145.5431,
                      ymin = -15, ymax = 55.38525 )) # current bounding box
xrange <- bbox_new$xmax - bbox_new$xmin # range of x values
yrange <- bbox_new$ymax - bbox_new$ymin # range of y values
bbox_new <- bbox_new %>%  
  st_as_sfc()

#plot 
asia_map <- tm_shape(world, bbox = bbox_new)+
  tm_fill(col = "white") + 
  tm_borders(lwd = 0.1) + 
tm_shape(world_asia) +
  tm_borders(lwd = 0.5) +
  tm_fill(col = "gdpPercap", 
          palette = "YlOrRd", 
          style = "order",
          title = "GDP Per Capita",
          textNA = "Missing Data",
          colorNA = "gray") +
  tm_compass(type = "4star", 
             position = c(0.25,0.02), 
             size = 4, 
             show.labels = 2) + #show all 4 directions on the compass, not just N 
  #compass types: arrow, 4star, 8star, radar, rose
  tm_scale_bar(breaks = c(0, 500, 1000, 1500, 2000), 
               text.size = 1, 
               position = c("center", "bottom")) + 
  tm_text(text = "name_long", 
          size = 0.7, 
          col = "black") + 
  tm_layout(scale = 0.6, #zoom in and out 
            frame.lwd = 5,
            legend.text.size = 1,
            legend.title.size = 1.5, 
            legend.position = c("left", "bottom"),
            legend.bg.color = "white",
            legend.frame = "black", 
            title.size = 2,
            title.position = c("center", "top"), 
            main.title = "2014 GDP Per Capita in Asia",
            main.title.position = "center") +
  tmap_options(bg.color = "lightblue1") + 
  tm_credits(paste("2014 GDP per capita for 43 countries in Asia. The mean per-capita GDP \n was $20,026 (SD = 24,361; SE = 3,715). Original data is plotted in \n WGS84 and sourced from 'spData' (https://nowosad.github.io/spData/)."), 
             size = 0.8,
             position=c("RIGHT", "BOTTOM"),
             bg.color = "white", 
             bg.alpha = 0.5)
asia_map
This map displays 2014 GDP per capita for 43 countries in Asia. 4 countries were omitted due to mising data: the Democratic People's Republic of Korea, Northern Cyprus, Syria, and Taiwan. The maximum per-capita GDP is $120,860 in Qatar, and the minimum per-capita GDP is $1,839 in Afghanistan. The mean per-capita GDP is $20,026, and the median per-capita GDP is $10,650 (SD = 24,361; SE = 3,715). Only 4 countries measured per-capita GDP above $50,000. Per-capita GDP is rounded to the nearest whole number. Original world country polygon data is plotted in WGS84 and sourced from [spData](<https://nowosad.github.io/spData/>).

Figure 1: This map displays 2014 GDP per capita for 43 countries in Asia. 4 countries were omitted due to mising data: the Democratic People’s Republic of Korea, Northern Cyprus, Syria, and Taiwan. The maximum per-capita GDP is $120,860 in Qatar, and the minimum per-capita GDP is $1,839 in Afghanistan. The mean per-capita GDP is $20,026, and the median per-capita GDP is $10,650 (SD = 24,361; SE = 3,715). Only 4 countries measured per-capita GDP above $50,000. Per-capita GDP is rounded to the nearest whole number. Original world country polygon data is plotted in WGS84 and sourced from spData.

show
#tmap_save(filename = "asiamap_order.jpg", width = 8, height = 5, units = "in", dpi = 300)

GitHub

Full code for this assignment can be found here.

Citation

For attribution, please cite this work as

Forsline (2021, Oct. 7). Mia Forsline: Exploring basic cartography using `sf` and `tmap`. Retrieved from miaforsline.github.io/

BibTeX citation

@misc{forsline_cartography,
  author = {Forsline, Mia},
  title = {Mia Forsline: Exploring basic cartography using `sf` and `tmap`},
  url = {miaforsline.github.io/},
  year = {2021}
}