class: center, middle, inverse, title-slide .title[ # Spatial Data Visualization with R ] .author[ ### Dr Thiyanga S. Talagala ] .author[ ### ] --- ## What geometries do you see on the map? <img src="https://bookdown.org/lexcomber/brunsdoncomber2e/bookdown-demo_files/figure-html/Op2-1.png" width="80%" style="display: block; margin: auto;" /> --- # Geospatial Vector Data 1. Points: single (x, y) point 2. Lines: two or more connected (x, y) points 3. Polygon: three or more (x, y) points connected and closed --- ## Globe to Plane What do we want to plot? Globe - 3D Space Where do we plot? Computer screen or paper - 2D Space To define the location of objects on the Earth, which is round, we need a coordinate system that adapts to the Earth’s shape. --- # Coordinate Reference System (CRS) - the way in which spatial data that represent the earth’s surface (which is round / 3 dimensional) are flattened so that you can “Draw” them on a 2-dimensional surface. --- ## Components of CRS Coordinate system: X, Y grid to define a point located in space Horizontal and vertical units --- ## Why multiple CRS? To optimize to best represent the shape and/or scale / distance and/or area --- ### Maps of the United States in Different CRS <img src="https://thiyanga-spatiotemporal.netlify.app/posts/time_series/us.jpeg" width="70%" style="display: block; margin: auto;" /> Source: https://www.earthdatascience.org/ --- ### Human head projections using different CRS .pull-left[ <img src="https://thiyanga-spatiotemporal.netlify.app/posts/time_series/head.jpeg" width="90%" style="display: block; margin: auto;" /> ] .pull-right[ Source: https://www.earthdatascience.org/ ] --- # Shapefile - Vector data storage format for storing the location, shape, and attributes of geographic features - A shapefile is made up of three required files with the same prefix ('spatial-data' in this case) but different extensions: - spatial-data.shp: main file that stores records of each shape geometries - spatial-data.shx: index of how the geometries in the main file relate to one-another - spatial-data.dbf: attributes of each record --- ### CRS vs Shapefile - Shapefiles contain geographic vector data that is expressed in a particular CRS --- ## Map types .pull-left[ <img src="https://open.lib.umn.edu/app/uploads/sites/178/2017/07/Image076.jpg" width="90%" style="display: block; margin: auto;" /> ] .pull-right[ source: https://open.lib.umn.edu/mapping/chapter/5-simplification/ ] --- ## Sri Lanka ```r # install.packages("devtools") devtools::install_github("thiyangt/ceylon") ``` ```r library(ceylon) library(tidyverse) library(sp) library(viridis) ``` --- ```r data(sf_sl_0) ggplot(sf_sl_0) + geom_sf() ``` <img src="lecture7_files/figure-html/unnamed-chunk-6-1.png" width="100%" /> More information: https://github.com/thiyangt/ceylon --- # ozmaps package ```r library(sf) library(ozmaps) library(kableExtra) library(knitr) sf_oz <- ozmap_data("country") sf_oz %>% kable() ``` <table> <thead> <tr> <th style="text-align:left;"> NAME </th> <th style="text-align:left;"> geometry </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Australia </td> <td style="text-align:left;"> MULTIPOLYGON (((144.8691 -4... </td> </tr> </tbody> </table> --- ```r ggplot(sf_oz) + geom_sf() ``` <img src="lecture7_files/figure-html/unnamed-chunk-8-1.png" width="100%" /> --- ![](mapcrs.png) --- ## Tutorial: Visualizing spatial data https://tstdataviz.netlify.app/slides/spatiotemporal.html