class: center, middle, inverse, title-slide .title[ # 36-613: Data Visualization ] .subtitle[ ## Visualizing Spatial Data and Maps ] .author[ ### Professor Ron Yurko ] .date[ ### 10/3/2022 ] --- # How should we think about spatial data? - Typically location is measured with __latitude__ / __longitude__ (2D) - __Latitude__: Measures North / South (the "y-axis") - Range is `\((-90^{\circ}, 90^{\circ})\)` - Measures degrees from the equator `\((0^{\circ})\)` - `\((-90^{\circ}, 0^{\circ})\)` = southern hemisphere - `\((0^{\circ}, 90^{\circ})\)` = northern hemisphere -- - __Longitude__: Measures East/West (the "x-axis") - Range is `\((-180^{\circ}, 180^{\circ})\)` - Measures degrees from the prime meridian `\((0^{\circ})\)` in Greenwich, England - `\((-180^{\circ}, 0^{\circ})\)` = eastern hemisphere - `\((0^{\circ}, 180^{\circ})\)` = western hemisphere --- # Latitude and Longitude <img src="https://c.tadst.com/gfx/1200x630/longitude-and-latitude-simple.png?1" width="80%" style="display: block; margin: auto;" /> --- # Map Projections - Earth is a 3D object, but maps are 2D objects - __Map projections__: Transformation of the lat / long coordinates on a sphere (the earth) to a 2D plane - There are many different projections - each will distort the map in different ways. - The most common projections are: - [Mercator](https://en.wikipedia.org/wiki/Mercator_projection) - [Robinson](https://en.wikipedia.org/wiki/Robinson_projection) - [Conic](http://www.geo.hunter.cuny.edu/~jochen/gtech201/lectures/lec6concepts/Map%20coordinate%20systems/Conic%20projections.htm#:~:text=Conic%20projections%20are%20created%20by,a%20developable%20map%20projection%20surface.) - [Cylindrical](https://en.wikipedia.org/wiki/Map_projection#Cylindrical) - [Planar](http://www.geo.hunter.cuny.edu/~jochen/gtech201/lectures/lec6concepts/Map%20coordinate%20systems/Planar%20projections.htm) - [Interrupted projections](https://en.wikipedia.org/wiki/Interruption_(map_projection)) --- # Mercator Projection (1500s) <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Mercator_projection_Square.JPG/700px-Mercator_projection_Square.JPG" width="60%" style="display: block; margin: auto;" /> --- # Mercator Projection (Tissot indicatrix) <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Mercator_with_Tissot%27s_Indicatrices_of_Distortion.svg/700px-Mercator_with_Tissot%27s_Indicatrices_of_Distortion.svg.png" width="60%" style="display: block; margin: auto;" /> --- # Robinson Projection (Standard from 1963-1998) <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/9/96/Robinson_projection_SW.jpg/700px-Robinson_projection_SW.jpg" width="70%" style="display: block; margin: auto;" /> --- # Robinson Projection (Tissot indicatrix) <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Robinson_with_Tissot%27s_Indicatrices_of_Distortion.svg/700px-Robinson_with_Tissot%27s_Indicatrices_of_Distortion.svg.png" width="70%" style="display: block; margin: auto;" /> --- # Winkel Tripel Projection (proposed 1921, now the standard) <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/9/91/Winkel_triple_projection_SW.jpg/660px-Winkel_triple_projection_SW.jpg" width="60%" style="display: block; margin: auto;" /> --- # Winkel Tripel Projection (Tissot indicatrix) <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/36/Winkel_Tripel_with_Tissot%27s_Indicatrices_of_Distortion.svg/660px-Winkel_Tripel_with_Tissot%27s_Indicatrices_of_Distortion.svg.png" width="60%" style="display: block; margin: auto;" /> --- # And many more... (see [xkcd comic](https://xkcd.com/977/)) <img src="https://i.pinimg.com/originals/2d/03/cf/2d03cffa216afb23fa50fb07fc1221b1.jpg" width="60%" style="display: block; margin: auto;" /> --- ## Visualizing spatial data on maps using [`ggmap`](https://cran.r-project.org/web/packages/ggmap/readme/README.html) .pull-left[ ```r library(ggmap) # First, we'll draw a "box" around the US # (in terms of latitude and longitude) US <- c(left = -125, bottom = 10, right = -67, top = 49) map <- get_stamenmap(US, zoom = 5, maptype = "toner-lite") # Visualize the basic map *ggmap(map) ``` - Draw map based on lat / lon coordinates - Put the box into `get_stamenmap()` to access [Stamen Maps](http://maps.stamen.com/#terrain/12/37.7706/-122.3782) - Draw the map using `ggmap()` to serve as base ] .pull-right[ <img src="figs/Lec10/unnamed-chunk-10-1.png" width="100%" /> ] --- # Three main types of spatial data 1. __Point Pattern Data__: lat-long coordinates where events have occurred 2. __Point-Referenced data__: Latitude-longitude (lat-long) coordinates as well as one or more variables specific to those coordinates. 3. __Areal Data__: Geographic regions with one or more variables associated with those regions. -- - Each type is structured differently within a dataset - Each type requires a different kind of graph(s) --- # Point-Pattern data - __Point Pattern Data__: lat-long coordinates where events have occurred - __Point pattern data simply records the lat-long of events__; thus, there are only two columns - Again, latitude and longitude are represented with dots, sometimes called a dot or bubble map. -- - The goal is to understand how the __density__ of events varies across space - The density of the dots can also be visualized (e.g., with contours) - __Use methods we've discussed before for visualizing 2D joint distribution__ --- # Point-Referenced data - __Point-Referenced data__: Latitude-longitude (lat-long) coordinates as well as one or more variables specific to those coordinates - Point-referenced data will have the following form: ```r airports %>% dplyr::select(lat, lon, altitude, n_depart, n_arrive, name) %>% slice(1:3) ``` ``` ## # A tibble: 3 × 6 ## lat lon altitude n_depart n_arrive name ## <dbl> <dbl> <dbl> <int> <int> <chr> ## 1 -6.08 145. 5282 5 5 Goroka Airport ## 2 -5.21 146. 20 8 8 Madang Airport ## 3 -5.83 144. 5388 10 12 Mount Hagen Kagamuga Airport ``` -- - The goal is to understand how the variable(s) (e.g., `altitude`) vary across different spatial locations - Typically, the latitude and longitude are represented with dots, and the variable(s) are represented with size and/or colors --- ## Adding points to the map as usual .pull-left[ ```r ggmap(map) + * geom_point(data = airports, aes(x = lon, y = lat), alpha = 0.25) ``` - Display locations of airports using `geom_point()` layer, need to specify `data` for layer - Currently viewing __point-pattern__ data ] .pull-right[ <img src="figs/Lec10/unnamed-chunk-12-1.png" width="100%" /> ] --- ## Altering points on the map (in the usual way) .pull-left[ ```r ggmap(map) + geom_point(data = airports, aes(x = lon, y = lat, * size = sqrt(n_depart), * color = sqrt(n_arrive)), alpha = .5) + scale_size_area(breaks = sqrt(c(1, 5, 10, 50, 100, 500)), labels = c(1, 5, 10, 50, 100, 500), name = "# departures") + scale_color_distiller(palette = "Spectral") + labs(color = "sqrt(# arrivals)") + theme(legend.position = "bottom") ``` - Displaying additional variables through `aes` ] .pull-right[ <img src="figs/Lec10/unnamed-chunk-13-1.png" width="100%" /> ] --- ## Thinking about areal data - __Areal Data__: Geographic regions associated with one or more variables specific to those regions - Areal data will have the following form (example US states data from 1970s): ```r state_data %>% dplyr::slice(1:3) ``` ``` ## # A tibble: 3 × 9 ## Population Income Illiteracy `Life Exp` Murder `HS Grad` Frost Area state ## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> ## 1 3615 3624 2.1 69.0 15.1 41.3 20 50708 alabama ## 2 365 6315 1.5 69.3 11.3 66.7 152 566432 alaska ## 3 2212 4530 1.8 70.6 7.8 58.1 15 113417 arizona ``` -- - Need to match the region with the actual geographic boundaries - Many geographic boundaries/features are stored as "shapefiles" - i.e., complicated polygons - Can contain the lines, points, etc. to represent any geographic feature - Shapefiles are readily available for countries, states, counties, etc. --- ## Access shapefiles using `map_data()` .pull-left[ ```r library(maps) state_borders <- map_data("state") head(state_borders) ``` ``` ## long lat group order region subregion ## 1 -87.46201 30.38968 1 1 alabama <NA> ## 2 -87.48493 30.37249 1 2 alabama <NA> ## 3 -87.52503 30.37249 1 3 alabama <NA> ## 4 -87.53076 30.33239 1 4 alabama <NA> ## 5 -87.57087 30.32665 1 5 alabama <NA> ## 6 -87.58806 30.32665 1 6 alabama <NA> ``` - For example: `map_data("world")`, `map_data("state")`, `map_data("county")` (need to install [`maps` package](https://cran.r-project.org/web/packages/maps/maps.pdf)) - Contains lat/lon coordinates to draw geographic boundaries ] -- .pull-right[ - Typical workflow for plotting areal data (e.g., using states): - Get state-specific data - Get state boundaries - Merge state-specific data with state boundaries (using `left_join()`) ```r state_plot_data <- state_borders %>% * left_join(state_data, * by = c("region" = "state")) ``` - Then plot the data ] --- ## Create a choropleth map with `geom_polygon()` .pull-left[ ```r state_plot_data %>% ggplot() + * geom_polygon(aes(x = long, y = lat, * group = group, * fill = Illiteracy), * color = "black") + scale_fill_gradient2( low = "darkgreen", mid = "lightgrey", high = "darkorchid4", midpoint = 0.95) + theme_void() + * coord_map("polyconic") + labs(fill = "Illiteracy %") + theme(legend.position = "bottom") ``` ] .pull-right[ <img src="figs/Lec10/unnamed-chunk-18-1.png" width="100%" /> ] --- ## Uniform size with [`statebins`](https://github.com/hrbrmstr/statebins) .pull-left[ ```r library(statebins) state_data$new_state <- str_to_title(state_data$state) *statebins(state_data = state_data, state_col = "new_state", value_col = "Illiteracy") + theme_statebins() ``` - Make all states equal in size - Keeps spatial arrangement but focus is on the variable of interest displayed by the color ] .pull-right[ <img src="figs/Lec10/unnamed-chunk-19-1.png" width="100%" /> ] --- class: center, middle # Next time: Visualizing text data __HW5 due Wednesday! Graphics Critique / Replication #2 due Friday Oct 7th!__ Recommended reading: [CW Chapter 15 Visualizing geospatial data](https://clauswilke.com/dataviz/geospatial-data.html) [KH Chapter 7 Draw Maps](https://socviz.co/maps.html#maps)