-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcolors.qmd
More file actions
79 lines (54 loc) · 2.87 KB
/
colors.qmd
File metadata and controls
79 lines (54 loc) · 2.87 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
# Colors
When evaluating the power and efficiency of a plot, color is always a key factor that sometimes speaks a language even louder than words. So in this chapter, you will be introduced with several widely-applied color schemes and get to know how to use proper colors to make better plots based on different features of your data.
## RColorBrewer
``RColorBrewer`` is an R package having built-in sensible color schemes ready-to-use for figures. Colors are grouped into three types: sequential, diverging, and qualitative.
>Sequential – Light colours for low data, dark for high data
>
>Qualitative(for categorical data) – Colours designed to give maximum visual difference between categories so great for non-ordered categorical data
>
>Diverging – Light colours for mid-range data, low and high use dark colours, great to seperate two extremes
>
```{r,fig.width=9.6, fig.height=7.2}
library(RColorBrewer)
display.brewer.all()
```
Here is an example of plotting categorical data using ``Dark2`` pallets under qualitative group of RColorBrewer:
```{r,fig.width=6, fig.height=4}
library(ggplot2)
ggplot(iris, aes(Petal.Length, Sepal.Length, colour = Species)) +
geom_point() +
scale_colour_brewer(palette = "Dark2")
```
Also, you can create your own sequential pallets.
```{r,fig.width=6, fig.height=4}
ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_raster() +
scale_fill_gradient(low = "white", high = "red")
```
Or diverging pallets:
```{r,fig.width=6, fig.height=4}
ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_raster() +
scale_fill_gradient2(low = "grey", mid = "white", high = "red",midpoint = .02)
```
For discrete data, using ``scale_colour_manual`` is a good choice. For discrete ordinal data, we can use another package (such as vcd)
```{r,fig.width=4, fig.height=3}
ggplot(mtcars, aes(mpg, wt)) +
geom_point(aes(colour = factor(cyl))) +
scale_colour_manual(values = c("red", "yellow", "blue"))
colors<-brewer.pal(5,'Blues')
barplot(1:5, col=colors)
```
## Perceptually uniform color spaces: Viridis
The ``viridis`` R package provides four palettes for use in R which are pretty, perceptually uniform and easy to read by those with colorblindness.
The package contains eight color scales: ``viridis``, the primary choice, and five alternatives with similar properties - ``magma``, ``plasma``, ``inferno``, ``civids``, ``mako``, and ``rocket`` -, and a rainbow color map - ``turbo``.
<center>
{width=75%}
</center>
Perceived differences are proportional to scalar differences when using ``viridis``. The following example shows ``viridis``on continuous data using ``scale_color_viridis_c``, use ``scale_color_viridis_d()`` for discrete data
```{r,fig.width=4, fig.height=3}
library("viridis")
ggplot(iris, aes(Sepal.Length, Sepal.Width))+
geom_point(aes(color = Sepal.Length)) +
scale_color_viridis_c()
```