-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbPalettes.Rmd
More file actions
95 lines (73 loc) · 4.07 KB
/
cbPalettes.Rmd
File metadata and controls
95 lines (73 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---
title: "Ben Anderson"
author: 'Colour Blind Palettes for Plots (Contact: [email protected], `@dataknut`)'
date: 'Last run at: `r Sys.time()`'
output:
bookdown::html_document2:
code_folding: hide
fig_caption: yes
number_sections: yes
self_contained: no
toc: yes
toc_depth: 2
toc_float: yes
html_document:
df_print: paged
toc: yes
toc_depth: '2'
bibliography: '`r path.expand("~/bibliography.bib")`'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
# colour blind palettes for charts
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
# with grey
cbgPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# with black
cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
```
# Colour Blind palettes
Obviously these aid interpretation of plots by those who are colour blind but the [palettes](http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette) are also useful clear defaults. Note that there are `r length(cbbPalette)` colours in each palette:
* palette with grey: `r cbgPalette`
* palette with black: `r cbbPalette`
Source: http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
This means that a plot with more than this number of factors will run out of colours (see below).
## Grey palette
This is a cross-reference to Figure \@ref(fig:plot1) which uses the grey palette.
```{r plot1, fig.cap="Scatter plot of mpg by displacement (grey palette)"}
df <- mtcars
df$auto <- ifelse(df$am == 0, "Automatic", "Manual") # set labels
p <- ggplot2::ggplot(df, aes(x = mpg, y = disp, colour = as.factor(carb))) +
guides(colour = guide_legend(title = "Number of carburetors:")) +
theme(legend.position="bottom") +
scale_colour_manual(values=cbgPalette) + # use colour-blind friendly grey palette
geom_point() # <- make the plot in an object first
p + labs(x = "Miles/(US) gallon", y = "Displacement (cu.in.)") + facet_grid(auto ~ .) # <- draw the plot and add more features
```
## Black palette
This is a cross-reference to Figure \@ref(fig:plot2) which uses the black palette.
```{r plot2, fig.cap="Scatter plot of mpg by displacement (black palette)"}
p <- ggplot2::ggplot(df, aes(x = mpg, y = disp, colour = as.factor(carb))) +
guides(colour = guide_legend(title = "Number of carburetors:")) +
theme(legend.position="bottom") +
scale_colour_manual(values=cbbPalette) + # use colour-blind friendly grey palette
geom_point() # <- make the plot in an object first
p + labs(x = "Miles/(US) gallon", y = "Displacement (cu.in.)") + facet_grid(auto ~ .) # <- draw the plot and add more features
```
# Using viridis
An alternative to the palettes is the [viridis](https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html) package [@viridis]. This has the benefit of not having a small number of colours although Figure \@ref(fig:plot3) doesn't really demonstrate this!
```{r plot3, fig.cap="Scatter plot of mpg by displacement (viridis palette)"}
library(viridis)
p <- ggplot2::ggplot(df, aes(x = mpg, y = disp, colour = carb)) +
guides(colour = guide_legend(title = "Number of carburetors:")) +
theme(legend.position="bottom") +
scale_colour_viridis() + # use viridis
geom_point() # <- make the plot in an object first
p + labs(x = "Miles/(US) gallon", y = "Displacement (cu.in.)") + facet_grid(auto ~ .) # <- draw the plot and add more features
```
# Summary
Personally I prefer the [colour blind palettes](http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette) if I have a small number of categories (less than `r length(cbbPalette)`) but usually use [viridis](https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html) if I have more than that and if I have continuous data.
# Ackowledgement
Uses ggplot2 [@ggplot2] and associated mtcars dataset to draw plots. Palettes available from http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
# References