Skip to content

Commit a8b458a

Browse files
committed
Update README
1 parent cb6fdeb commit a8b458a

File tree

13 files changed

+768
-816
lines changed

13 files changed

+768
-816
lines changed

README.Rmd

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
knitr::opts_chunk$set(collapse=TRUE, fig.path="tools/figures/", fig.height=2, dev="svg")
33
```
44

5-
[![CRAN version](http://www.r-pkg.org/badges/version/shades)](https://cran.r-project.org/package=shades) [![Build Status](https://travis-ci.org/jonclayden/shades.svg?branch=master)](https://travis-ci.org/jonclayden/shades) [![codecov](https://codecov.io/gh/jonclayden/shades/branch/master/graph/badge.svg)](https://codecov.io/gh/jonclayden/shades)
5+
[![CRAN version](https://www.r-pkg.org/badges/version/shades)](https://cran.r-project.org/package=shades) [![CI Status](https://github.com/jonclayden/shades/actions/workflows/ci.yaml/badge.svg)](https://github.com/jonclayden/shades/actions/workflows/ci.yaml) [![codecov](https://codecov.io/gh/jonclayden/shades/branch/master/graph/badge.svg)](https://codecov.io/gh/jonclayden/shades) [![Dependencies](https://tinyverse.netlify.app/badge/shades)](https://tinyverse.netlify.app)
66

77
# Simple colour manipulation in R 😎
88

9-
The `shades` package allows colours to be manipulated easily in R. Properties such as brightness and saturation can be quickly queried, changed or varied, and perceptually uniform colour gradients can be constructed. It plays nicely with the pipe operator from the [popular `magrittr` package](https://github.com/tidyverse/magrittr), and fits naturally into that paradigm. It can also be used [with `ggplot2` scales](#interoperability-with-ggplot2).
9+
The `shades` package allows colours to be manipulated easily in R. Properties such as brightness and saturation can be quickly queried, changed or varied, and perceptually uniform colour gradients can be constructed. It plays nicely with the pipe operator from the [popular `magrittr` package](https://github.com/tidyverse/magrittr), or the similar native one introduced in R 4.1.0, and fits naturally into that paradigm. It can also be used [with `ggplot2` scales](#interoperability-with-ggplot2).
1010

11-
The package is available on [CRAN](https://cran.r-project.org/package=shades). You can also install the current development version from GitHub using [`devtools`](https://github.com/r-lib/devtools):
11+
The package is available on [CRAN](https://cran.r-project.org/package=shades). You can also install the current development version from GitHub using the `remotes` package:
1212

1313
```{r, eval=FALSE}
14-
# install.packages("devtools")
15-
devtools::install_github("jonclayden/shades")
14+
# install.packages("remotes")
15+
remotes::install_github("jonclayden/shades")
1616
```
1717

1818
Feedback on the package or suggestions are welcome, either by filing an issue or by email.
@@ -44,40 +44,39 @@ swatch(gradient(c("red","blue"), 5, space="Lab"))
4444

4545
Here, we are using the `swatch` function to visualise a set of colours as a series of squares. Notice the more uniform appearance of the gradient when it traverses through the [Lab colour space](https://en.wikipedia.org/wiki/Lab_color_space).
4646

47-
Similarly, we can create a set of new colours by changing the brightness and saturation levels of some base colours, and make the code more readable by using the [`magrittr` pipe operator](https://github.com/tidyverse/magrittr).
47+
Similarly, we can create a set of new colours by changing the brightness and saturation levels of some base colours, and make the code more readable by using the pipe operator.
4848

4949
```{r saturation}
50-
library(shades); library(magrittr)
51-
c("red","blue") %>% brightness(0.6) %>% saturation(seq(0,1,0.25)) %>% swatch
50+
c("red","blue") |> brightness(0.6) |> saturation(seq(0,1,0.25)) |> swatch()
5251
```
5352

5453
This operation takes the original two colours, reduces their brightness to 60%, assigns a whole series of saturation levels to the result, and then passes it to `swatch` for visualisation. Notice that the pipeline is combinative (like the base function `outer`), returning each combination of parameters in a multidimensional array. The final shades are arranged in two rows by `swatch`, for convenience.
5554

5655
Note that `NA` can be used as a pass-through value:
5756

5857
```{r missing}
59-
"cornflowerblue" %>% saturation(c(NA,seq(0,1,0.25))) %>% swatch
58+
"cornflowerblue" |> saturation(c(NA,seq(0,1,0.25))) |> swatch()
6059
```
6160

6261
Any of these gradients can be directly passed to a standard graphical function, to be used as a colour scale. However, when choosing a colour scale, it is helpful to bear in mind that some viewers may have a colour vision deficiency (colour blindness), making it harder for them to distinguish certain colours and therefore to see a continuous scale. The `dichromat` function can be used to simulate this.
6362

6463
```{r dichromat}
65-
rev(grDevices::rainbow(9)) %>% dichromat %>% swatch
66-
gradient("viridis",9) %>% dichromat %>% swatch
64+
rev(grDevices::rainbow(9)) |> dichromat() |> swatch()
65+
gradient("viridis",9) |> dichromat() |> swatch()
6766
```
6867

6968
Here we are using the built-in "viridis" colour map, [developed for Python's `matplotlib`](http://bids.github.io/colormap/), which was specifically designed to appear continuous under as many conditions as possible. When shown with simulated red-blindness, the default for `dichromat`, it is clearly much more interpretable than a typical rainbow palette generated by R's built-in graphics functions.
7069

7170
The package also supports colour mixing, either additively (as with light) or subtractively (as with paint). For example, consider additive mixtures of the three primary RGB colours.
7271

7372
```{r addmix}
74-
c("red", addmix("red","green"), "green", addmix("green","blue"), "blue") %>% swatch
73+
c("red", addmix("red","green"), "green", addmix("green","blue"), "blue") |> swatch()
7574
```
7675

7776
Similarly, we can subtractively combine the three secondary colours.
7877

7978
```{r submix}
80-
c("cyan", submix("cyan","magenta"), "magenta", submix("magenta","yellow"), "yellow") %>% swatch
79+
c("cyan", submix("cyan","magenta"), "magenta", submix("magenta","yellow"), "yellow") |> swatch()
8180
```
8281

8382
A "light mixture" infix operator, `%.)%`, and a "paint mixture" infix operator, `%_/%`, are also available.

README.md

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11

22

3-
[![CRAN version](http://www.r-pkg.org/badges/version/shades)](https://cran.r-project.org/package=shades) [![Build Status](https://travis-ci.org/jonclayden/shades.svg?branch=master)](https://travis-ci.org/jonclayden/shades) [![codecov](https://codecov.io/gh/jonclayden/shades/branch/master/graph/badge.svg)](https://codecov.io/gh/jonclayden/shades)
3+
[![CRAN version](https://www.r-pkg.org/badges/version/shades)](https://cran.r-project.org/package=shades) [![CI Status](https://github.com/jonclayden/shades/actions/workflows/ci.yaml/badge.svg)](https://github.com/jonclayden/shades/actions/workflows/ci.yaml) [![codecov](https://codecov.io/gh/jonclayden/shades/branch/master/graph/badge.svg)](https://codecov.io/gh/jonclayden/shades) [![Dependencies](https://tinyverse.netlify.app/badge/shades)](https://tinyverse.netlify.app)
44

55
# Simple colour manipulation in R 😎
66

7-
The `shades` package allows colours to be manipulated easily in R. Properties such as brightness and saturation can be quickly queried, changed or varied, and perceptually uniform colour gradients can be constructed. It plays nicely with the pipe operator from the [popular `magrittr` package](https://github.com/tidyverse/magrittr), and fits naturally into that paradigm. It can also be used [with `ggplot2` scales](#interoperability-with-ggplot2).
7+
The `shades` package allows colours to be manipulated easily in R. Properties such as brightness and saturation can be quickly queried, changed or varied, and perceptually uniform colour gradients can be constructed. It plays nicely with the pipe operator from the [popular `magrittr` package](https://github.com/tidyverse/magrittr), or the similar native one introduced in R 4.1.0, and fits naturally into that paradigm. It can also be used [with `ggplot2` scales](#interoperability-with-ggplot2).
88

9-
The package is available on [CRAN](https://cran.r-project.org/package=shades). You can also install the current development version from GitHub using [`devtools`](https://github.com/r-lib/devtools):
9+
The package is available on [CRAN](https://cran.r-project.org/package=shades). You can also install the current development version from GitHub using the `remotes` package:
1010

1111

12-
```r
13-
# install.packages("devtools")
14-
devtools::install_github("jonclayden/shades")
12+
``` r
13+
# install.packages("remotes")
14+
remotes::install_github("jonclayden/shades")
1515
```
1616

