-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
85 lines (64 loc) · 3.17 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# ggchromatic <img src='man/figures/logo.png' align="right" height="138" />
*/ʤiː-ʤiːkrəʊˈmætɪk/*
<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![R-CMD-check](https://github.com/teunbrand/ggchromatic/workflows/R-CMD-check/badge.svg)](https://github.com/teunbrand/ggchromatic/actions)
[![Codecov test coverage](https://codecov.io/gh/teunbrand/ggchromatic/branch/master/graph/badge.svg)](https://codecov.io/gh/teunbrand/ggchromatic?branch=master)
<!-- badges: end -->
The 'ggchromatic' package provides additional colour and fill scales to use with 'ggplot2' It uses the 'ggplot2' extension system to map a number of variables to different colour spaces with the 'farver' package. The package introduces 'chromatic scales', a term mirroring music terms. In music, chromatic scales cover all 12 notes. In ggchromatic, a chromatic scale can cover all channels in colour space. Admittedly, colour spaces might not be the most intuitive tool for interpreting a data visualisation, but can be useful for visualising less strict data impressions.
## Installation
You can install the development of ggchromatic version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("teunbrand/ggchromatic")
```
## Example
These is a basic example of mapping three variables to the RGB colour space. This happens automatically for colours constructed by `rgb_spec()`.
```{r}
library(ggchromatic)
ggplot(mtcars, aes(mpg, disp)) +
geom_point(aes(colour = cmy_spec(mpg, drat, wt)))
```
The associated scale functions give more options for customisation, like the HSV scale below.
```{r}
df <- data.frame(
x = c(row(volcano)), y = c(col(volcano)), z = c(volcano)
)
ggplot(df, aes(x, y, fill = hsv_spec(z, x, y))) +
geom_raster() +
scale_fill_hsv(limits = list(h = c(NA, 170)),
oob = scales::oob_squish,
channel_limits = list(h = c(0, 0.8))) +
coord_equal()
```
Perhaps a use-case for chromatic scales is when the interpretation of a variable isn't directly related to a property of the data points. For example, embeddings project data into a new space which might preserve some properties, such as distance between data points, but wherein dimensions of the new space are meaningless on their own. You can use a chromatic scale to map the embedding to colours, which can give a neat 'flavour' to the data points through colour.
```{r}
if (requireNamespace("umap")) {
set.seed(42)
# Project the iris dataset into 3D UMAP space
config <- umap::umap.defaults
config$n_components <- 3
umap_3d <- umap::umap(iris[, 1:4], config)$layout
df <- data.frame(
SL = iris$Sepal.Length,
SW = iris$Petal.Width,
x = umap_3d[, 1],
y = umap_3d[, 2],
z = umap_3d[, 3]
)
ggplot(df, aes(SL, SW)) +
geom_point(aes(colour = rgb_spec(x, y, z)))
}
```