---
title: "Technical Guideline Series"
output:
  pdf_document: default
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Part 4 - Guidelines for Colour
**Revised by Renske Gudde - Technical Support Unit (TSU) of Knowledge and Data**  
**Reviewed by Aidin Niamir - Head of the Technical Support Unit of Knowledge and Data and Rainer M. Krug - IPBES Task Force on Knowledge and Data**

*For any inquires please contact aidin.niamir@senckenberg.de*  

Version: 1.2  
Last Updated: 9 November 2023

DOI: [10.5281/zenodo.6838820](https://doi.org/10.5281/zenodo.6838820)     

This technical guidelines focuses on recommendations for the use of colour in maps and can be applied to other visualisations. We present some base considerations for colour paired with links to useful resources for further exploration to be used by all IPBES experts creating figures and maps.

Begin by loading the following packages:

```{r message = FALSE, warning=FALSE}
library(RColorBrewer)
library(ggplot2)
library(sf)
library(rnaturalearth)
```

### I. Overview of Pallets: 

#### *A Types of Pallets:* 

Colour is a critical key to communicating information to audiences. Often incorrect colour schemes are used that either make it difficult for people to understand the map or bias the interpretation. There are generally three types of palettes, qualitative, sequential, and diverging each corresponding to distinctive qualities of the data they represent.
  
Qualitative or categorical palettes have distinctive colours that are used to represent nominal or qualitative data with no presumed ordering, such as gender or cereal brands. Sequential palettes often represent continuous data such as temperature or height. These palettes are generally marked by even steps in value or intensity of a hue. Diverging palettes have two sets of sequential palettes meeting on a mutual colour which can represent continuous data with an obvious midpoint, such as data representing change from a baseline, or are often used to represent Likert scale survey responses.

#### *B. Colour Blind Friendly Palettes* 

When creating a map or figure, please choose colour schemes that are colour blind friendly (more information about [colour palettes and colour blindness](https://davidmathlogic.com/colorblind/#%23D81B60-%231E88E5-%23FFC107-%23004D40) or about [colour schemes and contrasts](https://personal.sron.nl/~pault/)). Printing a map or figure in black and white or grey tones are a good method to see how well the map or figure is interpretable without colour. Especially for maps, when areas without sufficient data are indicated, it is advisable to use a pattern instead of grey scales, because without the use of colour it can seem that there is a value for the data deficient areas.

There are various apps to simulate colour blindness:

1. [Sim Daldonism](https://michelf.ca/projects/sim-daltonism/), open source for **Mac and iOS**, to simulate various types of colour blindness
2. [Color Oracle](https://colororacle.org/#:~:text=Color%20Oracle%20is%20a%20free,color%20vision%20impairments%20will%20see), free available for **Windows, Linux and Mac**, focussing on the extreme forms of colour blindness (so that people with colour blindness that is less extreme or normal colour vision can also interpret the figures easily)
3. [ColourSimulations](https://apps.microsoft.com/detail/9NBLGGH4385H?hl=en-us&gl=US), real-time simulations of red-green colour-blindness for **Windows**.

There is also an R-package [colorBlindness](https://cran.r-project.org/web/packages/colorBlindness/index.html) ([with its own guide](https://cran.r-project.org/web/packages/colorBlindness/vignettes/colorBlindness.html)) which provides a collection of safe colours for various types of figures and maps.
 
#### *C. No rainbow colour palettes*  

Rainbow colour schemes are interpreted by humans to have sharp artificial boundaries that are not representative of the underlying data. [Crameri et al. 2020 covers](https://doi.org/10.1038/s41467-020-19160-7){target="_blank"} in more detail the current problems involving the use of colour in science communication. An example of this is presented in the figure below taken from [this article](https://personal.sron.nl/~pault/#){target="_blank"} where geoid height is displayed using a sunset scheme and then a traditional rainbow scheme. Large jumps in the data are interpreted within the lines of light blue and yellow that are not inherent within the data.

![Figure 2: Map illustrating the differences of interpretation of data displayed with a sunset palette scheme and the traditional rainbow palette.](colour_map_example.png)
  
 
### II. RColorBrewer in R and Examples

#### *A. RColorBrewer* 

In R, one can use the RColorBrewer package which provides convenient qualitative, sequential, and diverging palettes. 

To display all of the prepared palettes within the RColorBrewer package run this line of code: 
```{r, fig.height = 8, fig.align = 'center'}
RColorBrewer::display.brewer.all()
```
  
To display all of the colourblind friendly palettes run this:
```{r, fig.height = 7, fig.align = 'center'}
RColorBrewer::display.brewer.all(colorblindFriendly = TRUE)
```  

If you would like to visualise a specific palette, specify the number of colours and the palette name.   
```{r, fig.align = 'center'}
RColorBrewer::display.brewer.pal(n = 5, name = 'YlOrRd')
```

[This article](https://www.datanovia.com/en/blog/the-a-z-of-rcolorbrewer-palette/ ){target="_blank"} provides a great overview on how to use the RColorBrewer package with these palettes integrated with ggplot with examples. 


#### *B. Hexadecimal* 

R uses hexadecimal to represent colours. “Hexadecimal is a base-16 number system used to describe colour. Red, green, and blue are each represented by two characters (#rrggbb) that has 16 possible symbols.” - [R colour cheatsheet](https://www.nceas.ucsb.edu/sites/default/files/2020-04/colorPaletteCheatsheet.pdf){target="_blank"}

These values can be used to specify specific colours for a plot. To return the hexadecimal colour code of a palette, use this code: brewer.pal(n, name)

For example: 
```{r}
RColorBrewer::brewer.pal(n = 5, name = "YlOrRd")
```
We could then call those specific colours in a plot, as opposed to using general names like “blue” or pre-set palettes. 
  
  
#### *C. Examples* 

The next two examples showcase how to use the package RColorBrewer to set palletes within maps. We will use the populated places dataset from R natural earth website available in the first example [here for free download](https://www.naturalearthdata.com/downloads/110m-cultural-vectors/110m-populated-places/){target="_blank"}. These are just example datasets whose accuracy has not been checked.  

First run this code to download country data and the large cities dataset we will be working with. The code also projects both datasets to the Robinson Projection. 
```{r}
land <- ne_countries(scale = 110, returnclass = "sf") # Downloads land polygons 
cities <- read_sf('ne_110m_populated_places.shp') # Downloads the large cities points dataset

robin <- "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs" # Defines Projection 

# Converts to Robinson
land <- st_transform(land, crs = robin)
cities <- st_transform(cities, crs = robin)
```
  
**Example 1: Continuous Data**  
We will use population information contained in the large cities dataset, which is a continuous variable. The function scale_color_gradient() is used for the continuous data. The hex values were obtained from the brewer.pal function shown in the last section. 

```{r}
ggplot(cities) +
  geom_sf(data = land, # adds land for reference
          col = NA,
          fill = "gray60") +
  geom_sf(aes(color = POP_MIN/1000000)) + # Assigns the colour of the large cities to their minimum population size
  scale_color_gradient( # used with continuous data
    low = "#FFFFB2", 
    high = "#BD0026") +
  labs(color = "Population (millions)") +
  theme(legend.position = "bottom", # Places legend on the bottom 
        panel.background = element_blank(), # controls the background panel colour 
        panel.grid.major = element_line(color = "grey80")) # controls the grid line colour 
```
   
**Example 2: Categorical Data**  
We now will use the land dataset and function scale_fill_brewer() to set the colour of the continents according to one of the qualitative palettes included in the RColorBrewer package.

```{r}
ggplot(land) +
  geom_sf(aes(fill = continent, color = continent)) + # Fill and colour are the same to effective remove borders
  scale_color_brewer(palette = "Dark2") + # Uses a RColorBrewer palette
  scale_fill_brewer(palette = "Dark2") +
   theme(legend.position = "bottom", 
        legend.title = element_blank(),
         panel.background = element_blank(), 
        panel.grid.major = element_line(color = "grey80")) 
```
   

### III. Suggestions
* Use colour schemes and projections consistently throughout the chapter, if possible throughout the assessment.

* No data is symbolized with the colour grey (BBBBBB; RGB:187, 187, 187)

* White or light sky blue (87CEFA; RGB: 135, 206, 250) is used for the ocean in maps

* Don’t overload colour - avoid colour when not necessary - use it to highlight important information

* Grey normally represents NA data in a map, but can be very useful in other plots to focus your audience’s attention on key parts of your visualisation that have colour 

### IV. Additional Resources  
* R Colour Sheet: https://www.nceas.ucsb.edu/sites/default/files/2020-04/colorPaletteCheatsheet.pdf

* Useful tool to assist in picking palettes and colours: https://colorbrewer2.org/ 

* Tool to visualise colours as someone with different types of colour blindness: https://davidmathlogic.com/colorblind/#%23D81B60-%231E88E5-%23FFC107-%23004D40 

* colourBlindness Package guide: https://cran.r-project.org/web/packages/colorBlindness/vignettes/colorBlindness.html 

Your feedback on this content is welcome. Let us know what other useful material would you like to see here by emailing tsu.data@ipbes.net

