class: inverse, bottom, right <h1 align="center"> Spatial Analysis Using R </h1> <br> <br> <br> <br> <h3> Montevideo - June 2019 </h3> <hr width = 300, align = "right"> <p> <a href="https://orlando-sabogal.github.io/">Orlando Sabogal</a> - <a href="https://twitter.com/AntonioSabogal">@AntonioSabogal</a> - <a href="https://github.com/orlando-sabogal">Github</a> <p> ??? This is the first silde Introduce myself --- #What is special about spatial data? <br/> - The title of this slide was taken from [An old but good article](https://escholarship.org/uc/item/3ph5k0d4) by Luc Anselin. -- - Tobler's (1979) first law of geography. Have you heard of the second law? - What *near* and *distant* mean in a particular context? - Data-driven vs model-driven -- - What about independece and correlation among observations? -- - Computers and geography are in the same team. See [Geography and computers: Past, present, and future](https://onlinelibrary.wiley.com/doi/10.1111/gec3.12403) by Arribas-Bel and Reades - [Goegraphic Data Science article](https://onlinelibrary.wiley.com/doi/full/10.1111/gean.12194) by Singleton and Arriba-bel - There is much that geography can learn about data science. -- - Data is **NOT ENOUGH** to extract knowledge. --- class: inverse, center, middle #Visualization --- class: inverse, center, middle background-image: url(Minard.png) background-size: contain ??? Source: --- class: inverse, center, middle background-image: url(bike_ggplot_james-cheshire-768x508.png) background-size: contain ??? Source: https://www.r-graph-gallery.com/270-james-cheshire-dataart/ see: http://oobrien.com/wp-content/uploads/2012/09/mappinglondon_soc.pdf --- class: inverse, center, middle background-image: url(journey_to_work_web_james_cheshire-768x898.gif) background-size: contain ??? Source: https://www.r-graph-gallery.com/270-james-cheshire-dataart/ --- #Statistical models <br/> -- - Areal data: Observed variable on zones follows a random process. Spatial Autucorrelation. Modifable areal unit problem MAUP. -- - Point data:Location as a random process (crimes). Differnce between spatial distribution and completely random spatial distribution. Usually associated with hotspots and Kernel Denstiy Estimation KDE. -- - Continous data: *Geostatistics.* The variables is continously distributed across all the territory but there are limited discrete-fixed observations. Semivariogram, interpolation, Kriging. --- #Programming - Programming? Writting code? or just *scripting*? - Always use google. Even the most experimented programmers are all day in stackoverflow or github figuring out how to make things work. - I see programming as a superpower. - It can certainly foster your work. - Reproducible research, government accountability, citizen science. - **Empowerment** - R is a (powerful) tool. Be aware of other tools: *python*, *javascript*, *SQL.* --- #Content This course is about using data with geographic properties. Hopefully, **we will code a lot.** .pull-left[ ###You will learn - Some general concepts. - Key Libraries - To make reproducible and reusable maps - That *the sky is the limit* - Hopefully, **to learn R**. ] .pull-right[ ###You will not learn - Geographic Statistics and sound methods. - *raster* data. - To write efficient R code.<sup>*</sup> - To handle big data. - To solve every possible problem. ] .footnote[[*] This is up to you.] --- #libraries -- There are many libraries. In this course we are going to try to cover the basics apsects of: <br> </br> -- sf <br> </br> -- sf + *The Tidyverse* <br> </br> -- sf + *The Tidyverse* + notebooks <br> </br> -- tmap <br> </br> -- leaflet <br> </br> --- class: inverse, center, middle # sf: simple features --- #Spatial Operations -- - Read and Write Data ```r st_read() st_write() ``` -- - It works as a tibble with and additional *geometry* variable. You can use almost the whole **Tidyverse** ```r SF_Object %>% ...functions()... filter() arrange() group_by() summarise() ... ``` --- ###Some Useful Functions ```r plot(st_geometry()) st_join() st_union() st_difference() st_buffer() ... ``` -- ###CRS: Coordinate Reference System - *geographic* (distance is not in meters) - *projected* (distance is in meters) -- ```r st_crs() st_is_longlat() st_set_crs() st_transform() ``` -- Go to the [sf Tutorial!](https://orlando-sabogal.github.io/SpatialAnalysis-MontevideoWorkshop2019/Notebooks/WarmUp.nb.html) --- class: inverse, center, middle #tmap ##(I love it) --- #A grammar for plots **tmap** works in a similar way to **ggplot2**. <br/> Is simple to use. Think about having an empty canvas where you add elements by using **+ tm_SomeFuncion.** ```r tm_shape() + tm_polygons() + tm_shape() + tm_dots() + tm_compass() + tm_scale_bar() ``` ###Some capabilities: - Auxiliar maps (insets) - Interactiviy, connection to Leaflet - Full customization of legends - Faceted maps -- Go to the [tmap Tutorial!](https://orlando-sabogal.github.io/SpatialAnalysis-MontevideoWorkshop2019/Notebooks/tmap-tutorial.nb.html) --- class: inverse, center, middle #leaflet --- #Leaflet and Leaflet for R Originallyl, Leaflet is a javascript [library](https://leafletjs.com/) "...designed with simplicity, performance and usability in mind." Fortunately, in the [html widgets for R](http://www.htmlwidgets.org/) there is a [Leaflet for R](https://rstudio.github.io/leaflet/) that let you use most of the capabilities of Leaflet with your regular R code. To create maps with leaflet you can use *data.frame* objects, *sf* objects, *raster* objects and *json* files. Leaflet for R is methodologically easy to use and you can get farther by exploring the arguments of the functions. A nice feature of Leaflet is that is integrated with shiny. ```r leaflet() %>% addProviderTiles() %>% addPolygons() %>% addCircles() %>% ... ``` --- #Some examples - [The propensity to cycling tool PCT](http://www.pct.bike/m/?r=avon) - [PCT website](https://www.pct.bike/) - [PCT Github repositories!](https://github.com/npct) - What about a similar tool for Uruguay? - [Reimagining NYC Neighborhoods with NewerHoods](https://data-clinic.shinyapps.io/newerhoods/) [Repo](https://github.com/tsdataclinic/newerhoods) - [Isolines](https://byollin.shinyapps.io/Isolines/). [Repo](https://github.com/byollin/Isolines) - [Water Quality](https://udwq.shinyapps.io/lakeprofiledashboard/) - [SpatialEpiApp](https://paulamoraga.shinyapps.io/spatialepiapp/) - Is actually an [R package](https://paula-moraga.github.io/blog/2018/01/04/2018-01-04_spatialepiapp/) too. <br/> -- Go to the [leaflet Tutorial!](https://orlando-sabogal.github.io/SpatialAnalysis-MontevideoWorkshop2019/Notebooks/InteractiveMaps.nb.html) --- class: inverse, center, middle #The sky is the limit! --- <br/> ###There are many **free** and **open** resources that you can use: - [Geocomputation with R](https://geocompr.robinlovelace.net/) - [Spation-Temporal Statistics With R](https://spacetimewithr.org/) - [Spatial Data Science](https://keen-swartz-3146c4.netlify.com/) - [Predictive Soil Mapping With R](https://soilmapper.org/) - [Spatial Data Science](https://rspatial.org/index.html) - [Intro to Gis and Spatial Analysis](https://mgimond.github.io/Spatial/index.html) - [Workshop on R and Movement Ecology](https://www.danaseidel.com/MovEco-R-Workshop/) - [Geoinformatics with R](http://rgeo.tbuckl.com/) --- class: center, middle # 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.name/knitr), and [R Markdown](https://rmarkdown.rstudio.com).