class: center, middle, inverse, title-slide .title[ # Geospatial visualization: raster maps ] .author[ ### MACS 24000
University of Chicago ] --- # Geospatial visualizations * Earliest form of information visualizations * Geospatial data visualizations * [Google Maps](https://www.google.com/maps) --- # Dr. John Snow .center[ [![:scale 70%](https://upload.wikimedia.org/wikipedia/commons/2/27/Snow-cholera-map-1.jpg)](https://commons.wikimedia.org/wiki/File:Snow-cholera-map-1.jpg) ] --- # Minard's map of Napoleon's march on Russia [![Charles Minard's 1869 chart showing the number of men in Napoleon’s 1812 Russian campaign army, their movements, as well as the temperature they encountered on the return path. Source: Wikipedia.](https://upload.wikimedia.org/wikipedia/commons/2/29/Minard.png)](https://en.wikipedia.org/wiki/File:Minard.png) --- # Minard's map of Napoleon's march on Russia [![English translation of Minard's map](https://upload.wikimedia.org/wikipedia/commons/thumb/e/e2/Minard_Update.png/1280px-Minard_Update.png)](https://commons.wikimedia.org/wiki/File:Minard_Update.png) --- # Designing modern maps * Depict spatial features * Incorporate additional attributes and information * Major features * Scale * Projection * Symbols --- # Scale * Proportion between distances and sizes on a map and their actual distances and sizes on Earth * Small-scale map * Large-scale map --- # Large-scale map <img src="index_files/figure-html/large-scale-1.png" width="864" /> --- # Small-scale map <img src="index_files/figure-html/small-scale-1.png" width="864" /> --- # Projection * Process of taking a three-dimensional object and visualizing it on a two-dimensional surface * No 100% perfect method for this * Always introduces distortions * Properties of projection methods 1. Shape 1. Area 1. Angles 1. Distance 1. Direction --- # Conformal projections <img src="index_files/figure-html/mercator-1.png" width="864" /> --- # Equal-area projections <img src="index_files/figure-html/equal-area-1.png" width="864" /> --- # Mollweide <img src="index_files/figure-html/mollweide-1.png" width="864" /> --- # Symbols <img src="index_files/figure-html/bb-hydepark-stamen-1.png" width="864" /> --- # `ggmap` * Package for drawing maps using `ggplot2` and **raster** map tiles * Static image files generated by mapping services * Focus on incorporating data into existing maps * Severely limits ability to change the appearance of the geographic map --- # Obtain map images * [OpenStreetMap](https://www.openstreetmap.org/) * [Stamen Maps](http://maps.stamen.com/#terrain/12/37.7706/-122.3782) * [Google Maps](https://maps.google.com) -- ## Method for specifying map regions 1. Bounding box 1. Center/zoom --- # Bounding box ```r # store bounding box coordinates chi_bb <- c( left = -87.936287, bottom = 41.679835, right = -87.447052, top = 42.000835 ) chicago_stamen <- get_stamenmap( bbox = chi_bb, zoom = 11 ) chicago_stamen ``` ``` ## 627x712 terrain map image from Stamen Maps. ## See ?ggmap to plot it. ``` --- # Bounding box ```r ggmap(chicago_stamen) ``` <img src="index_files/figure-html/bb-chicago-stamen-plot-1.png" width="864" /> --- # Level of detail <img src="index_files/figure-html/bb-chicago-stamen-zoom-1.png" width="864" /> --- # Identifying bounding box > Use [bboxfinder.com](http://bboxfinder.com/#0.000000,0.000000,0.000000,0.000000) to determine the exact longitude/latitude coordinates for the bounding box you wish to obtain. --- # Types of map tiles <img src="index_files/figure-html/stamen-maptype-1.png" width="864" /> --- # Import crime data * City of Chicago open data portal * [Crime data from 2017](https://data.cityofchicago.org/Public-Safety/Crimes-2017/d62x-nvdr) ``` ## Rows: 267,345 ## Columns: 22 ## $ ID <dbl> 11094370, 11118031, 11134189, 11156462, 1116487… ## $ `Case Number` <chr> "JA440032", "JA470589", "JA491697", "JA521389",… ## $ Date <chr> "09/21/2017 12:15:00 AM", "10/12/2017 07:14:00 … ## $ Block <chr> "072XX N CALIFORNIA AVE", "055XX W GRAND AVE", … ## $ IUCR <chr> "1122", "1345", "4651", "1110", "0265", "143A",… ## $ `Primary Type` <chr> "DECEPTIVE PRACTICE", "CRIMINAL DAMAGE", "OTHER… ## $ Description <chr> "COUNTERFEIT CHECK", "TO CITY OF CHICAGO PROPER… ## $ `Location Description` <chr> "CURRENCY EXCHANGE", "JAIL / LOCK-UP FACILITY",… ## $ Arrest <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,… ## $ Domestic <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE… ## $ Beat <chr> "2411", "2515", "0922", "2514", "1221", "0232",… ## $ District <chr> "024", "025", "009", "025", "012", "002", "005"… ## $ Ward <dbl> 50, 29, 12, 30, 32, 20, 9, 12, 12, 27, 32, 17, … ## $ `Community Area` <dbl> 2, 19, 58, 19, 24, 40, 49, 30, 30, 23, 24, 44, … ## $ `FBI Code` <chr> "10", "14", "26", "11", "02", "15", "03", "06",… ## $ `X Coordinate` <dbl> 1156443, 1138788, 1159425, 1138653, 1161264, 11… ## $ `Y Coordinate` <dbl> 1947707, 1913480, 1875711, 1920720, 1905292, 18… ## $ Year <dbl> 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,… ## $ `Updated On` <chr> "03/01/2018 03:52:35 PM", "03/01/2018 03:52:35 … ## $ Latitude <dbl> 42.01229, 41.91871, 41.81467, 41.93858, 41.8958… ## $ Longitude <dbl> -87.69971, -87.76551, -87.69073, -87.76583, -87… ## $ Location <chr> "(42.012293397, -87.699714109)", "(41.918711651… ``` --- # Plot high-level map of crime .pull-left[ ```r chicago <- chicago_stamen ggmap(chicago) ``` ] .pull-right[ <img src="index_files/figure-html/import-chicago-out-1.png" width="864" /> ] --- # Using `geom_point()` .pull-left[ ```r ggmap(chicago) + geom_point( data = crimes, mapping = aes( x = Longitude, y = Latitude ) ) ``` ] .pull-right[ <img src="index_files/figure-html/plot-crime-point-out-1.png" width="864" /> ] --- # Using `geom_point()` .pull-left[ ```r ggmap(chicago) + geom_point( data = crimes, aes( x = Longitude, y = Latitude ), size = .25, alpha = .01 ) ``` ] .pull-right[ <img src="index_files/figure-html/plot-crime-point-alpha-out-1.png" width="864" /> ] --- # Using `stat_density_2d()` .pull-left[ ```r ggmap(chicago) + geom_density_2d( data = crimes, aes( x = Longitude, y = Latitude ) ) ``` ] .pull-right[ <img src="index_files/figure-html/kde-contour-out-1.png" width="864" /> ] --- # Using `stat_density_2d()` .pull-left[ ```r ggmap(chicago) + stat_density_2d( data = crimes, aes( x = Longitude, y = Latitude, fill = stat(level) ), geom = "polygon" ) ``` ] .pull-right[ <img src="index_files/figure-html/kde-fill-out-1.png" width="864" /> ] --- # Using `stat_density_2d()` <img src="index_files/figure-html/plot-crime-density-1.png" width="864" /> --- # Looking for variation <img src="index_files/figure-html/plot-crime-wday-1.png" width="864" /> --- # Locations of murders ``` ## # A tibble: 671 × 22 ## ID `Case Number` Date Block IUCR `Primary Type` Description ## <dbl> <chr> <chr> <chr> <chr> <chr> <chr> ## 1 23128 JA149608 02/11/2017 07:… 001X… 0110 HOMICIDE FIRST DEGR… ## 2 23851 JA530946 11/30/2017 11:… 088X… 0110 HOMICIDE FIRST DEGR… ## 3 23355 JA302423 06/11/2017 06:… 047X… 0110 HOMICIDE FIRST DEGR… ## 4 23379 JA312425 06/18/2017 04:… 006X… 0110 HOMICIDE FIRST DEGR… ## 5 23673 JA490016 10/28/2017 10:… 048X… 0110 HOMICIDE FIRST DEGR… ## 6 23224 JA210752 04/03/2017 12:… 013X… 0110 HOMICIDE FIRST DEGR… ## 7 23627 JA461918 10/07/2017 11:… 018X… 0110 HOMICIDE FIRST DEGR… ## 8 23628 JA461918 10/07/2017 11:… 018X… 0110 HOMICIDE FIRST DEGR… ## 9 10836558 JA138326 02/01/2017 06:… 013X… 0142 HOMICIDE RECKLESS H… ## 10 23477 JA364517 07/26/2017 05:… 047X… 0110 HOMICIDE FIRST DEGR… ## # … with 661 more rows, and 15 more variables: `Location Description` <chr>, ## # Arrest <lgl>, Domestic <lgl>, Beat <chr>, District <chr>, Ward <dbl>, ## # `Community Area` <dbl>, `FBI Code` <chr>, `X Coordinate` <dbl>, ## # `Y Coordinate` <dbl>, Year <dbl>, `Updated On` <chr>, Latitude <dbl>, ## # Longitude <dbl>, Location <chr> ``` --- # Locations of murders <img src="index_files/figure-html/homicide-city-1.png" width="864" /> --- # Locations of murders <img src="index_files/figure-html/get-high-low-murder-maps-1.png" width="864" /> --- # Locations of murders <img src="index_files/figure-html/plot-murder-1.png" width="864" /> --- # Additional aesthetics <img src="index_files/figure-html/plot-violent-1.png" width="864" /> --- # Exercise using `ggmap` ![](https://pixel.nymag.com/imgs/daily/strategist/2018/09/05/schwinn-recumbent-exercise-bike.w710.h473.2x.jpg)