Skip to content

Commit

Permalink
Improve text slides 20241128
Browse files Browse the repository at this point in the history
  • Loading branch information
damianooldoni committed Nov 27, 2024
1 parent 6b7fb61 commit 7fdd9c0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 50 deletions.
Binary file modified docs/assets/images/20241128/20241128_giscoR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 59 additions & 50 deletions docs/sessions/20241128_visualize_spatial_data_in_r.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@

### Spatial vector data

Points, lines, polygons, etc. Topic of the coding club of [28 May](https://inbo.github.io/coding-club/sessions/20240528_spatial_data_in_r_with_sf.html#1)
Points, lines, polygons, etc were the topic of the coding club of [28 May 2024](https://inbo.github.io/coding-club/sessions/20240528_spatial_data_in_r_with_sf.html#1).

- **sf** : newest and recommended package
- **sp** : its predecessor (retired)

### Spatial raster data

Topic of the coding club of [24 November 2022](https://inbo.github.io/coding-club/sessions/20221124_coding_club_rasters.html)
Rasters were the topic of the coding club of [24 November 2022](https://inbo.github.io/coding-club/sessions/20221124_coding_club_rasters.html).

- [**terra**](https://rspatial.github.io/terra/reference/terra-package.html) : newest and recommended package
- [**raster**](https://rspatial.org/raster/pkg/index.html) : its predecessor (no support for sf), match with sp
Expand All @@ -71,7 +71,9 @@
---
class: left, top

Today we play around the visualization of spatial vector data. No raster data!
# Today

Today we play around the visualization of **spatial vector data** only. No raster data!


---
Expand Down Expand Up @@ -104,17 +106,20 @@

You have a lot of tiles. Some are available for being used with an open license, others not.

There are tiles covering the entire world, or just a specific region (= Flanders, see the [Web Map (Tile) Service](https://www.vlaanderen.be/digitaal-vlaanderen/onze-oplossingen/geografische-webdiensten/ons-gis-aanbod/raadpleegdiensten#wat-is-een-wmts) of the Flemish Authority)
Tiles are nothing more than images (pngs or jpgs). They are small images that are put together to form a map. The tiles are usually 256x256 pixels and are arranged in a grid. The tiles are generated by a server and are requested by the client (your browser) when you zoom in or out on a map.

There are tiles covering the entire world, or just a specific region. For Flanders, see the [Web Map (Tile) Service](https://www.vlaanderen.be/digitaal-vlaanderen/onze-oplossingen/geografische-webdiensten/ons-gis-aanbod/raadpleegdiensten#wat-is-een-wmts) (WMTS) of the Flemish Authority.

So, if you have ever seen a map on the web, you have seen tiles! And if you have ever heard a know-it-all speaking about WMTS, well, do not be afraid about that!


* Image from [README](https://github.com/riatelab/maptiles#readme) of maptiles package
![:scale 80%]({{ site.baseurl}}/assets/images/20241128/20241128_tiles.png)

---
class: left, top

# Static vs dynamic maps

**Static maps**: data on the map and the tiles of the background are fixed: no zooming in/out. Easy for pdfs or other static content.
**Static maps**: data visualized on the map and the background tiles are fixed: no zooming in/out. Easy for pdfs or other static content.

**dynamic maps**: zooming in/out is possible. Perfect for web pages.

Expand All @@ -125,50 +130,50 @@

Geospatial visualization in R is evolving fast. The packages are numerous and the choice is not always easy. The [R-Spatial CRAN Task View](https://github.com/cran-task-views/Spatial/tree/main)* is a handy source to get an updated overview of R spatial packages. See section related to [visualization](https://github.com/cran-task-views/Spatial/blob/main/Spatial.md#visualizing-spatial-data).

\* Thanks Floris for the tip!
Another tip: choose packages actively maintained!

<small>* __Note__: the CRAN Task View is a curated list of R packages for a particular task. It is maintained by the community. And thanks Floris Vanderhaeghe for the tip!</small>

---
class: left, top

# Packages used today

There are a lot of packages. Really a lot! What should we use? Choose preferably the actively maintained packages.

Today we will mainly use:

- ggplot: allows to plot geospatial vector data. No maps!
- [maptiles](https://github.com/riatelab/maptiles): the best way to add tiles to your plots. A lot of tiles are freely available for the entire world. Option to use also specific endpoints (tile repositories on the web)
- [ggspatial]():
- [mapview](https://r-spatial.github.io/mapview/): the package for creating interactive maps very quickly and conveniently. Fill the gap of quick (not presentation grade) interactive plotting to examine and visually investigate both aspects of spatial data. It uses [Leaflet](http://leafletjs.com/) or [Mapbox GL](https://docs.mapbox.com/mapbox-gl-js/api/)+ [Deck.gl](https://deck.gl/) javascript libraries in the background via [mapdeck](https://symbolixau.github.io/mapdeck/articles/mapdeck.html). Your first choice before moving to leaflet.
- leaflet: a R wrapper for the very popular [Leaflet JavaScript library](http://leafletjs.com/) by [Vladamir Agafonkin](http://agafonkin.com/en/). A very good package for fine tuning dynamic maps. A little knowledge of html is needed for labels and pop ups.
- leaflet: a R wrapper for the very popular [Leaflet JavaScript library](http://leafletjs.com/) by [Vladamir Agafonkin](http://agafonkin.com/en/). More verbose than mapview. A very good package for fine tuning dynamic maps. A little knowledge of html is needed for labels and pop ups.

---
class: left, top

Install the packages:
Install and load the packages:

```r
install.packages(
c("sf", "terra", "maptiles", "leaflet", "leafem", "mapview",
"htmltools", "htmlwidgets", "crosstalk", "DT"
)
# Load the packages and install if needed
required_packages <- c(
"sf", # The standard package for working with spatial vector data
"ggplot2",
"ggrepel", # For avoiding label overlapping
"ggspatial",
"prettymapr", # For using map tiles with ggspatial
"raster", # For using map tiles with ggspatial
"mapview",
"leafem", # For providing extensions to leaflet maps
"leafpop", # For including tables, images and graphs in leaflet pop-ups
"inbospatial" # For sharing useful R functions dealing with spatial data
)
```

Load the packages:
# Install packages not yet installed
installed_packages <- required_packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
install.packages(required_packages[!installed_packages])
}

```r
library(tidyverse)
library(sf)
library(terra)
library(maptiles)
library(mapview)
library(leaflet)
library(htmltools)
library(htmlwidgets)
library(leafem)
library(crosstalk)
library(DT)
```
# Packages loading
invisible(lapply(required_packages, library, character.only = TRUE))

---
class: center, top
Expand Down Expand Up @@ -238,7 +243,6 @@

**NOTE**: The code to read the spatial files is provided!


---
background-image: url({{ site.baseurl}}/assets/images/background_challenge_2.png)
class: left, top
Expand All @@ -248,28 +252,19 @@
Let'use [ggspatial](https://github.com/paleolimbot/ggspatial) to create static maps.

1. Let's add now map tiles from OpenStreetMap (default) as background. Is the resolution of the automaticaly chosen zoom level not enough? Increase it, but still do not hardcode the zoom level. Hint: Type `?annotation_map_tile` in the R console to get help.
2. Add cahing option to avoid downloading the tiles each time you run the code.
3. Add a north arrow at the top right corner of the map and a scale bar at the bottom left corner of the map. Hint: see [dedicated section]((https://paleolimbot.github.io/ggspatial/articles/ggspatial.html#using-north-arrow-and-scalebar)) in ggspatial article.
4. Which other tiles you can use with `annotation_map_tile()`? Test one or more and let's find the favourite ones from the group.

2. Add caching option to avoid downloading the tiles each time you run the code.
3. Add a north arrow at the top right corner of the map and a scale bar at the bottom left corner of the map. Hint: see [dedicated section](https://paleolimbot.github.io/ggspatial/articles/ggspatial.html#using-north-arrow-and-scalebar) in ggspatial article.
4. Which other tiles you can use with `annotation_map_tile()`? Test them and let's find your favourite ones.

---
class: left, top

## Intermezzo: leaflet vs mapview

[Leaflet](https://leafletjs.com/) is a very popular JavaScript library for interactive maps. The R package [leaflet](https://rstudio.github.io/leaflet/) is a wrapper for that library. It allows to create interactive maps that can be embedded in a web page. The package is very flexible and allows to create maps with a lot of functionalities. The documentation is very good and there are a lot of examples available (check the "Articles"!).

However, if you are looking for a quick and easy way to create interactive maps, you can use [mapview](https://r-spatial.github.io/mapview/). It is a wrapper for leaflet and allows to create interactive maps with just one line of code. It is very useful to quickly explore your data and to create maps for presentations or reports. The documentation is also very good and there are a lot of examples available in the "Articles".

---
class: left, top

## Intermezzo: INBO goes spatial

Do you know INBO is very active in spatial data? Just think about qgisprocess package (see coding club of Dec 2023).But check also the R package [inbospatial](https://github.com/inbo/inbospatial): a collection of useful R functions for spatial data. Version 0.0.3 has been released just few days ago. This package will be helpful for the next challenge!

![:scale 80%]({{ site.baseurl}}/assets/images/20241128/20241128_inbospatial_0_0_3.png)

---
background-image: url({{ site.baseurl}}/assets/images/background_challenge_3.png)
class: left, top
Expand All @@ -287,9 +282,23 @@
No legend. Set line width of the polygons to 1 and set opacity of the polygons to 0.
4. Combine the two maps. Make sure the hover text still shows the number of
alien species.
5. You have already installed and loaded [leafem](https://r-spatial.github.io/leafem/), a package with leaflet extensions for mapview, so you should see by default the mouse coordinates (lat, lon) and zoom level on top left. If not, check how to add them in the [extra leaflet functionalities](https://r-spatial.github.io/mapview/articles/mapview_06-add.html) vignette. Add also the INBO coding club logo to the map with a link to the INBO coding club homepage: https://inbo.github.io/coding-club/. Link to the logo is provided in the R code. You don't need to download it!
5. You have already installed and loaded [leafem](https://r-spatial.github.io/leafem/), a package with leaflet extensions for mapview, so you should see by default the mouse coordinates (lat, lon) and zoom level on top left. If not, check how to add them in the [extra leaflet functionalities](https://r-spatial.github.io/mapview/articles/mapview_06-add.html) vignette.


---
class: left, top

## Intermezzo: INBO goes spatial

Do you know INBO is very active in spatial data? Just think about qgisprocess package (see coding club of Dec 2023).But check also the R package [inbospatial](https://github.com/inbo/inbospatial): a collection of useful R functions for spatial data. Version 0.0.3 has been released just few days ago. This package will be helpful for the next challenge!

![:scale 80%]({{ site.baseurl}}/assets/images/20241128/20241128_inbospatial_0_0_3.png)


---
class: left, top

# Challenge 3B - inbospatial
# Bonus challenge - inbospatial

Amog other things, inbospatial provides a lot of layers!

Expand Down Expand Up @@ -350,7 +359,7 @@
- [leaflet](https://rstudio.github.io/leaflet/) for R documentation.
- [leafem](https://r-spatial.github.io/leafem/) documentation.
- [leafpop](https://rdrr.io/cran/leafpop/) documentation.
- INBO's R package: [inbospatial](https://github.com/inbo/inbospatial): a collection of useful R functions for spatial data.
- [inbospatial](https://github.com/inbo/inbospatial): a collection of useful R functions for spatial data made in INBO.
- [tmap](https://r-tmap.github.io/tmap/) documentation.
- [giscoR](https://ropengov.github.io/giscoR/) documentation.

Expand All @@ -360,7 +369,7 @@
![:scale 30%]({{ site.baseurl}}/assets/images/coding_club_logo_1.png)

<!-- Adjust the room number and name -->
Topic: RepoRts - how to combine code and text in R <br>
Topic: RepoRts <br>
Room: HT - 01.69 - Paul Janssen <br>
Date: **17/12/2024**, van **10:00** tot **12:00** <br>
Help needed with packages or setup? You are welcom from **9:45**

0 comments on commit 7fdd9c0

Please sign in to comment.