EDS 223: Spatial Analysis - Assignment 1
spData
world dataworld_asia <- world[world[["continent"]] == "Asia", ]
plot(world_asia, max.plot = 10) #multi-plot of all attributes
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):
plot(world_asia["gdpPercap"], nbreaks = 10)
#nbreaks specifies the number of breaks
#breaks uses a vector to specify break values or the break style
plot(world_asia["gdpPercap"], breaks = "jenks")
plot(world_asia["gdpPercap"], graticule = TRUE, key.pos = NULL, axes = TRUE)
class(world_asia$gdpPercap) #numeric class = continuous numeric variable
[1] "numeric"
#create a histogram, which shows a long right tail and strong skew
graphics::hist(world_asia$gdpPercap,
breaks = 10)
#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
#tmap_save(filename = "asiamap_order.jpg", width = 8, height = 5, units = "in", dpi = 300)
Full code for this assignment can be found here.
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} }