Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Jan 27, 2025
1 parent 429cd5a commit 282d5a8
Showing 1 changed file with 154 additions and 0 deletions.
154 changes: 154 additions & 0 deletions vignettes/examples_topo_Africa.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
---
title: "tmap example: topographic map (Africa)"
output:
bookdown::html_vignette2:
pkgdown:
as_is: true
template:
math-rendering: mathjax
bibliography: '`r system.file("tmap.bib", package="tmap")`'
csl: "`r system.file('ieee.csl', package = 'tmap')`"
editor_options:
chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
out.width = "100%",
dpi = 300,
fig.width = 7.2916667,
comment = "#>"
)
hook_output <- knitr::knit_hooks$get("output")
knitr::knit_hooks$set(output = function(x, options) {
lines <- options$output.lines
if (is.null(lines)) {
return(hook_output(x, options)) # pass to default hook
}
x <- unlist(strsplit(x, "\n"))
more <- "..."
if (length(lines)==1) { # first n lines
if (length(x) > lines) {
# truncate the output, but add ....
x <- c(head(x, lines), more)
}
} else {
x <- c(more, x[lines], more)
}
# paste these lines together
x <- paste(c(x, ""), collapse = "\n")
hook_output(x, options)
})
```

```{r, message = FALSE}
library(tmap)
library(dplyr)
library(sf)
tmap_options(scale = 0.75)
```


## About the data

We use several spatial data object that are contained in tmap. The first is called `World`, which we only use for country borders. We'll only select the African countries:

```{r}
metroAfrica = sf::st_intersection(metro, World[World$continent == "Africa", ])
Africa = World[World$continent == "Africa", ]
```

The second is `land` which is a `stars` object that contains land cover. The third object is `metro` an `sf` object with metropolitan areas. The fourth and final ovject is `World_rivers`.

## Topographic map

```{r, fig.height = 6}
tm_shape(land) +
tm_raster("cover_cls",
col.legend = tm_legend("Land use")) +
tm_shape(World_rivers) +
tm_lines(
lwd = "strokelwd",
lwd.scale = tm_scale_asis(values.scale = .5),
col = "#A6CEE3") +
tm_shape(Africa, is.main = TRUE) +
tm_borders() +
tm_shape(metroAfrica) +
tm_symbols(fill = "red", shape = "pop2020", size = "pop2020",
size.scale = tm_scale_intervals(
breaks = c(1, 2, 5, 10, 15, 20, 25) * 1e6,
values.range = c(0.2,2)
),
size.legend = tm_legend("Population in 2020"),
shape.scale = tm_scale_intervals(
breaks = c(1, 2, 5, 10, 15, 20, 25) * 1e6,
values = c(21, 23, 22, 21, 23, 22)
),
shape.legend = tm_legend_combine("size")) +
tm_labels("name") +
tm_credits("United Nations, Department of Economic and Social Affairs, Population Division (2014). World Urbanization Prospects.\nProduction of Global Land Cover Data - GLCNMO2008.", position = tm_pos_out("center", "bottom"))
```

## Breaking down in layers

If this code chunk is overwhelming, let's that a look at the layer groups:

### Land use layer

```{r, fig.height = 3.5}
tm =
tm_shape(land) +
tm_raster("cover_cls",
col.legend = tm_legend("Land use"))
tm
```


### Adding rivers

```{r, fig.height = 3.5}
tm = tm +
tm_shape(World_rivers) +
tm_lines(
lwd = "strokelwd",
lwd.scale = tm_scale_asis(values.scale = .5),
col = "#A6CEE3")
tm
```

### African countries

Note that the first two layers are from a global data set (so not just Africa).
Note that in the final plot this raster is cropped to the bounding box of Africa.
We'll use `is.main` for the the spatial object `Africa` to do the actual cropping.

```{r, fig.height = 6}
tm = tm +
tm_shape(Africa, is.main = TRUE) +
tm_borders()
tm
```

### African cities

```{r, fig.height = 6}
tm = tm +
tm_shape(metroAfrica) +
tm_symbols(fill = "red", shape = "pop2020", size = "pop2020",
size.scale = tm_scale_intervals(
breaks = c(1, 2, 5, 10, 15, 20, 25) * 1e6,
values.range = c(0.2,2)
),
size.legend = tm_legend("Population in 2020"),
shape.scale = tm_scale_intervals(
breaks = c(1, 2, 5, 10, 15, 20, 25) * 1e6,
values = c(21, 23, 22, 21, 23, 22)
),
shape.legend = tm_legend_combine("size")) +
tm_labels("name")
tm
```

See the [vigette about scale](https://r-tmap.github.io/tmap/articles/basics_scales) and [about legends](https://r-tmap.github.io/tmap/articles/basics_legends).

0 comments on commit 282d5a8

Please sign in to comment.