Skip to content

Commit

Permalink
added another choro example, testing knitr options (dpi)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Dec 12, 2024
1 parent e531f92 commit 421e6a5
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 12 deletions.
2 changes: 1 addition & 1 deletion data-raw/World.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mn = which.min(col[,1])
x1 = unname(col[mn, 1])
x2 = unname(col[mn+1, 1])

col = rbind(cbind(X = 180, Y = -86), col[1:mn, ], cbind(X = c(-180, 180), Y = -86))
col = rbind(cbind(X = 180, Y = -90), col[1:mn, ], cbind(X = c(-180, 180), Y = -90))
colist[[8]] = col

colist2 = lapply(colist, function(x) {
Expand Down
Binary file modified data/World.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion vignettes/01_basics_vv.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ data(World, metro, rivers, land)



### About the data
## About the data

A spatial data object contained in tmap is called `World`. It is a data frame with a row for each country. The columns are the following data variables plus an additional geometry column which contains the geometries (see sf package):

Expand Down
88 changes: 88 additions & 0 deletions vignettes/11_examples_choro_World.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: "tmap example: choropleth (World)"
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.5)
```


## About the data

A spatial data object contained in tmap is called `World`. It is a data frame with a row for each country. The columns are the following data variables plus an additional geometry column which contains the geometries (see sf package):

```{r}
names(World)
```

We will create a choropleth of the Gender Inequality Index (GII) per country.

## The choropleth: step 1

```{r, fig.height = 5}
tm_shape(World) +
tm_polygons(fill = "gender")
```

## The choropleth: step 2

A few improvements:

* A suitable map projection. This one 'Equal Earth' is equal-area, i.e. the polygon area sizes are proportianal to the real-world country area sizes.
* A different color scheme. Run `cols4all::c4a_gui()` to explore them. For this application, we were looking for: a diverging, color-blind friendly palette with sufficient contrast with black (to see the border lines)
* A custom legend title
* The option `earth_boundary` is enabled, which shows the earth boundaries. Note that this feature is only available for certain map projections (for advanced users: families of pseudo-cylindrical and orthographic projections).

```{r, fig.height = 5}
tm_shape(World, crs = "+proj=eqearth") +
tm_polygons(
fill = "gender",
fill.scale = tm_scale_intervals(values = "-tableau.classic_orange_blue"),
fill.legend = tm_legend("Gender Inequality Index (GII)")) +
tm_options(earth_boundary = TRUE, frame = FALSE)
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "tmap example: choropleth"
title: "tmap example: choropleth (Netherlands)"
output:
bookdown::html_vignette2:
pkgdown:
Expand All @@ -15,8 +15,9 @@ editor_options:
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
fig.width=6,
fig.height=3,
out.width = "100%",
dpi = 300,
fig.width = 7.2916667,
comment = "#>"
)
hook_output <- knitr::knit_hooks$get("output")
Expand Down Expand Up @@ -50,7 +51,7 @@ tmap_options(scale = 0.5)
```


### About the data
## About the data

In tmap, there are two example data sets of the Netherlands:

Expand All @@ -60,7 +61,7 @@ In tmap, there are two example data sets of the Netherlands:

The first two datasets contain demographic data of the Netherlands as of 2022. The third dataset only contains metadata (names and codes) and will only be used to plot province borders.

```{r, out.width="100%"}
```{r, out.width="100%", fig.height = 6}
tmap_arrange(
qtm(NLD_dist, title = "NLD_dist: 3340 districts"),
qtm(NLD_muni, title = "NLD_mini: 345 municipalities"),
Expand All @@ -78,22 +79,55 @@ names(NLD_dist)

We will create a chroropleth about the variable `"edu_appl_sci"`, which contains the percentage of 15-75 year old people who have a university degree (Dutch: WO) or applied sciences degree (Dutch: HBO), per district as of 1st October 2022.

## The choropleth
## Choropleth: step 1

```{r, fig.height = 8, out.width="100%"}
```{r, fig.height = 8}
tm_shape(NLD_dist) +
tm_polygons(fill = "edu_appl_sci") # data variable -> fill color
```

## Choropleth: step 2

We remove the district borders, because they are too dense in urban areas. Instead, we add borders of municipalities and provinces op top:

```{r, fig.height = 8}
tm_shape(NLD_dist) +
tm_polygons(
fill = "edu_appl_sci",
col = NULL) + # disable border lines
tm_shape(NLD_muni) +
tm_borders(lwd = 1) + # municipality border line width 1
tm_shape(NLD_prov) +
tm_borders(lwd = 3) # province border line width 3
```

## Choropleth: step 3

A few improvements and embellishments:

* A different color scheme. Run `cols4all::c4a_gui()` to explore them. For this application, we were looking for: a diverging, color-blind friendly palette with sufficient contrast with black (to see the border lines)
* Legend: position top left, title removed (because we will use tm_title instead)
* Title added with tm_title. Note that `z = 0` makes sure the title is on top
* Map components added: compass, scalebar, and credits text

```{r, fig.height = 8}
tm_shape(NLD_dist) +
tm_polygons(
fill = "edu_appl_sci",
fill.scale = tm_scale_continuous(values = "-orange_blue_diverging"),
fill.legend = tm_legend("", position = tm_pos_in("left", "top")),
col = NULL # disable border lines
col = NULL
) +
tm_shape(NLD_muni) +
tm_borders(lwd = 1) +
tm_shape(NLD_prov) +
tm_borders(lwd = 3) +
tm_title("Proportion of Population Aged 15–75 with a University or Applied Sciences Degree by District (as of October 1, 2022)", width = 15, position = tm_pos_in("left", "top"), z = 0) +
tm_title("Proportion of Population Aged 15–75 with a University or Applied Sciences Degree by District (as of October 1, 2022)",
width = 15,
position = tm_pos_in("left", "top"),
z = 0) +
tm_compass(position = tm_pos_in("left", "bottom")) +
tm_scalebar(position = tm_pos_in("left", "bottom")) +
tm_credits("© Data: Statistics Netherlands, Software: R-tmap", position = tm_pos_in("left", "bottom"))
tm_credits("© Data: Statistics Netherlands, Software: R-tmap",
position = tm_pos_in("left", "bottom"))
```

1 comment on commit 421e6a5

@mtennekes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to mention: updated antarctica: now bbox stretches to lat = -90 (was -86)

Please sign in to comment.