class: center, middle, inverse, title-slide # Introduction to Spatial Data and Leaflet Mapping with R ## Summer Institute in Computational Social Science, Covenant University, Nigeria 2022 ### Dr. Laurie Baker ### College of the Atlantic ### 2020/04/09 (updated: 2022-06-23) --- # About me .pull-left[ .center[ <img style="border-radius: 50%;" src="images/laurie_baker.jpg" width="300px"/> .font150[**Dr. Laurie Baker**] College of the Atlantic, Bar Harbor, ME ]] .pull-right[ ### Education <svg viewBox="0 0 640 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M622.34 153.2L343.4 67.5c-15.2-4.67-31.6-4.67-46.79 0L17.66 153.2c-23.54 7.23-23.54 38.36 0 45.59l48.63 14.94c-10.67 13.19-17.23 29.28-17.88 46.9C38.78 266.15 32 276.11 32 288c0 10.78 5.68 19.85 13.86 25.65L20.33 428.53C18.11 438.52 25.71 448 35.94 448h56.11c10.24 0 17.84-9.48 15.62-19.47L82.14 313.65C90.32 307.85 96 298.78 96 288c0-11.57-6.47-21.25-15.66-26.87.76-15.02 8.44-28.3 20.69-36.72L296.6 284.5c9.06 2.78 26.44 6.25 46.79 0l278.95-85.7c23.55-7.24 23.55-38.36 0-45.6zM352.79 315.09c-28.53 8.76-52.84 3.92-65.59 0l-145.02-44.55L128 384c0 35.35 85.96 64 192 64s192-28.65 192-64l-14.18-113.47-145.03 44.56z"></path></svg> BSc and MSc, Marine Biology <svg viewBox="0 0 640 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M622.34 153.2L343.4 67.5c-15.2-4.67-31.6-4.67-46.79 0L17.66 153.2c-23.54 7.23-23.54 38.36 0 45.59l48.63 14.94c-10.67 13.19-17.23 29.28-17.88 46.9C38.78 266.15 32 276.11 32 288c0 10.78 5.68 19.85 13.86 25.65L20.33 428.53C18.11 438.52 25.71 448 35.94 448h56.11c10.24 0 17.84-9.48 15.62-19.47L82.14 313.65C90.32 307.85 96 298.78 96 288c0-11.57-6.47-21.25-15.66-26.87.76-15.02 8.44-28.3 20.69-36.72L296.6 284.5c9.06 2.78 26.44 6.25 46.79 0l278.95-85.7c23.55-7.24 23.55-38.36 0-45.6zM352.79 315.09c-28.53 8.76-52.84 3.92-65.59 0l-145.02-44.55L128 384c0 35.35 85.96 64 192 64s192-28.65 192-64l-14.18-113.47-145.03 44.56z"></path></svg> PhD, Epidemiology <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M458.622 255.92l45.985-45.005c13.708-12.977 7.316-36.039-10.664-40.339l-62.65-15.99 17.661-62.015c4.991-17.838-11.829-34.663-29.661-29.671l-61.994 17.667-15.984-62.671C337.085.197 313.765-6.276 300.99 7.228L256 53.57 211.011 7.229c-12.63-13.351-36.047-7.234-40.325 10.668l-15.984 62.671-61.995-17.667C74.87 57.907 58.056 74.738 63.046 92.572l17.661 62.015-62.65 15.99C.069 174.878-6.31 197.944 7.392 210.915l45.985 45.005-45.985 45.004c-13.708 12.977-7.316 36.039 10.664 40.339l62.65 15.99-17.661 62.015c-4.991 17.838 11.829 34.663 29.661 29.671l61.994-17.667 15.984 62.671c4.439 18.575 27.696 24.018 40.325 10.668L256 458.61l44.989 46.001c12.5 13.488 35.987 7.486 40.325-10.668l15.984-62.671 61.994 17.667c17.836 4.994 34.651-11.837 29.661-29.671l-17.661-62.015 62.65-15.99c17.987-4.302 24.366-27.367 10.664-40.339l-45.984-45.004z"></path></svg> Certified Tidyverse Instructor, RStudio ### Research Interests <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M201.5 174.8l55.7 55.8c3.1 3.1 3.1 8.2 0 11.3l-11.3 11.3c-3.1 3.1-8.2 3.1-11.3 0l-55.7-55.8-45.3 45.3 55.8 55.8c3.1 3.1 3.1 8.2 0 11.3l-11.3 11.3c-3.1 3.1-8.2 3.1-11.3 0L111 265.2l-26.4 26.4c-17.3 17.3-25.6 41.1-23 65.4l7.1 63.6L2.3 487c-3.1 3.1-3.1 8.2 0 11.3l11.3 11.3c3.1 3.1 8.2 3.1 11.3 0l66.3-66.3 63.6 7.1c23.9 2.6 47.9-5.4 65.4-23l181.9-181.9-135.7-135.7-64.9 65zm308.2-93.3L430.5 2.3c-3.1-3.1-8.2-3.1-11.3 0l-11.3 11.3c-3.1 3.1-3.1 8.2 0 11.3l28.3 28.3-45.3 45.3-56.6-56.6-17-17c-3.1-3.1-8.2-3.1-11.3 0l-33.9 33.9c-3.1 3.1-3.1 8.2 0 11.3l17 17L424.8 223l17 17c3.1 3.1 8.2 3.1 11.3 0l33.9-34c3.1-3.1 3.1-8.2 0-11.3l-73.5-73.5 45.3-45.3 28.3 28.3c3.1 3.1 8.2 3.1 11.3 0l11.3-11.3c3.1-3.2 3.1-8.2 0-11.4z"></path></svg> Public Health, <svg viewBox="0 0 576 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M327.1 96c-89.97 0-168.54 54.77-212.27 101.63L27.5 131.58c-12.13-9.18-30.24.6-27.14 14.66L24.54 256 .35 365.77c-3.1 14.06 15.01 23.83 27.14 14.66l87.33-66.05C158.55 361.23 237.13 416 327.1 416 464.56 416 576 288 576 256S464.56 96 327.1 96zm87.43 184c-13.25 0-24-10.75-24-24 0-13.26 10.75-24 24-24 13.26 0 24 10.74 24 24 0 13.25-10.75 24-24 24z"></path></svg> Ecology, <svg viewBox="0 0 576 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M528 0H48C21.5 0 0 21.5 0 48v320c0 26.5 21.5 48 48 48h192l-16 48h-72c-13.3 0-24 10.7-24 24s10.7 24 24 24h272c13.3 0 24-10.7 24-24s-10.7-24-24-24h-72l-16-48h192c26.5 0 48-21.5 48-48V48c0-26.5-21.5-48-48-48zm-16 352H64V64h448v288z"></path></svg> Data Science ] .center[ [<svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M326.612 185.391c59.747 59.809 58.927 155.698.36 214.59-.11.12-.24.25-.36.37l-67.2 67.2c-59.27 59.27-155.699 59.262-214.96 0-59.27-59.26-59.27-155.7 0-214.96l37.106-37.106c9.84-9.84 26.786-3.3 27.294 10.606.648 17.722 3.826 35.527 9.69 52.721 1.986 5.822.567 12.262-3.783 16.612l-13.087 13.087c-28.026 28.026-28.905 73.66-1.155 101.96 28.024 28.579 74.086 28.749 102.325.51l67.2-67.19c28.191-28.191 28.073-73.757 0-101.83-3.701-3.694-7.429-6.564-10.341-8.569a16.037 16.037 0 0 1-6.947-12.606c-.396-10.567 3.348-21.456 11.698-29.806l21.054-21.055c5.521-5.521 14.182-6.199 20.584-1.731a152.482 152.482 0 0 1 20.522 17.197zM467.547 44.449c-59.261-59.262-155.69-59.27-214.96 0l-67.2 67.2c-.12.12-.25.25-.36.37-58.566 58.892-59.387 154.781.36 214.59a152.454 152.454 0 0 0 20.521 17.196c6.402 4.468 15.064 3.789 20.584-1.731l21.054-21.055c8.35-8.35 12.094-19.239 11.698-29.806a16.037 16.037 0 0 0-6.947-12.606c-2.912-2.005-6.64-4.875-10.341-8.569-28.073-28.073-28.191-73.639 0-101.83l67.2-67.19c28.239-28.239 74.3-28.069 102.325.51 27.75 28.3 26.872 73.934-1.155 101.96l-13.087 13.087c-4.35 4.35-5.769 10.79-3.783 16.612 5.864 17.194 9.042 34.999 9.69 52.721.509 13.906 17.454 20.446 27.294 10.606l37.106-37.106c59.271-59.259 59.271-155.699.001-214.959z"></path></svg> lauriebaker.rbind.io](https://lauriebaker.rbind.io) [<svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg> @llbaker1707](https://twitter.com/llbaker1707) [<svg viewBox="0 0 496 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> @laurielbaker](https://github.com/laurielbaker) ] --- layout: true # What we'll cover today. --- - Intro to spatial data and the `sf` package -- - Building a map using `ggplot2` -- - Coordinate Systems and geospatial objects in R -- - Building interactive maps using `leaflet`. --- layout: true # Packages for today's adventure --- ```r library(tidyverse) ## For plotting and data wrangling. library(leaflet) ## For leaflet interactive maps library(sf) ## For spatial data library(RColorBrewer) ## For colour palettes library(htmltools) ## For html library(leafsync) ## For placing plots side by side library(stringr) ## For character string manipulation library(rgeoboundaries) ## Geographic administrative boundaries library(kableExtra) ## Table output (in slides) ``` --- layout: false class: inverse center middle text-white .font200[Introduction to the sf package] --- layout: false # Intro to Spatial Data * Everything happens somewhere! -- * We represent spatial data in a variety of ways: points, lines, polygons, rasters. <img src="images/spatial_data_types.jpg" title="Diagram showing series of points (GPS locations), lines (roads), and polygons (e.g. countries and regions)" alt="Diagram showing series of points (GPS locations), lines (roads), and polygons (e.g. countries and regions)" width="900" /> -- <svg viewBox="0 0 352 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M96.06 454.35c.01 6.29 1.87 12.45 5.36 17.69l17.09 25.69a31.99 31.99 0 0 0 26.64 14.28h61.71a31.99 31.99 0 0 0 26.64-14.28l17.09-25.69a31.989 31.989 0 0 0 5.36-17.69l.04-38.35H96.01l.05 38.35zM0 176c0 44.37 16.45 84.85 43.56 115.78 16.52 18.85 42.36 58.23 52.21 91.45.04.26.07.52.11.78h160.24c.04-.26.07-.51.11-.78 9.85-33.22 35.69-72.6 52.21-91.45C335.55 260.85 352 220.37 352 176 352 78.61 272.91-.3 175.45 0 73.44.31 0 82.97 0 176zm176-80c-44.11 0-80 35.89-80 80 0 8.84-7.16 16-16 16s-16-7.16-16-16c0-61.76 50.24-112 112-112 8.84 0 16 7.16 16 16s-7.16 16-16 16z"></path></svg> What are some other examples of point, line, and polygon data? --- layout: false # The sf package * Package for geospatial data manipulation and analysis that works with features * **points** (POINT, MULTIPOINT) * **lines** (LINESTRING, MULTILINESTRING) * **polygons** (POLYGON, MULTIPOLYGON) <img src="images/spatial_data_types.jpg" title="Diagram showing series of points (GPS locations), lines (roads), and polygons (e.g. countries and regions)" alt="Diagram showing series of points (GPS locations), lines (roads), and polygons (e.g. countries and regions)" width="900" /> <svg viewBox="0 0 352 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M96.06 454.35c.01 6.29 1.87 12.45 5.36 17.69l17.09 25.69a31.99 31.99 0 0 0 26.64 14.28h61.71a31.99 31.99 0 0 0 26.64-14.28l17.09-25.69a31.989 31.989 0 0 0 5.36-17.69l.04-38.35H96.01l.05 38.35zM0 176c0 44.37 16.45 84.85 43.56 115.78 16.52 18.85 42.36 58.23 52.21 91.45.04.26.07.52.11.78h160.24c.04-.26.07-.51.11-.78 9.85-33.22 35.69-72.6 52.21-91.45C335.55 260.85 352 220.37 352 176 352 78.61 272.91-.3 175.45 0 73.44.31 0 82.97 0 176zm176-80c-44.11 0-80 35.89-80 80 0 8.84-7.16 16-16 16s-16-7.16-16-16c0-61.76 50.24-112 112-112 8.84 0 16 7.16 16 16s-7.16 16-16 16z"></path></svg> How do we represent these forms as data? <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> Hint: What do two points become when they are connected? --- layout: true # Births in North Carolina --- * Let's load in the data for North Carolina (`nc`) from the package `sf` using the function `st_read`. -- ```r nc_df <- st_read(system.file("shape/nc.shp", package="sf")) ``` ``` ## Reading layer `nc' from data source ## `/Library/Frameworks/R.framework/Versions/4.1/Resources/library/sf/shape/nc.shp' ## using driver `ESRI Shapefile' ## Simple feature collection with 100 features and 14 fields ## Geometry type: MULTIPOLYGON ## Dimension: XY ## Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965 ## Geodetic CRS: NAD27 ``` * `st` = spatial type and `.shp` is a common geographic shape file format. -- <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> `.shp` files come with a lot of other file types: `.shx`, `.dbf` etc. They all need to be kept together. --- layout: true # Rename the columns --- * Number of births for counties in North Carolina in 1974 * Rename our columns to country, births, and geometry. ```r nc <- nc_df %>% select("NAME", "BIR74", "BIR79", "geometry") %>% rename("county" = "NAME", "births1974" = "BIR74", "births1979" = "BIR79") ``` <br> <br> <br> <br> <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> The pipe symbol `%>%` means "and then". It's a way to combine multiple functions into an assembly line. --- layout: true # Getting to know the data --- * Let's load in the data for North Carolina (nc) ```r head(nc) ``` <div class="kable-table"> <table> <thead> <tr> <th style="text-align:left;"> county </th> <th style="text-align:right;"> births1974 </th> <th style="text-align:right;"> births1979 </th> <th style="text-align:left;"> geometry </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Ashe </td> <td style="text-align:right;"> 1091 </td> <td style="text-align:right;"> 1364 </td> <td style="text-align:left;"> MULTIPOLYGON (((-81.47276 3... </td> </tr> <tr> <td style="text-align:left;"> Alleghany </td> <td style="text-align:right;"> 487 </td> <td style="text-align:right;"> 542 </td> <td style="text-align:left;"> MULTIPOLYGON (((-81.23989 3... </td> </tr> <tr> <td style="text-align:left;"> Surry </td> <td style="text-align:right;"> 3188 </td> <td style="text-align:right;"> 3616 </td> <td style="text-align:left;"> MULTIPOLYGON (((-80.45634 3... </td> </tr> <tr> <td style="text-align:left;"> Currituck </td> <td style="text-align:right;"> 508 </td> <td style="text-align:right;"> 830 </td> <td style="text-align:left;"> MULTIPOLYGON (((-76.00897 3... </td> </tr> <tr> <td style="text-align:left;"> Northampton </td> <td style="text-align:right;"> 1421 </td> <td style="text-align:right;"> 1606 </td> <td style="text-align:left;"> MULTIPOLYGON (((-77.21767 3... </td> </tr> <tr> <td style="text-align:left;"> Hertford </td> <td style="text-align:right;"> 1452 </td> <td style="text-align:right;"> 1838 </td> <td style="text-align:left;"> MULTIPOLYGON (((-76.74506 3... </td> </tr> </tbody> </table> </div> <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> `head()` is a useful function that returns the first 6 rows of the data --- layout: true # And inspect the structure --- ```r str(nc) ``` ``` ## Classes 'sf' and 'data.frame': 100 obs. of 4 variables: ## $ county : chr "Ashe" "Alleghany" "Surry" "Currituck" ... ## $ births1974: num 1091 487 3188 508 1421 ... ## $ births1979: num 1364 542 3616 830 1606 ... ## $ geometry :sfc_MULTIPOLYGON of length 100; first list element: List of 1 ## ..$ :List of 1 ## .. ..$ : num [1:27, 1:2] -81.5 -81.5 -81.6 -81.6 -81.7 ... ## ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg" ## - attr(*, "sf_column")= chr "geometry" ## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA ## ..- attr(*, "names")= chr [1:3] "county" "births1974" "births1979" ``` <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> `str()` shows the structure of the data including the data types: `num`: numeric, `chr`: character, `sfc`: simple feature list column. --- layout: false class: inverse center middle text-white .font200[Plotting a map with ggplot2] --- layout: true # Building a map in ggplot2 --- .left-code[ ```r *ggplot(nc) ``` * .hlb[Data] * Geom * Aesthetics * Labels * Scales <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> Every plot starts with data. ] .right-plot[ <img src="leaflet_slides3_files/figure-html/first-map1a-1.png" title="Empty plotting area" alt="Empty plotting area" width="95%" /> ] --- .left-code[ ```r ggplot(nc) + * geom_sf() ``` * Data * .hlb[Geom] * Aesthetics * Labels * Scales <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> `geom_sf()` is reading our `geometry` column which contains the coordinates and creating our map from there. ] .right-plot[ <img src="leaflet_slides3_files/figure-html/first-map1b-1.png" title="Map of North Carolina showing border of counties." alt="Map of North Carolina showing border of counties." width="95%" /> ] --- .left-code[ ```r ggplot(nc) + * geom_sf(aes(fill = births1974)) ``` * Data * Geom * .hlb[Aesthetics] * Labels * Scales <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> `aes()` relates our variables (columns) to visual elements on the plot. ] .right-plot[ <img src="leaflet_slides3_files/figure-html/first-map1c-1.png" title="Map of North Carolina with administrative regions filled by number of births in North Carolina counties from 1974-78." alt="Map of North Carolina with administrative regions filled by number of births in North Carolina counties from 1974-78." width="95%" /> ] --- .left-code[ ```r ggplot(nc) + geom_sf(aes(fill = births1974)) + * labs(title = "Births per county in 1974-1978", * x = "Longitude", * y = "Latitude", * fill = "# Births") ``` * Data * Geom * Aesthetics * .hlb[Labels] * Scales <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> `labs()` adds labels to our plot. ] .right-plot[ <img src="leaflet_slides3_files/figure-html/first-map1d-1.png" title="Map updated to include a title, x and y axis labels, and a legend. Map of North Carolina with administrative regions filled by number of births in North Carolina counties from 1974-78. On the x axis, longitude, on the y axis latitude, and a legend indicating the number of suddent infant death cases." alt="Map updated to include a title, x and y axis labels, and a legend. Map of North Carolina with administrative regions filled by number of births in North Carolina counties from 1974-78. On the x axis, longitude, on the y axis latitude, and a legend indicating the number of suddent infant death cases." width="95%" /> ] --- .left-code[ ```r ggplot(nc) + geom_sf(aes(fill = births1974)) + labs(title = "Births per county in 1974-1978", x = "Longitude", y = "Latitude", fill = "# births") + * scale_y_continuous(breaks = 34:36) ``` * Data * Geom * Aesthetics * Labels * .hlb[Scales] <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> `scales` to change range. ] .right-plot[ <img src="leaflet_slides3_files/figure-html/first-map1e-1.png" title="Map updated so that the y axis has breaks at 34, 35, and 36 degrees latitude. Map of North Carolina with administrative regions filled by number of births in North Carolina counties from 1974-78. On the x axis, longitude, on the y axis latitude, and a legend indicating the number of suddent infant death cases." alt="Map updated so that the y axis has breaks at 34, 35, and 36 degrees latitude. Map of North Carolina with administrative regions filled by number of births in North Carolina counties from 1974-78. On the x axis, longitude, on the y axis latitude, and a legend indicating the number of suddent infant death cases." width="95%" /> ] --- layout: true # Accessing Administrative Boundaries --- .left-code[ ```r # One country *nigeria <- geoboundaries(country = "Nigeria") ggplot(data = nigeria) + geom_sf() ``` <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> We can access administrative data using the `geoboundaries` function from the `rgeoboundaries` package. ] .right-plot[ <img src="leaflet_slides3_files/figure-html/first-country-1-1.png" title="Map of Nigeria: admin level 0" alt="Map of Nigeria: admin level 0" width="95%" /> ] --- .left-code[ ```r # Two countries *nigeria_chad <- geoboundaries(country = c("Nigeria", "Chad")) ggplot(data = nigeria_chad) + geom_sf() ``` ] .right-plot[ <img src="leaflet_slides3_files/figure-html/first-country-2-1.png" title="Map of Nigeria and Chad: admin level 0." alt="Map of Nigeria and Chad: admin level 0." width="95%" /> ] --- .left-code[ ```r # Admin Level 1 nigeria <- geoboundaries(country = "Nigeria", * adm_lvl = "adm1") ggplot(data = nigeria) + geom_sf() ``` <svg viewBox="0 0 352 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M96.06 454.35c.01 6.29 1.87 12.45 5.36 17.69l17.09 25.69a31.99 31.99 0 0 0 26.64 14.28h61.71a31.99 31.99 0 0 0 26.64-14.28l17.09-25.69a31.989 31.989 0 0 0 5.36-17.69l.04-38.35H96.01l.05 38.35zM0 176c0 44.37 16.45 84.85 43.56 115.78 16.52 18.85 42.36 58.23 52.21 91.45.04.26.07.52.11.78h160.24c.04-.26.07-.51.11-.78 9.85-33.22 35.69-72.6 52.21-91.45C335.55 260.85 352 220.37 352 176 352 78.61 272.91-.3 175.45 0 73.44.31 0 82.97 0 176zm176-80c-44.11 0-80 35.89-80 80 0 8.84-7.16 16-16 16s-16-7.16-16-16c0-61.76 50.24-112 112-112 8.84 0 16 7.16 16 16s-7.16 16-16 16z"></path></svg> Find out more administrative boundaries and other open data sources tutorials at [rspatialdata.github.io](https://rspatialdata.github.io) ] .right-plot[ <img src="leaflet_slides3_files/figure-html/first-country-3-1.png" title="Map of Nigeria: admin level 1." alt="Map of Nigeria: admin level 1." width="95%" /> ] --- <table> <thead> <tr> <th style="text-align:left;"> shapeName </th> <th style="text-align:left;"> shapeISO </th> <th style="text-align:left;"> shapeID </th> <th style="text-align:left;"> shapeGroup </th> <th style="text-align:left;"> shapeType </th> <th style="text-align:left;"> geometry </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Abia </td> <td style="text-align:left;"> NG-AB </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B1 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((7.547192 5.... </td> </tr> <tr> <td style="text-align:left;"> Adamawa </td> <td style="text-align:left;"> NG-AD </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B2 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((13.71303 10... </td> </tr> <tr> <td style="text-align:left;"> Akwa lbom </td> <td style="text-align:left;"> NG-AK </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B3 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((8.332865 4.... </td> </tr> <tr> <td style="text-align:left;"> Anambra </td> <td style="text-align:left;"> NG-AN </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B4 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((7.339063 6.... </td> </tr> <tr> <td style="text-align:left;"> Bauchi </td> <td style="text-align:left;"> NG-BA </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B5 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((11.00732 11... </td> </tr> <tr> <td style="text-align:left;"> Bayelsa </td> <td style="text-align:left;"> NG-BY </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B6 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((5.746892 4.... </td> </tr> <tr> <td style="text-align:left;"> Benue </td> <td style="text-align:left;"> NG-BE </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B7 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((9.179126 7.... </td> </tr> <tr> <td style="text-align:left;"> Borno </td> <td style="text-align:left;"> NG-BO </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B8 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((13.71303 10... </td> </tr> <tr> <td style="text-align:left;"> Cross River </td> <td style="text-align:left;"> NG-CR </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B9 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((8.305132 4.... </td> </tr> <tr> <td style="text-align:left;"> Delta </td> <td style="text-align:left;"> NG-DE </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B10 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((6.668921 6.... </td> </tr> <tr> <td style="text-align:left;"> Ebonyi </td> <td style="text-align:left;"> NG-EB </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B11 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((8.294515 6.... </td> </tr> <tr> <td style="text-align:left;"> Edo </td> <td style="text-align:left;"> NG-ED </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B12 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((6.664701 6.... </td> </tr> <tr> <td style="text-align:left;"> Ekiti </td> <td style="text-align:left;"> NG-EK </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B13 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((5.773028 7.... </td> </tr> <tr> <td style="text-align:left;"> Enugu </td> <td style="text-align:left;"> NG-EN </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B14 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((7.807141 6.... </td> </tr> <tr> <td style="text-align:left;"> Federal Capital Territory </td> <td style="text-align:left;"> NG-FC </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B15 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((7.730055 9.... </td> </tr> <tr> <td style="text-align:left;"> Gombe </td> <td style="text-align:left;"> NG-GO </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B16 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((11.64015 10... </td> </tr> <tr> <td style="text-align:left;"> Imo </td> <td style="text-align:left;"> NG-IM </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B17 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((7.263917 5.... </td> </tr> <tr> <td style="text-align:left;"> Jigawa </td> <td style="text-align:left;"> NG-JI </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B18 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((10.59405 12... </td> </tr> <tr> <td style="text-align:left;"> Kaduna </td> <td style="text-align:left;"> NG-KD </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B19 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((8.779404 10... </td> </tr> <tr> <td style="text-align:left;"> Kano </td> <td style="text-align:left;"> NG-KN </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B20 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((9.193549 11... </td> </tr> <tr> <td style="text-align:left;"> Katsina </td> <td style="text-align:left;"> NG-KT </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B21 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((9.045213 12... </td> </tr> <tr> <td style="text-align:left;"> Kebbi </td> <td style="text-align:left;"> NG-KE </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B22 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((4.942946 11... </td> </tr> <tr> <td style="text-align:left;"> Kogi </td> <td style="text-align:left;"> NG-KO </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B23 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((6.82789 8.4... </td> </tr> <tr> <td style="text-align:left;"> Kwara </td> <td style="text-align:left;"> NG-KW </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B24 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((6.208536 8.... </td> </tr> <tr> <td style="text-align:left;"> Lagos </td> <td style="text-align:left;"> NG-LA </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B25 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((4.350838 6.... </td> </tr> <tr> <td style="text-align:left;"> Nasarawa </td> <td style="text-align:left;"> NG-NA </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B26 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((8.657807 9.... </td> </tr> <tr> <td style="text-align:left;"> Niger </td> <td style="text-align:left;"> NG-NI </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B27 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((5.960175 11... </td> </tr> <tr> <td style="text-align:left;"> Ogun </td> <td style="text-align:left;"> NG-OG </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B28 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((4.088356 7.... </td> </tr> <tr> <td style="text-align:left;"> Ondo </td> <td style="text-align:left;"> NG-ON </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B29 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((6.004126 7.... </td> </tr> <tr> <td style="text-align:left;"> Osun </td> <td style="text-align:left;"> NG-OS </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B30 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((5.057633 8.... </td> </tr> <tr> <td style="text-align:left;"> Oyo </td> <td style="text-align:left;"> NG-OY </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B31 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((4.568998 8.... </td> </tr> <tr> <td style="text-align:left;"> Plateau </td> <td style="text-align:left;"> NG-PL </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B32 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((8.823522 10... </td> </tr> <tr> <td style="text-align:left;"> Rivers </td> <td style="text-align:left;"> NG-RI </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B33 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((7.263917 5.... </td> </tr> <tr> <td style="text-align:left;"> Sokoto </td> <td style="text-align:left;"> NG-SO </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B34 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((6.791399 13... </td> </tr> <tr> <td style="text-align:left;"> Taraba </td> <td style="text-align:left;"> NG-TA </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B35 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((10.97489 9.... </td> </tr> <tr> <td style="text-align:left;"> Yobe </td> <td style="text-align:left;"> NG-YO </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B36 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((12.39355 13... </td> </tr> <tr> <td style="text-align:left;"> Zamfara </td> <td style="text-align:left;"> NG-ZA </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B37 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((7.032052 12... </td> </tr> </tbody> </table> --- layout: true # Joining spreadsheet data to spatial data --- * Let's say we have population data we want to match. <div class="kable-table"> <table> <thead> <tr> <th style="text-align:left;"> ADM0_NAME </th> <th style="text-align:left;"> ADM0_PCODE </th> <th style="text-align:left;"> ADM1_NAME </th> <th style="text-align:left;"> ADM1_PCODE </th> <th style="text-align:right;"> F_TL </th> <th style="text-align:right;"> M_TL </th> <th style="text-align:right;"> T_TL </th> <th style="text-align:right;"> F_00_04 </th> <th style="text-align:right;"> F_05_09 </th> <th style="text-align:right;"> F_10_14 </th> <th style="text-align:right;"> F_15_19 </th> <th style="text-align:right;"> F_20_24 </th> <th style="text-align:right;"> F_25_29 </th> <th style="text-align:right;"> F_30_34 </th> <th style="text-align:right;"> F_35_39 </th> <th style="text-align:right;"> F_40_44 </th> <th style="text-align:right;"> F_45_49 </th> <th style="text-align:right;"> F_50_54 </th> <th style="text-align:right;"> F_55_59 </th> <th style="text-align:right;"> F_60_64 </th> <th style="text-align:right;"> F_65_69 </th> <th style="text-align:right;"> F_70_74 </th> <th style="text-align:right;"> F_75_79 </th> <th style="text-align:right;"> F_80Plus </th> <th style="text-align:right;"> M_00_04 </th> <th style="text-align:right;"> M_05_09 </th> <th style="text-align:right;"> M_10_14 </th> <th style="text-align:right;"> M_15_19 </th> <th style="text-align:right;"> M_20_24 </th> <th style="text-align:right;"> M_25_29 </th> <th style="text-align:right;"> M_30_34 </th> <th style="text-align:right;"> M_35_39 </th> <th style="text-align:right;"> M_40_44 </th> <th style="text-align:right;"> M_45_49 </th> <th style="text-align:right;"> M_50_54 </th> <th style="text-align:right;"> M_55_59 </th> <th style="text-align:right;"> M_60_64 </th> <th style="text-align:right;"> M_65_69 </th> <th style="text-align:right;"> M_70_74 </th> <th style="text-align:right;"> M_75_79 </th> <th style="text-align:right;"> M_80Plus </th> <th style="text-align:right;"> T_00_04 </th> <th style="text-align:right;"> T_05_09 </th> <th style="text-align:right;"> T_10_14 </th> <th style="text-align:right;"> T_15_19 </th> <th style="text-align:right;"> T_20_24 </th> <th style="text-align:right;"> T_25_29 </th> <th style="text-align:right;"> T_30_34 </th> <th style="text-align:right;"> T_35_39 </th> <th style="text-align:right;"> T_40_44 </th> <th style="text-align:right;"> T_45_49 </th> <th style="text-align:right;"> T_50_54 </th> <th style="text-align:right;"> T_55_59 </th> <th style="text-align:right;"> T_60_64 </th> <th style="text-align:right;"> T_65_69 </th> <th style="text-align:right;"> T_70_74 </th> <th style="text-align:right;"> T_75_79 </th> <th style="text-align:left;"> T_80Plus </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> NIGERIA </td> <td style="text-align:left;"> NG </td> <td style="text-align:left;"> ABIA </td> <td style="text-align:left;"> NG001 </td> <td style="text-align:right;"> 1937239 </td> <td style="text-align:right;"> 1941738 </td> <td style="text-align:right;"> 3878977 </td> <td style="text-align:right;"> 210483 </td> <td style="text-align:right;"> 215867 </td> <td style="text-align:right;"> 248705 </td> <td style="text-align:right;"> 235360 </td> <td style="text-align:right;"> 175331 </td> <td style="text-align:right;"> 141231 </td> <td style="text-align:right;"> 124752 </td> <td style="text-align:right;"> 118724 </td> <td style="text-align:right;"> 97191 </td> <td style="text-align:right;"> 89290 </td> <td style="text-align:right;"> 68647 </td> <td style="text-align:right;"> 66458 </td> <td style="text-align:right;"> 45452 </td> <td style="text-align:right;"> 42233 </td> <td style="text-align:right;"> 27155 </td> <td style="text-align:right;"> 18965 </td> <td style="text-align:right;"> 11395 </td> <td style="text-align:right;"> 218758 </td> <td style="text-align:right;"> 224631 </td> <td style="text-align:right;"> 252614 </td> <td style="text-align:right;"> 244697 </td> <td style="text-align:right;"> 197853 </td> <td style="text-align:right;"> 147846 </td> <td style="text-align:right;"> 119759 </td> <td style="text-align:right;"> 104223 </td> <td style="text-align:right;"> 96942 </td> <td style="text-align:right;"> 86562 </td> <td style="text-align:right;"> 66047 </td> <td style="text-align:right;"> 60055 </td> <td style="text-align:right;"> 38619 </td> <td style="text-align:right;"> 36743 </td> <td style="text-align:right;"> 21766 </td> <td style="text-align:right;"> 16548 </td> <td style="text-align:right;"> 8075 </td> <td style="text-align:right;"> 429241 </td> <td style="text-align:right;"> 440498 </td> <td style="text-align:right;"> 501319 </td> <td style="text-align:right;"> 480057 </td> <td style="text-align:right;"> 373184 </td> <td style="text-align:right;"> 289077 </td> <td style="text-align:right;"> 244511 </td> <td style="text-align:right;"> 222947 </td> <td style="text-align:right;"> 194133 </td> <td style="text-align:right;"> 175852 </td> <td style="text-align:right;"> 134694 </td> <td style="text-align:right;"> 126513 </td> <td style="text-align:right;"> 84071 </td> <td style="text-align:right;"> 78976 </td> <td style="text-align:right;"> 48921 </td> <td style="text-align:right;"> 35513 </td> <td style="text-align:left;"> 19470 </td> </tr> <tr> <td style="text-align:left;"> NIGERIA </td> <td style="text-align:left;"> NG </td> <td style="text-align:left;"> ADAMAWA </td> <td style="text-align:left;"> NG002 </td> <td style="text-align:right;"> 2168252 </td> <td style="text-align:right;"> 2228347 </td> <td style="text-align:right;"> 4396599 </td> <td style="text-align:right;"> 372760 </td> <td style="text-align:right;"> 320576 </td> <td style="text-align:right;"> 273011 </td> <td style="text-align:right;"> 233204 </td> <td style="text-align:right;"> 190216 </td> <td style="text-align:right;"> 155723 </td> <td style="text-align:right;"> 131631 </td> <td style="text-align:right;"> 105703 </td> <td style="text-align:right;"> 93150 </td> <td style="text-align:right;"> 67345 </td> <td style="text-align:right;"> 59734 </td> <td style="text-align:right;"> 45221 </td> <td style="text-align:right;"> 39961 </td> <td style="text-align:right;"> 29740 </td> <td style="text-align:right;"> 26280 </td> <td style="text-align:right;"> 14250 </td> <td style="text-align:right;"> 9747 </td> <td style="text-align:right;"> 391921 </td> <td style="text-align:right;"> 331150 </td> <td style="text-align:right;"> 277805 </td> <td style="text-align:right;"> 238530 </td> <td style="text-align:right;"> 190232 </td> <td style="text-align:right;"> 160292 </td> <td style="text-align:right;"> 138807 </td> <td style="text-align:right;"> 119876 </td> <td style="text-align:right;"> 95845 </td> <td style="text-align:right;"> 76036 </td> <td style="text-align:right;"> 56748 </td> <td style="text-align:right;"> 48899 </td> <td style="text-align:right;"> 32936 </td> <td style="text-align:right;"> 28317 </td> <td style="text-align:right;"> 20135 </td> <td style="text-align:right;"> 13404 </td> <td style="text-align:right;"> 7414 </td> <td style="text-align:right;"> 764681 </td> <td style="text-align:right;"> 651726 </td> <td style="text-align:right;"> 550816 </td> <td style="text-align:right;"> 471734 </td> <td style="text-align:right;"> 380448 </td> <td style="text-align:right;"> 316015 </td> <td style="text-align:right;"> 270438 </td> <td style="text-align:right;"> 225579 </td> <td style="text-align:right;"> 188995 </td> <td style="text-align:right;"> 143381 </td> <td style="text-align:right;"> 116482 </td> <td style="text-align:right;"> 94120 </td> <td style="text-align:right;"> 72897 </td> <td style="text-align:right;"> 58057 </td> <td style="text-align:right;"> 46415 </td> <td style="text-align:right;"> 27654 </td> <td style="text-align:left;"> 17161 </td> </tr> <tr> <td style="text-align:left;"> NIGERIA </td> <td style="text-align:left;"> NG </td> <td style="text-align:left;"> AKWA IBOM </td> <td style="text-align:left;"> NG003 </td> <td style="text-align:right;"> 2789212 </td> <td style="text-align:right;"> 2876552 </td> <td style="text-align:right;"> 5665764 </td> <td style="text-align:right;"> 326260 </td> <td style="text-align:right;"> 342223 </td> <td style="text-align:right;"> 353787 </td> <td style="text-align:right;"> 329383 </td> <td style="text-align:right;"> 246884 </td> <td style="text-align:right;"> 207987 </td> <td style="text-align:right;"> 184349 </td> <td style="text-align:right;"> 180667 </td> <td style="text-align:right;"> 144644 </td> <td style="text-align:right;"> 130441 </td> <td style="text-align:right;"> 94389 </td> <td style="text-align:right;"> 82954 </td> <td style="text-align:right;"> 63215 </td> <td style="text-align:right;"> 52546 </td> <td style="text-align:right;"> 26086 </td> <td style="text-align:right;"> 15922 </td> <td style="text-align:right;"> 7475 </td> <td style="text-align:right;"> 345584 </td> <td style="text-align:right;"> 364957 </td> <td style="text-align:right;"> 371722 </td> <td style="text-align:right;"> 355275 </td> <td style="text-align:right;"> 285723 </td> <td style="text-align:right;"> 216091 </td> <td style="text-align:right;"> 174848 </td> <td style="text-align:right;"> 159735 </td> <td style="text-align:right;"> 143522 </td> <td style="text-align:right;"> 125640 </td> <td style="text-align:right;"> 96021 </td> <td style="text-align:right;"> 77876 </td> <td style="text-align:right;"> 61936 </td> <td style="text-align:right;"> 46240 </td> <td style="text-align:right;"> 27453 </td> <td style="text-align:right;"> 16233 </td> <td style="text-align:right;"> 7696 </td> <td style="text-align:right;"> 671844 </td> <td style="text-align:right;"> 707180 </td> <td style="text-align:right;"> 725509 </td> <td style="text-align:right;"> 684658 </td> <td style="text-align:right;"> 532607 </td> <td style="text-align:right;"> 424078 </td> <td style="text-align:right;"> 359197 </td> <td style="text-align:right;"> 340402 </td> <td style="text-align:right;"> 288166 </td> <td style="text-align:right;"> 256081 </td> <td style="text-align:right;"> 190410 </td> <td style="text-align:right;"> 160830 </td> <td style="text-align:right;"> 125151 </td> <td style="text-align:right;"> 98786 </td> <td style="text-align:right;"> 53539 </td> <td style="text-align:right;"> 32155 </td> <td style="text-align:left;"> 15171 </td> </tr> <tr> <td style="text-align:left;"> NIGERIA </td> <td style="text-align:left;"> NG </td> <td style="text-align:left;"> ANAMBRA </td> <td style="text-align:left;"> NG004 </td> <td style="text-align:right;"> 2932876 </td> <td style="text-align:right;"> 3003908 </td> <td style="text-align:right;"> 5936784 </td> <td style="text-align:right;"> 319470 </td> <td style="text-align:right;"> 334304 </td> <td style="text-align:right;"> 379733 </td> <td style="text-align:right;"> 357447 </td> <td style="text-align:right;"> 273463 </td> <td style="text-align:right;"> 225850 </td> <td style="text-align:right;"> 193788 </td> <td style="text-align:right;"> 185580 </td> <td style="text-align:right;"> 149135 </td> <td style="text-align:right;"> 135868 </td> <td style="text-align:right;"> 95878 </td> <td style="text-align:right;"> 95605 </td> <td style="text-align:right;"> 59135 </td> <td style="text-align:right;"> 56517 </td> <td style="text-align:right;"> 32959 </td> <td style="text-align:right;"> 25634 </td> <td style="text-align:right;"> 12510 </td> <td style="text-align:right;"> 332996 </td> <td style="text-align:right;"> 352714 </td> <td style="text-align:right;"> 388676 </td> <td style="text-align:right;"> 376016 </td> <td style="text-align:right;"> 304587 </td> <td style="text-align:right;"> 232124 </td> <td style="text-align:right;"> 187703 </td> <td style="text-align:right;"> 164634 </td> <td style="text-align:right;"> 153188 </td> <td style="text-align:right;"> 134792 </td> <td style="text-align:right;"> 105401 </td> <td style="text-align:right;"> 89482 </td> <td style="text-align:right;"> 63031 </td> <td style="text-align:right;"> 53670 </td> <td style="text-align:right;"> 31439 </td> <td style="text-align:right;"> 22388 </td> <td style="text-align:right;"> 11067 </td> <td style="text-align:right;"> 652466 </td> <td style="text-align:right;"> 687018 </td> <td style="text-align:right;"> 768409 </td> <td style="text-align:right;"> 733463 </td> <td style="text-align:right;"> 578050 </td> <td style="text-align:right;"> 457974 </td> <td style="text-align:right;"> 381491 </td> <td style="text-align:right;"> 350214 </td> <td style="text-align:right;"> 302323 </td> <td style="text-align:right;"> 270660 </td> <td style="text-align:right;"> 201279 </td> <td style="text-align:right;"> 185087 </td> <td style="text-align:right;"> 122166 </td> <td style="text-align:right;"> 110187 </td> <td style="text-align:right;"> 64398 </td> <td style="text-align:right;"> 48022 </td> <td style="text-align:left;"> 23577 </td> </tr> <tr> <td style="text-align:left;"> NIGERIA </td> <td style="text-align:left;"> NG </td> <td style="text-align:left;"> BAUCHI </td> <td style="text-align:left;"> NG005 </td> <td style="text-align:right;"> 3272516 </td> <td style="text-align:right;"> 3417618 </td> <td style="text-align:right;"> 6690134 </td> <td style="text-align:right;"> 639299 </td> <td style="text-align:right;"> 509011 </td> <td style="text-align:right;"> 402259 </td> <td style="text-align:right;"> 352254 </td> <td style="text-align:right;"> 285240 </td> <td style="text-align:right;"> 235858 </td> <td style="text-align:right;"> 198776 </td> <td style="text-align:right;"> 153495 </td> <td style="text-align:right;"> 133251 </td> <td style="text-align:right;"> 90278 </td> <td style="text-align:right;"> 83531 </td> <td style="text-align:right;"> 51340 </td> <td style="text-align:right;"> 51485 </td> <td style="text-align:right;"> 30115 </td> <td style="text-align:right;"> 28710 </td> <td style="text-align:right;"> 15170 </td> <td style="text-align:right;"> 12444 </td> <td style="text-align:right;"> 679500 </td> <td style="text-align:right;"> 545659 </td> <td style="text-align:right;"> 442014 </td> <td style="text-align:right;"> 362751 </td> <td style="text-align:right;"> 260764 </td> <td style="text-align:right;"> 223221 </td> <td style="text-align:right;"> 203035 </td> <td style="text-align:right;"> 168629 </td> <td style="text-align:right;"> 143572 </td> <td style="text-align:right;"> 103270 </td> <td style="text-align:right;"> 89342 </td> <td style="text-align:right;"> 57851 </td> <td style="text-align:right;"> 50345 </td> <td style="text-align:right;"> 30837 </td> <td style="text-align:right;"> 29566 </td> <td style="text-align:right;"> 15850 </td> <td style="text-align:right;"> 11412 </td> <td style="text-align:right;"> 1318799 </td> <td style="text-align:right;"> 1054670 </td> <td style="text-align:right;"> 844273 </td> <td style="text-align:right;"> 715005 </td> <td style="text-align:right;"> 546004 </td> <td style="text-align:right;"> 459079 </td> <td style="text-align:right;"> 401811 </td> <td style="text-align:right;"> 322124 </td> <td style="text-align:right;"> 276823 </td> <td style="text-align:right;"> 193548 </td> <td style="text-align:right;"> 172873 </td> <td style="text-align:right;"> 109191 </td> <td style="text-align:right;"> 101830 </td> <td style="text-align:right;"> 60952 </td> <td style="text-align:right;"> 58276 </td> <td style="text-align:right;"> 31020 </td> <td style="text-align:left;"> 23856 </td> </tr> <tr> <td style="text-align:left;"> NIGERIA </td> <td style="text-align:left;"> NG </td> <td style="text-align:left;"> BAYELSA </td> <td style="text-align:left;"> NG006 </td> <td style="text-align:right;"> 1236961 </td> <td style="text-align:right;"> 1300813 </td> <td style="text-align:right;"> 2537774 </td> <td style="text-align:right;"> 150454 </td> <td style="text-align:right;"> 157800 </td> <td style="text-align:right;"> 157102 </td> <td style="text-align:right;"> 144745 </td> <td style="text-align:right;"> 109296 </td> <td style="text-align:right;"> 93373 </td> <td style="text-align:right;"> 84237 </td> <td style="text-align:right;"> 80603 </td> <td style="text-align:right;"> 62980 </td> <td style="text-align:right;"> 55671 </td> <td style="text-align:right;"> 38799 </td> <td style="text-align:right;"> 34675 </td> <td style="text-align:right;"> 24906 </td> <td style="text-align:right;"> 19395 </td> <td style="text-align:right;"> 10835 </td> <td style="text-align:right;"> 7773 </td> <td style="text-align:right;"> 4317 </td> <td style="text-align:right;"> 158035 </td> <td style="text-align:right;"> 169405 </td> <td style="text-align:right;"> 167379 </td> <td style="text-align:right;"> 160580 </td> <td style="text-align:right;"> 128084 </td> <td style="text-align:right;"> 98740 </td> <td style="text-align:right;"> 82485 </td> <td style="text-align:right;"> 72773 </td> <td style="text-align:right;"> 65964 </td> <td style="text-align:right;"> 54387 </td> <td style="text-align:right;"> 43741 </td> <td style="text-align:right;"> 31487 </td> <td style="text-align:right;"> 25781 </td> <td style="text-align:right;"> 17586 </td> <td style="text-align:right;"> 12790 </td> <td style="text-align:right;"> 7288 </td> <td style="text-align:right;"> 4308 </td> <td style="text-align:right;"> 308489 </td> <td style="text-align:right;"> 327205 </td> <td style="text-align:right;"> 324481 </td> <td style="text-align:right;"> 305325 </td> <td style="text-align:right;"> 237380 </td> <td style="text-align:right;"> 192113 </td> <td style="text-align:right;"> 166722 </td> <td style="text-align:right;"> 153376 </td> <td style="text-align:right;"> 128944 </td> <td style="text-align:right;"> 110058 </td> <td style="text-align:right;"> 82540 </td> <td style="text-align:right;"> 66162 </td> <td style="text-align:right;"> 50687 </td> <td style="text-align:right;"> 36981 </td> <td style="text-align:right;"> 23625 </td> <td style="text-align:right;"> 15061 </td> <td style="text-align:left;"> 8625 </td> </tr> </tbody> </table> </div> <svg viewBox="0 0 352 512" style="position:relative;display:inline-block;top:.1em;height:2em;" xmlns="http://www.w3.org/2000/svg"> <path d="M96.06 454.35c.01 6.29 1.87 12.45 5.36 17.69l17.09 25.69a31.99 31.99 0 0 0 26.64 14.28h61.71a31.99 31.99 0 0 0 26.64-14.28l17.09-25.69a31.989 31.989 0 0 0 5.36-17.69l.04-38.35H96.01l.05 38.35zM0 176c0 44.37 16.45 84.85 43.56 115.78 16.52 18.85 42.36 58.23 52.21 91.45.04.26.07.52.11.78h160.24c.04-.26.07-.51.11-.78 9.85-33.22 35.69-72.6 52.21-91.45C335.55 260.85 352 220.37 352 176 352 78.61 272.91-.3 175.45 0 73.44.31 0 82.97 0 176zm176-80c-44.11 0-80 35.89-80 80 0 8.84-7.16 16-16 16s-16-7.16-16-16c0-61.76 50.24-112 112-112 8.84 0 16 7.16 16 16s-7.16 16-16 16z"></path></svg> In what column do we find spatial information we can use to match this data to the other dataset? --- * We can match the data based on the names of the administrative boundaries. ```r # Joining Nigeria spatial data to the population data by the admin name columns nigeria_pop <- nigeria_pop %>% mutate(ADM1_NAME = str_to_title(ADM1_NAME, locale = "en")) # how well do they match up? anti_join(x = nigeria, y = nigeria_pop, by = c("shapeName" = "ADM1_NAME")) ``` <div class="kable-table"> <table> <thead> <tr> <th style="text-align:left;"> shapeName </th> <th style="text-align:left;"> shapeISO </th> <th style="text-align:left;"> shapeID </th> <th style="text-align:left;"> shapeGroup </th> <th style="text-align:left;"> shapeType </th> <th style="text-align:left;"> geometry </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Akwa lbom </td> <td style="text-align:left;"> NG-AK </td> <td style="text-align:left;"> NGA-ADM1-1590546715-B3 </td> <td style="text-align:left;"> NGA </td> <td style="text-align:left;"> ADM1 </td> <td style="text-align:left;"> MULTIPOLYGON (((8.332865 4.... </td> </tr> </tbody> </table> </div> ??? Often one of the trickiest part is to get the spelling of regions to match up. We can check where mismatches occur with an `anti_join`. The NA here is from Akwa Lbom and Akwa lbom. --- * We can fix mismatches using `case_when` ```r # Joining Nigeria spatial data to the population data by the administrative data column nigeria_pop <- nigeria_pop %>% mutate(ADM1_NAME = str_to_title(ADM1_NAME, locale = "en"), ADM1_NAME_SC = case_when(ADM1_NAME == "Akwa Ibom" ~ "Akwa lbom", TRUE ~ ADM1_NAME)) nigeria_df <- left_join(x = nigeria, y = nigeria_pop, by = c("shapeName" = "ADM1_NAME_SC")) ``` <svg viewBox="0 0 512 512" style="position:relative;display:inline-block;top:.1em;height:1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg> We can use `case_when` to change the name to match. --- * Now we can create our map .left-code[ ```r ggplot(data = nigeria_df) + geom_sf(aes(fill = T_TL)) + labs(x = "Longitude", y = "Latitude", fill = "Total Population") ``` ] .right-plot[ <img src="leaflet_slides3_files/figure-html/first-nigeria-map3-1.png" title="Map of Nigeria with admin 1 areas filled by population" alt="Map of Nigeria with admin 1 areas filled by population" width="95%" /> ] --- layout: true # Coordinate reference system --- * Every location on earth is specified by a longitude and latitude. -- * The challenge is transferring a 3D object to a 2D surface. -- <img src="images/OrangePeelProGoode.jpg" title="Orange peel with earth drawn on it flattened in shape of Goode projection" alt="Orange peel with earth drawn on it flattened in shape of Goode projection" width="800" /> -- * The Coordinate Reference system (CRS) determines how the data will be projected onto a map. --- layout: true # Coordinate reference system --- * We can flatten a 3D object to 2D in multiple ways <figure> <img src="images/map_projections.gif" width="850px"> </figure> [Projection Transitions](https://bl.ocks.org/mbostock/raw/3711652/) by Mick Bostock --- layout: true # Coordinate reference systems skew true size --- <img src="images/Nigeria_true_size.PNG" title="Map of the world showing Nigeria overlaid on Greenland. The map demonstrates how coordinate reference systems skew our perception of how big a country is" alt="Map of the world showing Nigeria overlaid on Greenland. The map demonstrates how coordinate reference systems skew our perception of how big a country is" width="50%" /> [The True Size Of: thetruesize.com](https://thetruesize.com/#?borders=1~!MTczMDY3NDE.MTE4OTczMjc*MjU3NDY1NzA(MzkyMTU3MA~!KE*OTYxNTIxNw.MTY3MDYyNzg) --- layout: true # Checking the coordinate reference system --- * We can check the CRS using `st_crs`: ```r st_crs(nc) ``` ``` ## Coordinate Reference System: ## User input: NAD27 ## wkt: ## GEOGCRS["NAD27", ## DATUM["North American Datum 1927", ## ELLIPSOID["Clarke 1866",6378206.4,294.978698213898, ## LENGTHUNIT["metre",1]]], ## PRIMEM["Greenwich",0, ## ANGLEUNIT["degree",0.0174532925199433]], ## CS[ellipsoidal,2], ## AXIS["latitude",north, ## ORDER[1], ## ANGLEUNIT["degree",0.0174532925199433]], ## AXIS["longitude",east, ## ORDER[2], ## ANGLEUNIT["degree",0.0174532925199433]], ## ID["EPSG",4267]] ``` --- layout: true # Transforming coordinate reference system --- * You can transform a coordinate reference system using `st_transform()`. -- * But what is a sensible coordinate reference system to assign? -- * EPSG 4326 aka WGS 1984 is a standard for use in cartography used by GPS technologies and is the default in leaflet. ```r nc <- st_transform(nc, "+init=epsg:4326") st_crs(nc) ``` ``` ## Coordinate Reference System: ## User input: +init=epsg:4326 ## wkt: ## GEOGCRS["unknown", ## DATUM["World Geodetic System 1984", ## ELLIPSOID["WGS 84",6378137,298.257223563, ## LENGTHUNIT["metre",1]], ## ID["EPSG",6326]], ## PRIMEM["Greenwich",0, ## ANGLEUNIT["degree",0.0174532925199433], ## ID["EPSG",8901]], ## CS[ellipsoidal,2], ## AXIS["longitude",east, ## ORDER[1], ## ANGLEUNIT["degree",0.0174532925199433, ## ID["EPSG",9122]]], ## AXIS["latitude",north, ## ORDER[2], ## ANGLEUNIT["degree",0.0174532925199433, ## ID["EPSG",9122]]]] ``` --- layout: true # Recap --- * Spatial data are represented as points, lines, polygons, and rasters. -- * We can store these spatial features as `geometry` using the `sf` package. -- * We can access administrative boundaries for several countries using the `rgeoboundaries` package. -- * When we are working with spatial data we need to take into account the coordinate reference system. -- * We can use other information in our data set to create the visual elements of our map. --- layout: false class: inverse center middle text-white .font200[Introduction to leaflet mapping] --- layout: true # Introduction to leaflet mapping --- * Leaflet is one of the most popular open-source JavaScript libraries for interactive maps * Like `ggplot2` it is built in layers on top of a base map --- layout: true # Our first leaflet map --- .left-code[ * Every plot starts with `leaflet()` ```r leaflet(data = nc) ``` ] .right-plot[
] --- .left-code[ * Layers are added using `%>%` ```r leaflet(data = nc) %>% * addTiles() ``` ] .right-plot[
] -- N.B. Layers are added with `%>%` in `leaflet` and `+` in `ggplot`. `%>%` also is used in the `tidyverse` packages. --- .left-code[ * We can set the view using `setView()` ```r leaflet(data = nc) %>% addTiles() %>% * setView(lng = -80, * lat = 34.5, * zoom = 5) ``` <!-- * **Your Turn**: Try setting lng to 3.17 and lat to 44.37. Try setting different values between 0 and 10 for the zoom. --> * **Your Turn**: Try setting lng to 8.13 and lat to 6.88. Try setting different values between 0 and 10 for the zoom. ] .right-plot[
] --- .left-code[ * We can set the view using `setView()` ```r leaflet(data = nc) %>% addTiles() %>% * setView(lng = 8.13, * lat = 6.88, * zoom = 6) ``` ] .right-plot[
] --- layout: true # Our first leaflet map --- .left-code[ * `addProviderTiles` give different base maps ```r leaflet(data = nc) %>% addProviderTiles(providers$Stamen.Terrain) %>% setView(lng = -80, lat = 34.5, zoom = 5) ``` * **Your Turn**: Choose a different provider tile: `providers$` + `Tab`. ] .right-plot[
N.B. The different provider tiles come with different licensing and some require an API. ] --- .left-code[ * Add polygons using `addPolygons()` ```r leaflet(data = nc) %>% addProviderTiles(providers$Stamen.Terrain) %>% setView(lng = -80, lat = 34.5, zoom = 5) %>% * addPolygons() ``` * **Your Turn**: Try creating a leaflet map with the nigeria data. ] .right-plot[
] --- layout: true # Creating a colour palette --- <img src="images/ColorBrewer_Ex.PNG" title="Map of U.S. Counties showing green blue colour scheme" alt="Map of U.S. Counties showing green blue colour scheme" width="550" style="display: block; margin: auto;" /> * What type of variables are we showing (numerical, categorical)? -- * Who is our audience? -- * What story are we telling with our data? -- * Choose your colours: [ColorBrewer2.org](https://colorbrewer2.org/#type=sequential&scheme=BuGn&n=3) and https://www.colorcodepicker.com/ --- layout: true # Creating a colour palette --- * Is our data numeric? <img src="images/continuous_discrete.png" title="Yellow chick describing how tall it is (3.1 inches) and how much it weighs (34.16 grams) to illustrate continuous data, measured data that can have infinite values within a possible range. An octopus describing how many legs it has (8) and how many spots it has (4) to illustrate discrete observations that can only exist at limited values, often counts." alt="Yellow chick describing how tall it is (3.1 inches) and how much it weighs (34.16 grams) to illustrate continuous data, measured data that can have infinite values within a possible range. An octopus describing how many legs it has (8) and how many spots it has (4) to illustrate discrete observations that can only exist at limited values, often counts." width="800" /> --- layout: true # Creating a colour palette --- * Is our data categorical? <img src="images/nominal_ordinal_binary.png" title="Species such as turtle, snail, and butterfly are unordered descriptions known as nominal data. Emotiosn are an example of ordinal data (e.g. I am an awesome bee, I am an unhappy bee, I am an okay bee). Extinct (dinosaur) and not extinct (shark) are examples of binary data where there are only two possibilities" alt="Species such as turtle, snail, and butterfly are unordered descriptions known as nominal data. Emotiosn are an example of ordinal data (e.g. I am an awesome bee, I am an unhappy bee, I am an okay bee). Extinct (dinosaur) and not extinct (shark) are examples of binary data where there are only two possibilities" width="800" /> --- layout: true # Creating a colour palette --- * `RColorBrewer` includes **sequential** colour palettes (e.g. number of people). ```r display.brewer.all(type = "seq", colorblindFriendly = TRUE) ``` <img src="leaflet_slides3_files/figure-html/pal-sequential-1.png" title="Diagram showing sequential palettes available in RColorBrewer including Orange to Red and more" alt="Diagram showing sequential palettes available in RColorBrewer including Orange to Red and more" width="45%" /> --- layout: true # Creating a colour palette --- * `RColorBrewer` includes **diverging** colour palettes (e.g. to show above or below). ```r display.brewer.all(type = "div", colorblindFriendly = TRUE) ``` <img src="leaflet_slides3_files/figure-html/pal-diverging-1.png" title="Diagram showing sequential palettes available in RColorBrewer including Spectral and Red to Blue" alt="Diagram showing sequential palettes available in RColorBrewer including Spectral and Red to Blue" width="45%" /> --- layout: true # Creating a colour palette --- * First we will define the colour palette and bins for the plot. ```r summary(nc$births1974) ``` ``` ## Min. 1st Qu. Median Mean 3rd Qu. Max. ## 248 1077 2180 3300 3936 21588 ``` ```r bins <- seq(from = 0, to = 25000, by = 5000) ``` -- * Then we can define the colours for the palette: ```r pal74 <- colorBin("OrRd", domain = nc$births1974, bins = bins) ``` * **Your Turn**: Try creating a color palette for the nigeria data for the total population variable `TL` --- .left-code[ * Customizing `addPolygons()` ```r leaflet(data = nc) %>% addProviderTiles(providers$Esri.OceanBasemap) %>% setView(lng = -80, lat = 34.5, zoom = 6) %>% addPolygons( fillColor = ~pal74(nc$births1974), * fillOpacity = 0.7, * color = "white", * opacity = 1, * weight = 2 ) ``` ] .right-plot[
] --- .left-code[ * Customising `addPolygons()` ```r leaflet(data = nc) %>% addProviderTiles(providers$Esri.OceanBasemap) %>% setView(lng = -80, lat = 34.5, zoom = 6) %>% addPolygons( * fillColor = ~pal74(nc$births1974), * fillOpacity = 1, * color = "blue", * opacity = 0.7, * weight = 1 ) ``` ] .right-plot[
* **Your Turn** Try changing the `color`, `opacity`, and `weight`. Next try adding the palette to the Nigeria plot. ] --- layout: true # What can you customise in addPolygons() --- ```r ?addPolygons() ``` * `color:` stroke color * `weight:` stroke width in pixels * `opacity:` stroke opacity * `fillColor:` fill color * `fillOpacity:` fill opacity -- * `highlightOptions:` Options for highlighting the shape on mouse over. --- .left-code[ * Let's assign our plot to an object. ```r *m1 <- leaflet(data = nc) %>% addProviderTiles(providers$Stamen.Terrain) %>% setView(lng = -80, lat = 34.5, zoom = 6) m1 %>% addPolygons( fillColor = ~pal74(nc$births1974), fillOpacity = 0.7, opacity = 1, color = "white", weight = 2) ``` ] .right-plot[
] --- .left-code[ * Let's add some `highlightOptions` ```r m1 %>% addPolygons( fillColor = ~pal74(nc$births1974), fillOpacity = 0.7, color = "white", opacity = 1, weight = 2, * highlight = highlightOptions( * weight = 3, * color = "blue", * fillOpacity = 1, * bringToFront = TRUE)) ``` ] .right-plot[
] --- layout: true # Let's add some labels! --- `sprintf`: returns a character vector containing a formatted combination of text and variable values. ```r labels <- sprintf("<strong>%s</strong><br/>%g births", nc$county, nc$births1974) %>% lapply(htmltools::HTML) head(labels, 1) ``` ``` ## [[1]] ## <strong>Ashe</strong><br/>1091 births ``` -- html - markup language for the web * `<strong>` = bold; `<br/>` = new line -- PHP - Hypertext Preprocessor * `%s` = place holder for a character string; `%g` = general format place holder for a number --- .left-code[ * Let's add some labels ```r (m1 <- m1 %>% addPolygons(data = nc, fillColor = ~pal74(nc$births1974), fillOpacity = 0.7, color = "white", opacity = 1, weight = 2, highlight = highlightOptions( weight = 3, color = "blue", fillOpacity = 1, bringToFront = TRUE), * label = labels)) ``` ] .right-plot[
] --- .left-code[ * Let's add a legend ```r m1 <- m1 %>% * addLegend( * position = "bottomright", * pal = pal74, * values = ~nc$births1974, * title = "Births by county in 1974", * opacity = 1) m1 ``` ] .right-plot[
] --- layout: true # Leaflet map with points --- ```r head(work) ``` <div class="kable-table"> <table> <thead> <tr> <th style="text-align:left;"> location </th> <th style="text-align:left;"> institute </th> <th style="text-align:left;"> work </th> <th style="text-align:right;"> lat </th> <th style="text-align:right;"> lon </th> <th style="text-align:left;"> icon </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Valparaíso, Chile </td> <td style="text-align:left;"> Instituto de Fomento Pesquero </td> <td style="text-align:left;"> Chilean Pink Cusk Eel </td> <td style="text-align:right;"> -33.0472 </td> <td style="text-align:right;"> -71.6127 </td> <td style="text-align:left;"> fish </td> </tr> <tr> <td style="text-align:left;"> Curitiba, Brasil </td> <td style="text-align:left;"> Universidade Federal do Paraná </td> <td style="text-align:left;"> Fox rabies </td> <td style="text-align:right;"> -25.4290 </td> <td style="text-align:right;"> -49.2671 </td> <td style="text-align:left;"> disease </td> </tr> <tr> <td style="text-align:left;"> Sable Island, Nova Scotia </td> <td style="text-align:left;"> Dalhousie University </td> <td style="text-align:left;"> Grey seals </td> <td style="text-align:right;"> 43.9337 </td> <td style="text-align:right;"> -59.9149 </td> <td style="text-align:left;"> gps </td> </tr> <tr> <td style="text-align:left;"> Greifswald, Germany </td> <td style="text-align:left;"> Friedrich Loeffler Institut </td> <td style="text-align:left;"> Fox rabies </td> <td style="text-align:right;"> 54.0865 </td> <td style="text-align:right;"> 13.3923 </td> <td style="text-align:left;"> disease </td> </tr> <tr> <td style="text-align:left;"> Arusha, Tanzania </td> <td style="text-align:left;"> Nelson Mandela African Institute of Science and Technology </td> <td style="text-align:left;"> Teaching </td> <td style="text-align:right;"> -3.3995 </td> <td style="text-align:right;"> 36.7968 </td> <td style="text-align:left;"> training </td> </tr> <tr> <td style="text-align:left;"> Kigali, Rwanda </td> <td style="text-align:left;"> National Institute of Statistics Rwanda </td> <td style="text-align:left;"> Teaching </td> <td style="text-align:right;"> -1.9415 </td> <td style="text-align:right;"> 30.0574 </td> <td style="text-align:left;"> training </td> </tr> </tbody> </table> </div> --- layout: true # Leaflet map with points --- .left-code[ ```r leaflet(work) %>% addProviderTiles(providers$Stamen.Watercolor) %>% addProviderTiles(providers$Stamen.TerrainLabels) %>% addCircleMarkers(~lon, ~lat) ``` * **Your Turn** What else can you change about addCircleMarkers? Hint: Type `??addControl` Try adding: `clusterOptions = markerClusterOptions()` to your map. ] .right-plot[
] --- .left-code[ ```r leaflet(work) %>% addProviderTiles(providers$Stamen.Watercolor) %>% addProviderTiles(providers$Stamen.TerrainLabels) %>% addCircleMarkers(~lon, ~lat, clusterOptions = markerClusterOptions()) ``` * **Your Turn** What else can you change about addCircleMarkers? Hint: Type `??addControl` Try adding: `clusterOptions = markerClusterOptions()` to your map. ] .right-plot[
] --- * Add labels ```r labels <- sprintf( "<strong>%s</strong>", work$institute) %>% lapply(htmltools::HTML) ``` --- .left-code[ ```r leaflet(work) %>% addProviderTiles(providers$Stamen.Watercolor) %>% addProviderTiles(providers$Stamen.TerrainLabels) %>% addCircleMarkers(~lon, ~lat, popup = ~labels) ``` ] .right-plot[
] --- layout: true # Recap * Leaflet maps are built in layers using `%>%` * Every leaflet map starts with a base map which we can customize by setting the view, adding provider tiles. * We choose a geometric shape depending on our spatial data type (e.g. `addPolygons` or `addCircleMarkers`). * In choosing a color palette we need to think about our data types and our audience. * We can add interactivity to our plot using highlight and labels. --- --- layout: true # Choose your own adventure --- * **Option 1** Create a new map with the births from 1979. * **Option 2** Change around the different visual elements of the plots. * **Option 3** Create a new set of labels for the leaflet map with points. * **Option 4** Check out the short tutorial on joins. * **Option 5** Create a new variable that calculates how many births above or below the average counties are. Choose a palette that will be best to show this. --- layout: true # Useful resources: --- * [RStudio leaflet (Tutorial)](https://rstudio.github.io/leaflet/) * [Geocomputation with R (Book)](https://geocompr.robinlovelace.net/preface.html) * [Afrimapr: Intro to Spatial Data in R (Tutorial)](https://andysouth.shinyapps.io/intro-to-spatial-r/) * [Afrimapr: Getting Data into R (Tutorial) ](https://andysouth.shinyapps.io/get-my-data-in/) * [Afrimapr: Joining Spreadsheet Data (Tutorial) ](https://andysouth.shinyapps.io/get-my-data-in/) * [Rspatialdata.github.io](https://rspatialdata.github.io) * [Humanitarian Data Exchange](https://data.humdata.org) --- class: inverse, center, middle layout: true # Thanks! Slides created via the R package [xaringan](https://github.com/yihui/xaringan). The chakra comes from [remark.js](https://remarkjs.com), [knitr](http://yihui.org/knitr), and [R Markdown](https://rmarkdown.rstudio.com). .font150.text-white[ Special thanks to Rich Leyshon at the Data Science Campus and Kemunto Ongera at Bates College ] .font150.text-white[ Slides template adapted from Garrick Aden-Buie GitHub: <http://github.com/gadenbuie/gentle-ggplot2> ] --- class: inverse, center, middle layout: true # Additional Slides --- --- layout: true # Colorbins --- ```r summary(nc$births1979) ``` ``` ## Min. 1st Qu. Median Mean 3rd Qu. Max. ## 319 1336 2636 4224 4889 30757 ``` ```r bins <- seq(from = 0, to = 35000, by = 5000) ``` --- * Then we can define the colours for the palette: ```r pal79 <- colorBin("OrRd", domain = nc$births1979, bins = bins) ``` --- layout: true # Let's create a second map --- * Let's create a second map of births in 1979. * First we'll need to create a new set of labels ```r labels79 <- sprintf( "<strong>%s</strong><br/>%g births", nc$county, nc$births1974 ) %>% lapply(htmltools::HTML) ``` * **Your Turn** Using `names(nc)` to check the column names, change `nc$births1974` to the column which corresponds to number of births in 1979. --- layout: true # Let's create a second map --- ```r m2 <- leaflet(data = nc) %>% addProviderTiles(providers$Stamen.Terrain) %>% setView(lng = -80, lat = 34.5, zoom = 6) %>% addPolygons( fillColor = ~pal79(nc$births1979), fillOpacity = 0.7, color = "white", opacity = 1, weight = 2, highlight = highlightOptions( weight = 3, color = "blue", fillOpacity = 1, bringToFront = TRUE), label = labels79) ``` --- ```r m2 <- m2 %>% addLegend( position = "bottomright", pal = pal79, values = ~nc$births1979, title = "Births by country in 1979", opacity = 1) ``` --- ```r m2 ```
--- layout: true # Placing two maps side by side --- ```r leafsync::sync(m1, m2, ncol = 2, sync = "all") ```