-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
255 lines (212 loc) · 8.25 KB
/
README.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
dev = "ragg_png",
fig.path = "man/figures/README-",
fig.width = 12,
fig.height = 9,
out.width = "100%"
)
```
# Painbow
<!-- badges: start -->
<!-- badges: end -->
Painbow lets you use [XKCD's "painbow" colormap](https://xkcd.com/2537/) in ggplot.
[XKCD](https://xkcd.com/2537/) implied that this colormap is terrible, and even called it a "painbow". However, these examples show that with certain tasks and data, this colormap outperforms even some of the most commonly cited "good" colormaps like viridis.
![](https://imgs.xkcd.com/comics/painbow_award.png){width=48%}
Here's a reproduction in ggplot using some custom theming:
```{r echo=FALSE, message=FALSE, warning=FALSE, fig.height=4.6, fig.width=5.5, dpi=300, out.width="48%"}
library(tidyverse)
library(painbow)
library(patchwork) # combine multiple graphs
library(systemfonts) # register_font()
set.seed(8675309)
# download.file("http://simonsoftware.se/other/xkcd.ttf", dest="extra content/xkcd/xkcd.ttf", mode="wb")
register_font("xkcd", "extra content/xkcd/xkcd.ttf")
x_axis = tibble(x = (min(painbow_data$x)-11):max(painbow_data$x+5)) |>
mutate(y_original = rnorm(n(), -10, 2)) |>
(function(d) {modelr::add_predictions(d, loess(data=d, formula=y_original ~ x, span=.25), "y")})()
x_ticks = x_axis %>%
mutate(tick_length = ifelse((x+11) %% (14*5) == (14*5-1), 8, 4)) %>%
mutate(y2 = y + tick_length) %>%
filter((x+11) %% 14 == 13)
y_axis = tibble(y = (min(painbow_data$y)-11):max(painbow_data$y)) |>
mutate(x_original = rnorm(n(), -10, 2)) |>
(function(d) {modelr::add_predictions(d, loess(data=d, formula=x_original ~ y, span=.25), "x")})()
y_ticks = y_axis %>%
mutate(x2 = x + 4) %>%
filter((y+11) %% 14 == 13)
ggplot(painbow_data) +
aes(x, y) +
geom_path(data=x_axis, size=.75) +
geom_path(data=y_axis, size=.75) +
geom_segment(data=x_ticks, aes(xend=x, yend=y2), size=.75) +
geom_segment(data=y_ticks, aes(xend=x2, yend=y), size=.75) +
geom_raster(interpolate = TRUE, aes(fill = value)) +
scale_x_continuous(expand=c(.01,0)) +
scale_y_continuous(expand=c(.01,0)) +
scale_fill_painbow(breaks = seq(0, 120, 20), limits = c(0, 120)) +
coord_fixed() +
guides(fill = guide_colorbar(
barheight = 12, barwidth = 1.05, draw.ulim = TRUE, draw.llim = TRUE, frame.colour = "black", frame.linewidth = 2, label.hjust = -0.4, ticks = FALSE, label.theme = element_text(family="xkcd", size = 15))) +
labs(
title = expression(underline("FIGURE 2")),
x = expression(paste(theta, " (PHASE)")),
y = expression(lambda),
fill = "PEAK\nENERGY") +
theme_void(16) +
theme(
axis.title.x = element_text(),
axis.title.y = element_text(angle = 0, vjust = 0.5),
axis.text = element_blank(),
plot.margin = margin(10,10,10,10),
plot.background = element_rect(color="black", size=1.5),
text = element_text(family="xkcd", lineheight = .6)) +
patchwork::plot_annotation(caption = "EVERY YEAR, DISGRUNTLED SCIENTISTS COMPETE \nFOR THE PAINBOW AWARD FOR *BEST* COLOR SCALE.",
theme = theme(plot.margin = margin(0,0,8,0),
plot.caption = element_text(family="xkcd", size=19, hjust=0.5)))
```
## Installation
You can install the latest development version:
```
install.packages("devtools")
devtools::install_github("steveharoz/painbow")
```
## Examples
Setup:
```{r setup, warning = FALSE, message = FALSE}
library(tidyverse)
library(painbow)
library(patchwork) # combine multiple graphs
```
### A simple example for the scale:
```{r}
ggplot(faithfuld) +
aes(waiting, eruptions, fill = density) +
geom_raster(interpolate = TRUE) +
scale_fill_painbow() +
labs(title = "Can you find the most dense region?") +
theme_bw(18)
```
### The dataset from the comic
The dataset is `painbow_data`. It was made using the comic's image and a scripted lookup table.
```{r echo=FALSE}
myplot = ggplot(painbow_data) +
aes(x, y, fill = value) +
geom_raster(interpolate = TRUE) +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
coord_fixed() +
guides(fill = guide_colorbar(
barheight = 12, draw.ulim = TRUE, draw.llim = TRUE, frame.colour = "black", ticks = FALSE)) +
labs(
x = expression(paste(theta, " (PHASE)")),
y = expression(lambda),
fill = "PEAK\nENERGY") +
theme_classic(16) +
theme(axis.title.y = element_text(angle = 0, vjust = 0.5), axis.text = element_blank())
myplot + scale_fill_continuous(breaks = seq(0, 120, 20), limits = c(0, 120)) +
myplot + scale_fill_viridis_c(breaks = seq(0, 120, 20), limits = c(0, 120)) +
myplot + scale_fill_gradientn(breaks = seq(0, 120, 20), limits = c(0, 120), colors = rainbow(255)) +
myplot + scale_fill_painbow(breaks = seq(0, 120, 20), limits = c(0, 120))
```
### Painbow may help you find outliers
Here is a 2D field with a regular pattern and a deviation. Can you find it?
Painbow makes a task easier compared with commonly touted "good" colormaps.
```{r fig.height=4}
##### 2D #####
COUNT = 512
data = expand.grid(
x = 1:COUNT,
y = 1:COUNT) %>%
mutate(z = sin(x/16) + cos(y/16)) %>%
mutate(znoise = z + dnorm(sqrt((x-0.75*COUNT)^2 + (y-0.33*COUNT)^2)/COUNT*20))
ggplot(data) +
aes(x=x, y=y, fill=znoise) +
geom_raster() +
labs(title = "ggplot default", fill=NULL) +
theme_void(15) + theme(legend.text = element_blank()) +
ggplot(data) +
aes(x=x, y=y, fill=znoise) +
geom_raster() +
scale_fill_viridis_c() +
labs(title = "Viridis", fill=NULL) +
theme_void(15) + theme(legend.text = element_blank()) +
ggplot(data) +
aes(x=x, y=y, fill=znoise) +
geom_raster() +
scale_fill_painbow() +
labs(title = "XKCD's colormap", fill=NULL) +
theme_void(15) + theme(legend.text = element_blank()) +
patchwork::plot_annotation(
title = "Three colormaps. Same data. Can you spot the weird region?",
theme = theme(text = element_text(size=20)))
```
### Painbow can help you spot a subtle pattern among data with high dynamic range
```{r}
######## 1D #########
COUNT = 1024
data = tibble(
x = 1:COUNT,
y = x/COUNT + sin(x/4)/100
)
ggplot(data) +
aes(x = x, y=x) +
geom_line() +
labs(title = "y = x") +
theme_void(15) + theme(legend.text = element_blank()) +
ggplot(data) +
aes(x = x, y=COUNT/2, fill=x) +
geom_tile(width=1, height=COUNT, color=NA) +
labs(title = "ggplot default") +
theme_void(15) + theme(legend.text = element_blank()) +
ggplot(data) +
aes(x = x, y=COUNT/2, fill=x) +
geom_tile(width=1, height=COUNT, color=NA) +
scale_fill_viridis_c() +
labs(title = "viridis") +
theme_void(15) + theme(legend.text = element_blank()) +
ggplot(data) +
aes(x = x, y=COUNT/2, fill=x) +
geom_tile(width=1, height=COUNT, color=NA) +
scale_fill_painbow() +
labs(title = "painbow") +
theme_void(15) + theme(legend.text = element_blank()) +
ggplot(data) +
aes(x = x, y=y) +
geom_line() +
labs(title = "y = x + sine wave") +
theme_void(15) + theme(legend.text = element_blank()) +
ggplot(data) +
aes(x = x, y=COUNT/2, fill=y) +
geom_tile(width=1, height=COUNT, color=NA) +
labs(title = "ggplot default") +
theme_void(15) + theme(legend.text = element_blank()) +
ggplot(data) +
aes(x = x, y=COUNT/2, fill=y) +
geom_tile(width=1, height=COUNT, color=NA) +
scale_fill_viridis_c() +
labs(title = "viridis") +
theme_void(15) + theme(legend.text = element_blank()) +
ggplot(data) +
aes(x = x, y=COUNT/2, fill=y) +
geom_tile(width=1, height=COUNT, color=NA) +
scale_fill_painbow() +
labs(title = "painbow") +
theme_void(15) + theme(legend.text = element_blank()) +
patchwork::plot_layout(ncol=4) +
patchwork::plot_annotation(
title = "Three colormaps. Same data. Can you spot the harmonic?",
theme = theme(text = element_text(size=20)))
```
## Feedback, issues, and contributions
Feedback, suggestions, issues, and contributions are all welcome! Please file an issue or pull request at https://github.com/steveharoz/painbow/issues
## Citing Painbow
The XKCD comic deserves credit: https://xkcd.com/2537/
Please cite this library via:
Steve Haroz (`r format(Sys.Date(), "%Y")`). Painbow. R package version `r getNamespaceVersion("painbow")`, <https://github.com/steveharoz/painbow/>.