项目作者: teunbrand

项目描述 :
Colour and fill scales for 'ggplot2' using colour spaces.
高级语言: R
项目地址: git://github.com/teunbrand/ggchromatic.git
创建时间: 2021-01-14T17:23:34Z
项目社区:https://github.com/teunbrand/ggchromatic

开源协议:Other

下载


" class="reference-link">ggchromatic

/ʤiː-ʤiːkrəʊˈmætɪk/

Lifecycle:
experimental
R-CMD-check
Codecov test
coverage

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 with:

  1. # install.packages("devtools")
  2. 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().

  1. library(ggchromatic)
  2. #> Loading required package: ggplot2
  3. ggplot(mtcars, aes(mpg, disp)) +
  4. geom_point(aes(colour = cmy_spec(mpg, drat, wt)))

The associated scale functions give more options for customisation, like
the HSV scale below.

  1. df <- data.frame(
  2. x = c(row(volcano)), y = c(col(volcano)), z = c(volcano)
  3. )
  4. ggplot(df, aes(x, y, fill = hsv_spec(z, x, y))) +
  5. geom_raster() +
  6. scale_fill_hsv(limits = list(h = c(NA, 170)),
  7. oob = scales::oob_squish,
  8. channel_limits = list(h = c(0, 0.8))) +
  9. 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.

  1. if (requireNamespace("umap")) {
  2. set.seed(42)
  3. # Project the iris dataset into 3D UMAP space
  4. config <- umap::umap.defaults
  5. config$n_components <- 3
  6. umap_3d <- umap::umap(iris[, 1:4], config)$layout
  7. df <- data.frame(
  8. SL = iris$Sepal.Length,
  9. SW = iris$Petal.Width,
  10. x = umap_3d[, 1],
  11. y = umap_3d[, 2],
  12. z = umap_3d[, 3]
  13. )
  14. ggplot(df, aes(SL, SW)) +
  15. geom_point(aes(colour = rgb_spec(x, y, z)))
  16. }
  17. #> Loading required namespace: umap