class: center, middle, inverse, title-slide # Data Visualization ## Making outline maps ### Andrew Irwin,
a.irwin@dal.ca
### Math & Stats, Dalhousie University ### 2021-03-15 (updated: 2021-03-06) --- class: middle # Plan * Kinds of maps * Basic maps * Adding points * Shading areas * Projections --- class: middle ### Map libraries * `ggmap` (part of tidyverse) * Many other libraries * `maps` * `mapproj` for making projections of the Earth's surface * `mapdata` with maps of China and Japan * `leaflet` for drawing raster or tiled maps (next lesson) --- class: middle ### Kinds of maps * Coastlines and political boundaries * Natural features (rivers, water bodies) * Points and filled regions on maps * How to represent latitude and longitude from a sphere on a screen? * Tiled maps --- ### A basic map ```r library(tidyverse) WorldData <- map_data('world') m1 <- ggplot(WorldData, aes(map_id=region)) + geom_map(map = WorldData, aes(x = long, y = lat), fill = "lightgray", colour = "#7f7f7f", alpha = 0.5, size=0.5) + theme_bw() ``` --- ### A basic map <img src="27-making-maps_files/figure-html/unnamed-chunk-2-1.png" width="90%" style="display: block; margin: auto;" /> --- ### Colour countries ```r values <- tibble( region = WorldData %>% pull(region) %>% unique()) values <- values %>% mutate(value = runif(nrow(values))) m2 <- ggplot(values, aes(map_id = region)) + geom_map(aes(fill = value), map = WorldData, colour = "lightgray", alpha = 1, size=0.5) + expand_limits(x = c(-180,180), y = c(-80,80)) + theme_bw() ``` --- ### Colour countries <img src="27-making-maps_files/figure-html/unnamed-chunk-4-1.png" width="90%" style="display: block; margin: auto;" /> --- ### Show only some countries ```r values <- tibble( region = c("Canada", "China", "Chile"), value = c(1, 2, 3)) m3 <- ggplot(values, aes(map_id = region)) + geom_map(aes(fill = value), map = WorldData, colour = "lightgray", alpha = 1, size=0.5) + expand_limits(x = c(-180,180), y = c(-80,80))+ theme_bw() ``` --- ### Show some countries <img src="27-making-maps_files/figure-html/unnamed-chunk-6-1.png" width="90%" style="display: block; margin: auto;" /> --- ### Colour some countries ```r values <- tibble( region = c("Canada", "China", "Chile"), value = c(1, 2, 3)) m4 <- ggplot(WorldData, aes(map_id = region)) + geom_map(map = WorldData, colour = "lightgray", fill = "darkgray", alpha = 1, size=0.5) + geom_map(aes(fill = value), data = values, map = WorldData) + expand_limits(x = c(-180,180), y = c(-80,80))+ theme_bw() ``` --- ### Colour some countries <img src="27-making-maps_files/figure-html/unnamed-chunk-8-1.png" width="90%" style="display: block; margin: auto;" /> --- ### Add points ```r library(rnaturalearth) library(rnaturalearthdata) library(ggrepel) canada <- ne_countries(country = "canada", scale = "large", returnclass = "sf") sites <- tibble(longitude = c(-81, -80), latitude = c(44, 45), label = c("A", "B")) m5 <- ggplot(data = canada) + geom_sf() + geom_point(data = sites, aes(x = longitude, y = latitude), size = 4, shape = 21, fill = "darkred") + geom_label_repel(data = sites, aes(x = longitude, y = latitude, label = label)) + coord_sf(xlim = c(-140,-50), ylim = c(42,65)) ``` --- ### Add points <img src="27-making-maps_files/figure-html/unnamed-chunk-10-1.png" width="90%" style="display: block; margin: auto;" /> --- ### Projections ```r library(mapproj) CanadaData <- map_data('world', 'Canada') m6 <- ggplot(CanadaData, aes(map_id=region)) + geom_map(map = CanadaData, aes(x = long, y = lat), fill = "lightgray", colour = "#7f7f7f", alpha = 0.5, size=0.5) + theme_bw() + coord_map("lambert", lat0 = 42, lat1 = 50) ``` --- ### Projections <img src="27-making-maps_files/figure-html/unnamed-chunk-12-1.png" width="90%" style="display: block; margin: auto;" /> --- ### Polar regions ```r library(mapproj) m7 <- ggplot(WorldData, aes(map_id=region)) + geom_map(map = WorldData, aes(x = long, y = lat), fill = "lightgray", colour = "#7f7f7f", alpha = 0.5, size=0.5) + theme_bw() + coord_map("perspective", 2, orientation=c(-90, -90, 90)) ``` --- ### Polar regions <img src="27-making-maps_files/figure-html/unnamed-chunk-14-1.png" width="90%" style="display: block; margin: auto;" /> --- class: middle ### Summary * Make a basic map * Select all or some countries * Fill regions with colour * Add points and labels * Use a different projection --- class: middle # Further reading * Course notes --- class: middle, inverse ## Task * Bonus task - make some simple maps based on course notes