-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathREADME.Rmd
338 lines (224 loc) · 7.57 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
---
output: github_document
---
[![R build status](https://github.com/SymbolixAU/colourvalues/workflows/R-CMD-check/badge.svg)](https://github.com/SymbolixAU/colourvalues/actions)
[![Codecov test coverage](https://codecov.io/gh/symbolixau/colourvalues/branch/master/graph/badge.svg)](https://app.codecov.io/gh/symbolixau/colourvalues?branch=master)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/colourvalues)](https://CRAN.R-project.org/package=colourvalues)
![downloads](http://cranlogs.r-pkg.org/badges/grand-total/colourvalues)
[![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/colourvalues)](https://CRAN.R-project.org/package=colourvalues)
[![Github Stars](https://img.shields.io/github/stars/SymbolixAU/colourvalues.svg?style=social&label=Github)](https://github.com/SymbolixAU/colourvalues)
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#",
fig.path = "man/figures/README-",
out.width = "100%",
out.height = 200
)
library(colourvalues)
```
# colourvalues
```{r, echo = FALSE}
bar_plot <- function(df) {
barplot( height = df[["a"]], col = df[["col"]], border = NA, space = 0, yaxt = 'n')
}
df <- data.frame(a = 10, x = 1:256)
df$col <- colour_values(df$x, palette = "viridis")
bar_plot( df )
```
---
### What does it do?
It maps viridis colours (by default) to values, and quickly!
**Note** It does not perform a 1-to-1 mapping of a palette to values. It interpolates the colours from a given palette.
### Why did you build it?
I'm aware there are other methods for mapping colours to values. And which do it quick too. But I can never remember them, and I find the interfaces a bit cumbersome. For example, `scales::col_numeric(palette = viridisLite::viridis(5), domain = range(1:5))(1:5)`.
I wanted **one** function which will work on **one** argument.
```{r}
colour_values(1:5)
colour_values(letters[1:5])
```
I also want it available at the `src` (C/C++) level for linking to other packages.
---
### Why do you spell colour with a 'u'?
Because it's correct, and [R tells us to](https://developer.R-project.org/Rds.html)
> For consistency, aim to use British (rather than American) spelling
But don't worry, `color_values(1:5)` works as well
---
### How do I install it?
From CRAN
```{r, eval=FALSE}
install.packages("colourvalues")
```
Or install the development version from [GitHub](https://github.com/SymbolixAU/colourvalues) with:
```r
# install.packages("devtools")
devtools::install_github("SymbolixAU/colourvalues")
```
---
### How can I make use of it in my package?
**Rcpp**
All functions are written in `Rcpp`. I have exposed some of them in header files so you can "link to" them in your package.
For example, the `LinkingTo` section in `DESCRIPTION` will look something like
```yaml
LinkingTo:
Rcpp,
colourvalues
```
And in a **c++** source file so you can `#include` the API header
```cpp
#include "colourvalues/api.hpp"
// [[Rcpp::depends(colourvalues)]]
```
And call
```cpp
// return hex colours
colourvalues::api::colour_values_hex()
// return RGP matrix
colourvalues::api::colour_values_rgb()
```
**R**
If you're not using `Rcpp`, just `Import` this package like you would any other.
## Do you have any examples?
Of course!
#### 256 numbers mapped to a colour
```{r}
bar_plot <- function(df) {
barplot( height = df[["a"]], col = df[["col"]], border = NA, space = 0, yaxt = 'n')
}
df <- data.frame(a = 10, x = 1:256)
df$col <- colour_values(df$x, palette = "viridis")
bar_plot( df )
```
#### 5000 numbers on a non-linear scale
```{r}
df <- data.frame(a = 10, x = c((1:5000)**3))
df$col <- colour_values(df$x, palette = "viridis")
bar_plot( df )
```
#### 1000 random numbers
```{r}
df <- data.frame(a = 10, x = rnorm(n = 1000))
df$col <- colour_values(df$x, palette = "inferno")
bar_plot( df )
```
Eurgh!
```{r}
df <- df[with(df, order(x)), ]
bar_plot( df )
```
That's better!
---
## Are there only viridis palettes?
No, you can chose one from
```{r}
colour_palettes()
```
And you can use `show_colours()` to view them all. Here's what some of them look like
```{r}
show_colours( colours = colour_palettes(c("viridis", "colorspace")))
```
## Do I have to use the in-built palettes?
No, you can use your own specified as a matrix of red, green and blue columns in the range [0,255]
```{r}
n <- 100
m <- grDevices::colorRamp(c("red", "green"))( (1:n)/n )
df <- data.frame(a = 10, x = 1:n)
df$col <- colour_values(df$x, palette = m)
bar_plot( df )
```
## Do you support 'alpha' values
Yep. Either supply a single alpha value for all the colours
```{r}
## single alpha value for all colours
df <- data.frame(a = 10, x = 1:255)
df$col <- colour_values(df$x, alpha = 50)
bar_plot( df )
```
Or use a vector of values the same length as `x`
```{r}
df <- data.frame(a = 10, x = 1:300, y = rep(c(1:50, 50:1), 3) )
df$col <- colour_values(df$x, alpha = df$y)
bar_plot( df )
```
Or include the alpha value as a 4th column in the palette matrix
```{r}
n <- 100
m <- grDevices::colorRamp(c("red", "green"))( (1:n)/n )
## alpha values
m <- cbind(m, seq(0, 255, length.out = 100))
df <- data.frame(a = 10, x = 1:n)
df$col <- colour_values(df$x, palette = m)
bar_plot( df )
```
## Some of my plotting functions don't support alpha, can I exclude it?
Yep. Set `include_alpha = FALSE`
```{r}
colour_values(1:5, include_alpha = F)
colour_values_rgb(1:5, include_alpha = F)
```
## Can I get a summary of colours to use in a legend?
Yes, for numeric values use the `n_summaries` argument to specify the number of summary values you'd like
```{r}
colour_values(1:10, n_summaries = 3)
```
You can also specify the number of digits you'd like returned in the summary
```{r}
colour_values(rnorm(n = 10), n_summaries = 3, digits = 2)
```
You can also use `format = FALSE` if you don't want the summary values formatted.
```{r}
dte <- seq(as.Date("2018-01-01"), as.Date("2018-02-01"), by = 1)
colour_values(dte, n_summaries = 3)
colour_values(dte, n_summaries = 3, format = F)
```
For categorical values use `summary = TRUE` to return a uniqe set of the values, and their associated colours
```{r}
colour_values(sample(letters, size = 50, replace = T), summary = T)
```
### I see you support lists, but how does it work?
Basically, it's the same as un-listing the list to create a vector of all the values, then colouring them.
So if your list contains different types, it will coerce all values to the same type and colour them.
But it returns a list of the same structure.
For example,
```{r}
l <- list( x = 1:5, y = list(z = letters[1:5] ) )
colour_values( l )
x <- c( 1:5, letters[1:5] )
colour_values( x )
```
What it doesn't do is treat each list element independently. For this you would use
```{r}
lapply( l, colour_values )
```
---
### What's the performance like?
**10 million numeric values**
```{r, out.height=400}
library(microbenchmark)
library(scales)
library(viridisLite)
n <- 1e7
df <- data.frame(x = rnorm(n = n))
m <- microbenchmark(
colourvalues = { colourvalues::colour_values(x = df$x) },
scales = { col_numeric(palette = rgb(subset(viridis.map, opt=="D")[, 1:3]), domain = range(df$x))(df$x) },
times = 25
)
m
```
**1 million characters (26 unique values)**
```{r, out.height=400}
library(microbenchmark)
library(scales)
library(viridisLite)
n <- 1e6
x <- sample(x = letters, size = n, replace = TRUE)
df <- data.frame(x = x)
m <- microbenchmark(
colourvalues = { x <- colourvalues::colour_values(x = df$x) },
scales = { y <- col_factor(palette = rgb(subset(viridis.map, opt=="D")[, 1:3]), domain = unique(df$x))(df$x) },
times = 25
)
m
```