1717
Feedback on the package or suggestions are welcome, either by filing an issue or by email.
@@ -23,11 +23,11 @@ Colours are represented in R using [CSS-style hex strings](https://en.wikipedia.
2323
The `shades` package defines a simple class, `shade`, which uses exactly this same convention and is entirely compatible with built-in colours, but it also stores information about the coordinates of the colours in a particular [colour space](https://en.wikipedia.org/wiki/Color_space).
2424

2525

26-
```r
26+
``` r
2727
library(shades)
2828
red <- shade("red")
2929
print(unclass(red))
30-
## [1] "red"
30+
## [1] "#FF0000"
3131
## attr(,"space")
3232
## [1] "sRGB"
3333
## attr(,"coords")
@@ -38,34 +38,33 @@ print(unclass(red))
3838
From here, the package switches between colour spaces as required, allowing various kinds of colour manipulation to be performed straightforwardly. For example, let's find the saturation level of a few built-in colours.
3939

4040

41-
```r
41+
``` r
4242
saturation(c("papayawhip","lavenderblush","olivedrab"))
4343
## [1] 0.1647100 0.0588200 0.7535287
4444
```
4545

4646
Now let's consider a colour gradient stepping through two different colour spaces, which we might want to use as a palette or colour scale.
4747

4848

49-
```r
49+
``` r
5050
swatch(gradient(c("red","blue"), 5))
5151
```
5252

5353
![plot of chunk gradients](tools/figures/gradients-1.svg)
5454

55-
```r
55+
``` r
5656
swatch(gradient(c("red","blue"), 5, space="Lab"))
5757
```
5858

5959
![plot of chunk gradients](tools/figures/gradients-2.svg)
6060

6161
Here, we are using the `swatch` function to visualise a set of colours as a series of squares. Notice the more uniform appearance of the gradient when it traverses through the [Lab colour space](https://en.wikipedia.org/wiki/Lab_color_space).
6262

63-
Similarly, we can create a set of new colours by changing the brightness and saturation levels of some base colours, and make the code more readable by using the [`magrittr` pipe operator](https://github.com/tidyverse/magrittr).
63+
Similarly, we can create a set of new colours by changing the brightness and saturation levels of some base colours, and make the code more readable by using the pipe operator.
6464

6565

66-
```r
67-
library(shades); library(magrittr)
68-
c("red","blue") %>% brightness(0.6) %>% saturation(seq(0,1,0.25)) %>% swatch
66+
``` r
67+
c("red","blue") |> brightness(0.6) |> saturation(seq(0,1,0.25)) |> swatch()
6968
```
7069

7170
![plot of chunk saturation](tools/figures/saturation-1.svg)
@@ -75,23 +74,23 @@ This operation takes the original two colours, reduces their brightness to 60%,
7574
Note that `NA` can be used as a pass-through value:
7675

7776

78-
```r
79-
"cornflowerblue" %>% saturation(c(NA,seq(0,1,0.25))) %>% swatch
77+
``` r
78+
"cornflowerblue" |> saturation(c(NA,seq(0,1,0.25))) |> swatch()
8079
```
8180

8281
![plot of chunk missing](tools/figures/missing-1.svg)
8382

8483
Any of these gradients can be directly passed to a standard graphical function, to be used as a colour scale. However, when choosing a colour scale, it is helpful to bear in mind that some viewers may have a colour vision deficiency (colour blindness), making it harder for them to distinguish certain colours and therefore to see a continuous scale. The `dichromat` function can be used to simulate this.
8584

8685

87-
```r
88-
rev(grDevices::rainbow(9)) %>% dichromat %>% swatch
86+
``` r
87+
rev(grDevices::rainbow(9)) |> dichromat() |> swatch()
8988
```
9089

9190
![plot of chunk dichromat](tools/figures/dichromat-1.svg)
9291

93-
```r
94-
gradient("viridis",9) %>% dichromat %>% swatch
92+
``` r
93+
gradient("viridis",9) |> dichromat() |> swatch()
9594
```
9695

9796
![plot of chunk dichromat](tools/figures/dichromat-2.svg)
@@ -101,35 +100,38 @@ Here we are using the built-in "viridis" colour map, [developed for Python's `ma
101100
The package also supports colour mixing, either additively (as with light) or subtractively (as with paint). For example, consider additive mixtures of the three primary RGB colours.
102101

103102

104-
```r
105-
c("red", addmix("red","green"), "green", addmix("green","blue"), "blue") %>% swatch
103+
``` r
104+
c("red", addmix("red","green"), "green", addmix("green","blue"), "blue") |> swatch()
106105
```
107106

108107
![plot of chunk addmix](tools/figures/addmix-1.svg)
109108

110109
Similarly, we can subtractively combine the three secondary colours.
111110

112111

113-
```r
114-
c("cyan", submix("cyan","magenta"), "magenta", submix("magenta","yellow"), "yellow") %>% swatch
112+
``` r
113+
c("cyan", submix("cyan","magenta"), "magenta", submix("magenta","yellow"), "yellow") |> swatch()
115114
```
116115

117116
![plot of chunk submix](tools/figures/submix-1.svg)
118117

119118
A "light mixture" infix operator, `%.)%`, and a "paint mixture" infix operator, `%_/%`, are also available.
120119

121120

122-
```r
121+
``` r
123122
("red" %.)% "green") == "yellow"
124123
## [1] TRUE
124+
```
125+
126+
``` r
125127
("cyan" %_/% "magenta") == "blue"
126128
## [1] TRUE
127129
```
128130

129131
Finally, you can calculate perceptual distances to a reference colour, as in
130132

131133

132-
```r
134+
``` r
133135
distance(c("red","green","blue"), "red")
134136
## [1] 0.00000 86.52385 53.07649
135137
```
@@ -139,7 +141,7 @@ distance(c("red","green","blue"), "red")
139141
The `shades` package can be used with the popular [`ggplot2` graphics library](https://github.com/tidyverse/ggplot2) in different ways, with different levels of integration. Firstly, gradients from this package can be used as `ggplot2` colour scales through the manual scale functions; for example,
140142

141143

142-
```r
144+
``` r
143145
library(shades); library(ggplot2)
144146
mtcars$cyl<- factor(mtcars$cyl)
145147
ggplot(mtcars, aes(cyl,mpg,fill=cyl)) + geom_boxplot() + scale_fill_manual(values=gradient("viridis",3))
@@ -150,13 +152,13 @@ ggplot(mtcars, aes(cyl,mpg,fill=cyl)) + geom_boxplot() + scale_fill_manual(value
150152
This does not require the two packages to know anything about each other, and is flexible and powerful, but it doesn't easily allow existing `ggplot2` scales to be modified using the colour manipulation functions from `shades`. As of `shades` version 1.3.0, it is also possible to call the package's colour property functions directly on palette functions and scales, so that (for example), we can darken all colours in an existing scale slightly:
151153

152154

153-
```r
155+
``` r
154156
ggplot(mtcars, aes(cyl,mpg,fill=cyl)) + geom_boxplot() + scale_fill_brewer(type="qual")
155157
```
156158

157159
![plot of chunk scales](tools/figures/scales-1.svg)
158160

159-
```r
161+
``` r
160162
ggplot(mtcars, aes(cyl,mpg,fill=cyl)) + geom_boxplot() + lightness(scale_fill_brewer(type="qual"), delta(-20))
161163
```
162164

tools/figures/addmix-1.svg

Lines changed: 7 additions & 9 deletions
Loading

0 commit comments

Comments
 (0)