-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathggplot_manualColours.Rmd
69 lines (55 loc) · 2.09 KB
/
ggplot_manualColours.Rmd
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
---
title: "Manual colours in ggplot"
author: Ben Anderson ([email protected], `@dataknut`)
date: 'Last run at: `r Sys.time()`'
output:
html_document:
fig_caption: yes
keep_md: yes
number_sections: yes
toc: yes
bibliography: ~/bibliography.bib
---
# ggplot
http://ggplot2.tidyverse.org/reference/scale_manual.html - [@ggplot2]
```{r setup}
knitr::opts_chunk$set(echo = TRUE)
library(data.table) # not really needed but I like it :-)
library(ggplot2)
```
Go!
Sometimes we want to set and use specific colours. For example drought phases...
First set the colours:
```{r Set colours}
normalCol <- "lightskyblue" # Normal
develCol <- "yellow1" # Developing
droughtCol <- "orange" # Drought
sevDroughtCol <- "red" # Severe Drought
recoveringCol <- "yellowgreen" # Recovering
```
Now make a plot:
```{r Simple test on numeric data}
droughtPhaseDT <- as.data.table(rep(1:5))
droughtPhaseDT <- droughtPhaseDT[, phase := ifelse(V1 == 1,
"1. Normal",
NA)]
droughtPhaseDT <- droughtPhaseDT[, phase := ifelse(V1 == 2,
"2. Developing",
phase)]
droughtPhaseDT <- droughtPhaseDT[, phase := ifelse(V1 == 3,
"3. Drought",
phase)]
droughtPhaseDT <- droughtPhaseDT[, phase := ifelse(V1 == 4,
"4. Severe Drought",
phase)]
droughtPhaseDT <- droughtPhaseDT[, phase := ifelse(V1 == 5,
"5. Recovering",
phase)]
ggplot(droughtPhaseDT, aes(x = phase, y = V1, fill = phase)) +
geom_col() +
theme(legend.title = element_blank()) +
theme(legend.position = "bottom") +
scale_fill_manual(values = c(normalCol, develCol, droughtCol, sevDroughtCol, recoveringCol)
)
```
# References