Skip to content

Commit

Permalink
to address #16 using solution from rstudio/learnr#529 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
andysouth committed May 26, 2021
1 parent 2c866af commit d1784f6
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions inst/tutorials/intro-to-spatial-r/intro-to-spatial-r.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ library(sf) # working with vector data
library(afrilearndata) # example data
library(tmap) # static and interactive mapping
library(raster) # raster manipulation
#library(rgdal) # raster tiff reading
#library(dplyr) # data wrangling
#library(mapview) # interactive mapping
# to make local running the same as linux on shinyapps
# see https://github.com/rstudio/learnr/issues/529
#options(tutorial.exercise.evaluator = learnr:::forked_evaluator)
# 2021-05-26 but gave me local error : Warning: Error in : 'mcparallel' is not an exported object from 'namespace:parallel'
```


Expand Down Expand Up @@ -249,6 +255,13 @@ In `tmap` `tm_shape([object_name])` defines the data to be used. Then `+` to add

See the hint button for how to set colour with `tm_symbols(col = "blue")` and `col=[column_name]` to set the colour of each point according to the data value stored in a column.

```{r tmap-points1-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(1)
```

```{r tmap-points1, exercise = TRUE, exercise.eval = TRUE, message = FALSE, warning = FALSE}
tm_shape(africapitals) +
Expand All @@ -267,6 +280,13 @@ tm_shape(africapitals) +

The highway network (lines) can be plotted using the same `tm_shape([object_name])` to start, then adding `tm_lines()` to display the lines. The hints button below shows options for colouring lines.

```{r tmap-lines1-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(2)
```

```{r tmap-lines1, exercise = TRUE, exercise.eval = TRUE, message = FALSE, warning = FALSE}
tm_shape(afrihighway) +
Expand All @@ -286,6 +306,13 @@ tm_shape(afrihighway) + tm_lines(col = "Name") #use a column name from the objec

Countries (polygons) can similarly be mapped using `tm_shape` and `tm_polygons`. See the hint button for options for colouring countries.

```{r tmap-polygons-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(3)
```

```{r tmap-polygons1, exercise = TRUE, exercise.eval = TRUE, message = FALSE, warning = FALSE}
tm_shape(africountries) +
Expand All @@ -307,7 +334,14 @@ Gridded (raster) data can represent e.g. remotely sensed or modelled data.

It can be displayed with `tm_shape([object_name])` & `tm_raster`. Here we specify the `breaks` or cutoffs to the different colour bands.

In this example, if you use the default breaks by not specifying any arguments with `tm_raster()` (see the hint) the map looks entirely one colour. This is because there are few very high density cells and a majority of cells with very low values. This is a common issue with population data. The default (equal-interval) classification doesn't work well; most of the map falls in the lowest category. If you look very closely you can see a few very high value cells e.g. in Lagos & Cairo.
In this example, if you use the default breaks by not specifying any arguments with `tm_raster()` (see the hint) the map looks entirely one colour. This is because there are few very high density cells and a majority of cells with very low values. This is a common issue with population data. The default (equal-interval) classification doesn't work well; most of the map falls in the lowest category. If you look very closely you can see a few very high value cells e.g. in Lagos & Cairo.

```{r tmap-raster1-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(4)
```

```{r tmap-raster1, exercise = TRUE, exercise.eval = TRUE, message = FALSE, warning = FALSE}
Expand Down Expand Up @@ -341,6 +375,13 @@ Try to make maps :
2. without the raster population layer & with country boundaries that are visible
3. with text labels for ISO country codes

```{r tmap-vector-raster-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(5)
```

```{r tmap-vector-raster, exercise = TRUE, exercise.eval = TRUE, message = FALSE, warning = FALSE, exercise.setup='set-crs-and-tmap-mode'}
tmap::tm_shape(afripop2020) +
Expand All @@ -365,6 +406,13 @@ In `tmap` you can keep the identical code that we've looked at so far and just a

This is the identical code from the previous section but shown in view mode.

```{r tmap-interactive-setup}
# to avoid issues with interactive maps https://github.com/rstudio/learnr/issues/529
# IdSeed must be set to be unique in the Rmd, even for mode('plot') to allow user to change
#tmap_mode("plot")
htmlwidgets::setWidgetIdSeed(6)
```

```{r tmap-interactive, exercise = TRUE, exercise.eval = TRUE, message = FALSE, warning = FALSE, exercise.setup='set-crs-and-tmap-mode'}
tmap_mode('view')
Expand Down Expand Up @@ -392,7 +440,7 @@ So far we have been using data that already exists within R as an R object.

The same things can be done on data coming from a file.

Spatial data can be read into R using `sf::st_read()` for vector data (points, lines and polygons) and the `raster` package for gridded data.
Spatial data can be read into R using `sf::st_read()` for vector data (points, lines and polygons) and the `raster` & `rgdal` packages for gridded data.

We show examples below using files that are stored in the package. To use with your own data replace filename1 & 2. (Note that these specified file names can also be a URL for data provided on the web.)

Expand All @@ -404,6 +452,7 @@ filename1 <- system.file("extdata","africountries.shp", package="afrilearndata",
myobject1 <- sf::st_read(filename1)
library(raster)
library(rgdal)
filename2 <- system.file("extdata","afripop2020.tif", package="afrilearndata", mustWork=TRUE)
myobject2 <- raster::raster(filename2)
Expand Down

0 comments on commit d1784f6

Please sign in to comment.