├── .Rbuildignore
├── .github
├── .gitignore
└── workflows
│ ├── R-CMD-check.yaml
│ ├── pkgdown.yaml
│ └── test-coverage.yaml
├── .gitignore
├── CITATION.cff
├── DESCRIPTION
├── LICENSE
├── LICENSE.md
├── NAMESPACE
├── NEWS.md
├── R
├── aaa.R
├── as_tibble.R
├── colour-manipulation.R
├── format.R
├── ggplot2-scales.R
├── included-palettes.R
├── options.R
├── pal_colour.R
├── pal_mix.R
├── pal_palette.R
├── pal_ramp.R
├── palettes-package.R
├── pillar.R
├── plot.R
├── pretty.R
├── print.R
├── reexport.R
├── scales-colour-mapping.R
├── symbol.R
├── sysdata.rda
├── utils.R
├── vctrs-arithmetic.R
├── vctrs-math.R
└── zzz.R
├── README.Rmd
├── README.md
├── codecov.yml
├── cran-comments.md
├── data-raw
├── README.md
├── colour-names.R
├── included-palettes.R
├── made-with-palettes.R
├── sysdata.R
└── sysdata
│ ├── colour_names.rds
│ └── made-with-palettes.rds
├── data
├── discrete_palettes.rda
├── diverging_palettes.rda
├── met_palettes.rda
├── met_palettes_a11y.rda
├── nord_palettes.rda
├── penguin_palettes.rda
├── performance_palettes.rda
├── pnw_palettes.rda
├── sequential_palettes.rda
└── viridis_palettes.rda
├── inst
└── WORDLIST
├── man
├── as_tibble.palettes_colour.Rd
├── carto_palettes.Rd
├── colour-mixing-arithmetic.Rd
├── colour-mixing-math.Rd
├── figures
│ ├── README-
│ │ ├── pal-colour-character.svg
│ │ ├── pal-colour-hex.svg
│ │ ├── pal-colour-vector.svg
│ │ ├── pal-palette.svg
│ │ └── tibble.svg
│ └── logo.png
├── list_colour_symbols.Rd
├── met_palettes.Rd
├── nord_palettes.Rd
├── pal_colour.Rd
├── pal_numeric.Rd
├── pal_palette.Rd
├── pal_ramp.Rd
├── palettes-options.Rd
├── palettes-package.Rd
├── palettes-rlang.Rd
├── palettes-vctrs.Rd
├── penguin_palettes.Rd
├── performance_palettes.Rd
├── plot.palettes_colour.Rd
├── pnw_palettes.Rd
├── reexports.Rd
├── scale_colour_palette_d.Rd
└── viridis_palettes.Rd
├── palettes.Rproj
├── pkgdown
├── _pkgdown.yml
└── favicon
│ ├── apple-touch-icon-120x120.png
│ ├── apple-touch-icon-152x152.png
│ ├── apple-touch-icon-180x180.png
│ ├── apple-touch-icon-60x60.png
│ ├── apple-touch-icon-76x76.png
│ ├── apple-touch-icon.png
│ ├── favicon-16x16.png
│ ├── favicon-32x32.png
│ └── favicon.ico
├── revdep
├── .gitignore
├── README.md
├── cran.md
├── email.yml
├── failures.md
└── problems.md
├── tests
├── testthat.R
└── testthat
│ ├── _snaps
│ ├── colour.md
│ ├── ggplot2-scales.md
│ └── symbol.md
│ ├── helper.R
│ ├── test-colour.R
│ ├── test-ggplot2-scales.R
│ ├── test-pal_ramp.R
│ ├── test-palette.R
│ └── test-symbol.R
└── vignettes
├── .gitignore
├── articles
└── made-with-palettes.Rmd
├── biscale.Rmd
├── compatibility.Rmd
├── creating-packages.Rmd
├── ggplot2.Rmd
├── gt.Rmd
└── palettes.Rmd
/.Rbuildignore:
--------------------------------------------------------------------------------
1 | ^palettes\.Rproj$
2 | ^\.Rproj\.user$
3 | ^README\.Rmd$
4 | ^LICENSE\.md$
5 | ^_pkgdown\.yml$
6 | ^docs$
7 | ^pkgdown$
8 | ^\.github$
9 | ^data-raw$
10 | ^codecov\.yml$
11 | ^cran-comments\.md$
12 | ^CRAN-SUBMISSION$
13 | ^revdep$
14 | ^vignettes/articles$
15 | ^CITATION\.cff$
16 |
--------------------------------------------------------------------------------
/.github/.gitignore:
--------------------------------------------------------------------------------
1 | *.html
2 |
--------------------------------------------------------------------------------
/.github/workflows/R-CMD-check.yaml:
--------------------------------------------------------------------------------
1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
3 | on:
4 | push:
5 | branches: [main, master]
6 | pull_request:
7 | branches: [main, master]
8 |
9 | name: R-CMD-check
10 |
11 | jobs:
12 | R-CMD-check:
13 | runs-on: ${{ matrix.config.os }}
14 |
15 | name: ${{ matrix.config.os }} (${{ matrix.config.r }})
16 |
17 | strategy:
18 | fail-fast: false
19 | matrix:
20 | config:
21 | - {os: macos-latest, r: 'release'}
22 | - {os: windows-latest, r: 'release'}
23 | - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
24 | - {os: ubuntu-latest, r: 'release'}
25 | - {os: ubuntu-latest, r: 'oldrel-1'}
26 |
27 | env:
28 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
29 | R_KEEP_PKG_SOURCE: yes
30 |
31 | steps:
32 | - uses: actions/checkout@v3
33 |
34 | - uses: r-lib/actions/setup-pandoc@v2
35 |
36 | - uses: r-lib/actions/setup-r@v2
37 | with:
38 | r-version: ${{ matrix.config.r }}
39 | http-user-agent: ${{ matrix.config.http-user-agent }}
40 | use-public-rspm: true
41 |
42 | - uses: r-lib/actions/setup-r-dependencies@v2
43 | with:
44 | extra-packages: any::rcmdcheck
45 | needs: check
46 |
47 | - uses: r-lib/actions/check-r-package@v2
48 | with:
49 | upload-snapshots: true
50 |
--------------------------------------------------------------------------------
/.github/workflows/pkgdown.yaml:
--------------------------------------------------------------------------------
1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
3 | on:
4 | push:
5 | branches: [main, master]
6 | pull_request:
7 | branches: [main, master]
8 | release:
9 | types: [published]
10 | workflow_dispatch:
11 |
12 |
13 | name: pkgdown
14 |
15 |
16 | jobs:
17 | pkgdown:
18 | runs-on: ubuntu-latest
19 | # Only restrict concurrency for non-PR jobs
20 | concurrency:
21 | group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
22 | env:
23 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
24 | steps:
25 | - uses: actions/checkout@v3
26 |
27 |
28 | - uses: r-lib/actions/setup-pandoc@v2
29 |
30 |
31 | - uses: r-lib/actions/setup-r@v2
32 | with:
33 | use-public-rspm: true
34 |
35 |
36 | - uses: r-lib/actions/setup-r-dependencies@v2
37 | with:
38 | extra-packages: any::pkgdown, local::.
39 | needs: website
40 |
41 |
42 | - name: Build site
43 | run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
44 | shell: Rscript {0}
45 |
46 |
47 | - name: Deploy to GitHub pages 🚀
48 | if: github.event_name != 'pull_request'
49 | uses: JamesIves/github-pages-deploy-action@v4.4.1
50 | with:
51 | clean: false
52 | branch: gh-pages
53 | folder: docs
54 |
--------------------------------------------------------------------------------
/.github/workflows/test-coverage.yaml:
--------------------------------------------------------------------------------
1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
3 | on:
4 | push:
5 | branches: [main, master]
6 | pull_request:
7 | branches: [main, master]
8 |
9 | name: test-coverage
10 |
11 | jobs:
12 | test-coverage:
13 | runs-on: ubuntu-latest
14 | env:
15 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
16 |
17 | steps:
18 | - uses: actions/checkout@v3
19 |
20 | - uses: r-lib/actions/setup-r@v2
21 | with:
22 | use-public-rspm: true
23 |
24 | - uses: r-lib/actions/setup-r-dependencies@v2
25 | with:
26 | extra-packages: any::covr
27 | needs: coverage
28 |
29 | - name: Test coverage
30 | run: |
31 | covr::codecov(
32 | quiet = FALSE,
33 | clean = FALSE,
34 | install_path = file.path(Sys.getenv("RUNNER_TEMP"), "package")
35 | )
36 | shell: Rscript {0}
37 |
38 | - name: Show testthat output
39 | if: always()
40 | run: |
41 | ## --------------------------------------------------------------------
42 | find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true
43 | shell: bash
44 |
45 | - name: Upload test results
46 | if: failure()
47 | uses: actions/upload-artifact@v3
48 | with:
49 | name: coverage-test-failures
50 | path: ${{ runner.temp }}/package
51 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .Rproj.user
2 | .Rhistory
3 | .RData
4 | .Ruserdata
5 | docs
6 |
7 | /README_cache
8 | inst/doc
9 |
--------------------------------------------------------------------------------
/CITATION.cff:
--------------------------------------------------------------------------------
1 | cff-version: 1.2.0
2 | message: 'To cite package "palettes" in publications use:'
3 | type: software
4 | license: MIT
5 | title: 'palettes: Methods for Colour Vectors and Colour Palettes'
6 | abstract: 'Provides a comprehensive library for colour vectors and colour palettes
7 | using a new family of colour classes (palettes_colour and palettes_palette) that
8 | always print as hex codes with colour previews. Capabilities include: formatting,
9 | casting and coercion, extraction and updating of components, plotting, colour mixing
10 | arithmetic, and colour interpolation.'
11 | authors:
12 | - family-names: McCarthy
13 | given-names: Michael
14 | repository: https://CRAN.R-project.org/package=palettes
15 | repository-code: https://github.com/mccarthy-m-g/palettes
16 | url: https://mccarthy-m-g.github.io/palettes/
17 | version: 0.1.1
18 | date-released: 2023-01-12
19 |
--------------------------------------------------------------------------------
/DESCRIPTION:
--------------------------------------------------------------------------------
1 | Package: palettes
2 | Title: Methods for Colour Vectors and Colour Palettes
3 | Version: 0.2.1.9000
4 | Authors@R:
5 | person(given = "Michael",
6 | family = "McCarthy",
7 | role = c("aut", "cre", "cph"),
8 | email = "m.mccarthy1624@gmail.com")
9 | Description: Provides a comprehensive library for colour vectors and colour
10 | palettes using a new family of colour classes (palettes_colour and
11 | palettes_palette) that always print as hex codes with colour previews.
12 | Capabilities include: formatting, casting and coercion, extraction and
13 | updating of components, plotting, colour mixing arithmetic, and colour
14 | interpolation.
15 | License: MIT + file LICENSE
16 | URL: https://mccarthy-m-g.github.io/palettes/, https://github.com/mccarthy-m-g/palettes
17 | BugReports: https://github.com/mccarthy-m-g/palettes/issues
18 | Depends:
19 | R (>= 2.10)
20 | Imports:
21 | vctrs,
22 | cli,
23 | methods,
24 | pillar,
25 | rlang (>= 1.0.0),
26 | purrr,
27 | prismatic,
28 | farver (>= 2.0.3),
29 | ggplot2 (>= 3.5.0),
30 | scales,
31 | tibble
32 | Suggests:
33 | pkgdown,
34 | testthat (>= 3.0.0),
35 | dplyr,
36 | knitr (>= 1.22),
37 | rmarkdown (>= 2.20),
38 | colorspace,
39 | gt (>= 0.9.0),
40 | biscale,
41 | sf,
42 | patchwork,
43 | MetBrewer,
44 | nord,
45 | PNWColors,
46 | viridisLite,
47 | covr,
48 | grDevices,
49 | withr
50 | VignetteBuilder:
51 | knitr
52 | Config/Needs/website:
53 | asciicast (>= 2.2.1),
54 | fontawesome
55 | Config/testthat/edition: 3
56 | Encoding: UTF-8
57 | Language: en-GB
58 | LazyData: true
59 | Roxygen: list(markdown = TRUE)
60 | RoxygenNote: 7.3.2
61 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | YEAR: 2021
2 | COPYRIGHT HOLDER: Michael McCarthy
3 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | # MIT License
2 |
3 | Copyright (c) 2024 Michael McCarthy
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/NAMESPACE:
--------------------------------------------------------------------------------
1 | # Generated by roxygen2: do not edit by hand
2 |
3 | S3method("+",palettes_colour)
4 | S3method(as_colour,default)
5 | S3method(as_colour,palettes_palette)
6 | S3method(as_tibble,palettes_colour)
7 | S3method(as_tibble,palettes_palette)
8 | S3method(cumsum,palettes_colour)
9 | S3method(format,palettes_colour)
10 | S3method(obj_print_data,palettes_colour)
11 | S3method(pal_ramp,palettes_colour)
12 | S3method(pal_ramp,palettes_palette)
13 | S3method(pillar_shaft,palettes_colour)
14 | S3method(plot,palettes_colour)
15 | S3method(plot,palettes_palette)
16 | S3method(print,palettes_colour)
17 | S3method(sum,palettes_colour)
18 | S3method(vec_arith,palettes_colour)
19 | S3method(vec_arith.palettes_colour,default)
20 | S3method(vec_arith.palettes_colour,palettes_colour)
21 | S3method(vec_cast,character.palettes_colour)
22 | S3method(vec_cast,list.palettes_palette)
23 | S3method(vec_cast,palettes_colour.character)
24 | S3method(vec_cast,palettes_colour.palettes_colour)
25 | S3method(vec_cast,palettes_palette.list)
26 | S3method(vec_cast,palettes_palette.palettes_palette)
27 | S3method(vec_math,palettes_colour)
28 | S3method(vec_ptype2,character.palettes_colour)
29 | S3method(vec_ptype2,list.palettes_palette)
30 | S3method(vec_ptype2,palettes_colour.character)
31 | S3method(vec_ptype2,palettes_colour.palettes_colour)
32 | S3method(vec_ptype2,palettes_palette.list)
33 | S3method(vec_ptype2,palettes_palette.palettes_palette)
34 | S3method(vec_ptype_abbr,palettes_colour)
35 | S3method(vec_ptype_abbr,palettes_palette)
36 | S3method(vec_ptype_full,palettes_colour)
37 | S3method(vec_ptype_full,palettes_palette)
38 | export(as_color)
39 | export(as_colour)
40 | export(as_palette)
41 | export(as_tibble)
42 | export(is_color)
43 | export(is_colour)
44 | export(is_palette)
45 | export(list_color_symbols)
46 | export(list_colour_symbols)
47 | export(pal_bin)
48 | export(pal_color)
49 | export(pal_colour)
50 | export(pal_factor)
51 | export(pal_numeric)
52 | export(pal_palette)
53 | export(pal_quantile)
54 | export(pal_ramp)
55 | export(scale_color_palette_b)
56 | export(scale_color_palette_c)
57 | export(scale_color_palette_d)
58 | export(scale_colour_palette_b)
59 | export(scale_colour_palette_c)
60 | export(scale_colour_palette_d)
61 | export(scale_fill_palette_b)
62 | export(scale_fill_palette_c)
63 | export(scale_fill_palette_d)
64 | import(rlang)
65 | import(vctrs)
66 | importFrom(methods,setOldClass)
67 | importFrom(pillar,pillar_shaft)
68 | importFrom(tibble,as_tibble)
69 | importFrom(tibble,tibble)
70 |
--------------------------------------------------------------------------------
/NEWS.md:
--------------------------------------------------------------------------------
1 | # palettes (development version)
2 |
3 | - Included five new colour palette sets with the package: Cartography palettes (`discrete_palettes`, `sequential_palettes`, `diverging_palettes`), Performance palettes (`performance_palettes`), and Palmer penguins palettes (`penguin_palettes`).
4 |
5 | - When plotting colour palettes with `plot()`, the order of facets now matches the order of palettes in `pal_palette()` objects.
6 |
7 | # palettes 0.2.1
8 |
9 | This is a small patch release that applies some internal fixes for the `scale_()` functions, due to a breaking change in ggplot2 v3.5.0 that deprecated the `scale_name` argument in `ggplot2::continuous_scale()`, `ggplot2::discrete_scale()` and `ggplot2::binned_scale()`. The "gt" vignette has also been updated to reflect changes to the `gt::data_color()` function in gt v0.9.0.
10 |
11 | # palettes 0.2.0
12 |
13 | - Added a variety of global options to adjust the printing behaviour of colour vectors. See `help("palettes-options")` and `vignette("palettes")` for details and examples (#35).
14 |
15 | - Added a "Made with palettes" article to the pkgdown site showcasing colour palette packages made with palettes (#37).
16 |
17 | - Improved error message for invalid colours to support singular and plural grammar (@olivroy, #32).
18 |
19 | - Minor improvements to documentation throughout the package---mainly cross-linking (#38).
20 |
21 | # palettes 0.1.1
22 |
23 | This is a small patch release that fixes a typo in the "Creating a Colour Package" vignette, replacing the non-existent `pal_brewer()` function with the intended `pal_ramp()` function.
24 |
25 | # palettes 0.1.0
26 |
27 | First public release.
28 |
--------------------------------------------------------------------------------
/R/aaa.R:
--------------------------------------------------------------------------------
1 | colourspaces <- c(
2 | "cmy", # 1
3 | # "cmyk", # 2 (Don't include since it has 4 channels, not 3)
4 | "hsl", # 3
5 | "hsb", # 4
6 | "hsv", # 5
7 | "lab", # 6
8 | "hunterlab", # 7
9 | "lch", # 8
10 | "luv", # 9
11 | "rgb", # 10
12 | "xyz", # 11
13 | "yxy", # 12
14 | "hcl", # 13
15 | "oklab", # 14
16 | "oklch" # 15
17 | )
18 |
--------------------------------------------------------------------------------
/R/as_tibble.R:
--------------------------------------------------------------------------------
1 | #' Cast colour vectors and colour palettes to tibbles
2 | #'
3 | #' `as_tibble()` turns an existing colour vector or colour palette into a
4 | #' so-called [tibble][tibble::tibble-package], a data frame with class `tbl_df`.
5 | #'
6 | #' @param x An object of class [`palettes_palette`][pal_palette()] or
7 | #' [`palettes_colour`][pal_colour()].
8 | #' @param ... Not used.
9 | #' @return
10 | #' A [tibble][tibble::tibble-package]. The output has the following properties:
11 | #'
12 | #' * For objects of class [`palettes_colour`][pal_colour()]: A tibble with
13 | #' column `colour` containing the colour vector.
14 | #' * For objects of class [`palettes_palette`][pal_palette()]: A tibble with
15 | #' columns `palette` and `colour` containing palette names and colour vectors.
16 | #' @seealso [pal_colour()], [pal_palette()]
17 | #' @export
18 | #' @examples
19 | #' x <- pal_colour(c("#663171", "#EA7428", "#0C7156"))
20 | #' as_tibble(x)
21 | #'
22 | #' y <- pal_palette(
23 | #' Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"),
24 | #' Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156")
25 | #' )
26 | #' as_tibble(y)
27 | as_tibble.palettes_colour <- function(x, ...) {
28 | tibble::tibble(colour = x)
29 | }
30 |
31 | #' @export
32 | #' @rdname as_tibble.palettes_colour
33 | as_tibble.palettes_palette <- function(x, ...) {
34 |
35 | purrr::map_dfr(
36 | x,
37 | function(x) tibble::tibble(colour = x),
38 | .id = "palette"
39 | )
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/R/colour-manipulation.R:
--------------------------------------------------------------------------------
1 | #' Lighten or darken colour vectors and colour palettes
2 | #'
3 | #' Lighten or darken the set of colours in `palettes_palette` or
4 | #' `palettes_colour` objects to create new colour palettes.
5 | #'
6 | #' @param palette An object of class `palettes_palette` or `palettes_colour`.
7 | #' @param shift A numeric vector between 0 and 1 specifying the amount to
8 | #' lighten or darken by. 0 corresponds to no shift, and 1 corresponds to a
9 | #' complete shift.
10 | #' @param space The colour space to shift colours in. One of "HCL", "HSL" or
11 | #' "combined".
12 | #'
13 | #' @return An object of class `palettes_palette` or `palettes_colour`.
14 | #' @keywords internal
15 | #' @noRd
16 | #'
17 | #' @examples
18 | #' # Shifting colours in different colour spaces gives different results. We
19 | #' # demonstrate this here by linearly shifting the lighten amount of red by
20 | #' # 0.1 from 0 to 1.
21 | #' colour_vector <- rep("red", 11)
22 | #' shift_by <- seq(0, 1, 0.1)
23 | #' spaces_palette <- pal_palette(
24 | #' hcl = pal_lighten(colour_vector, shift = shift_by, space = "HCL"),
25 | #' hsl = pal_lighten(colour_vector, shift = shift_by, space = "HSL"),
26 | #' combined = pal_lighten(colour_vector, shift = shift_by, space = "combined")
27 | #' )
28 | #' plot(spaces_palette)
29 | pal_lighten <- function(
30 | palette,
31 | shift = 0.5,
32 | space = c("HCL", "HSL", "combined")
33 | ) {
34 |
35 | if (is_palette(palette)) {
36 | palette_lighten(palette, shift, space)
37 | } else {
38 | colour_lighten(palette, shift, space)
39 | }
40 |
41 | }
42 |
43 | #' @keywords internal
44 | #' @noRd
45 | #' @rdname pal_lighten
46 | pal_darken <- function(
47 | palette,
48 | shift = 0.5,
49 | space = c("HCL", "HSL", "combined")
50 | ) {
51 |
52 | if (is_palette(palette)) {
53 | palette_lighten(palette, -1 * shift, space)
54 | } else {
55 | colour_lighten(palette, -1 * shift, space)
56 | }
57 |
58 | }
59 |
60 | colour_lighten <- function(
61 | palette,
62 | shift = 0.5,
63 | space = c("HCL", "HSL", "combined")
64 | ) {
65 |
66 | # These checks are done by prismatic::clr_lighten(), but we repeat them here
67 | # so the error messages are consistent with the palettes API.
68 | if (!(length(shift) == 1 || (length(shift) == length(palette)))) {
69 | rlang::abort("`shift` must be of length 1 or the same length as `palette`.")
70 | }
71 | space <- rlang::arg_match(space)
72 |
73 | palette <- prismatic::clr_lighten(palette, shift, space)
74 | as_colour(as.character(palette))
75 |
76 | }
77 |
78 | palette_lighten <- function(
79 | palette,
80 | shift = 0.5,
81 | space = c("HCL", "HSL", "combined")
82 | ) {
83 | palette <- purrr::map(palette, ~ colour_lighten(.x, shift, space))
84 | as_palette(palette)
85 | }
86 |
--------------------------------------------------------------------------------
/R/format.R:
--------------------------------------------------------------------------------
1 | # colour -----------------------------------------------------------------------
2 |
3 | #' @export
4 | format.palettes_colour <- function(x, ...) {
5 | out <- str_to_hex(vec_data(x))
6 | # It's important to keep NA in the vector!
7 | out[is.na(x)] <- NA
8 | out
9 | }
10 |
11 | #' @export
12 | vec_ptype_full.palettes_colour <- function(x, ...) "palettes_colour"
13 | #' @export
14 | vec_ptype_abbr.palettes_colour <- function(x, ...) "colour"
15 |
16 | # palette ---------------------------------------------------------------------
17 |
18 | #' @export
19 | vec_ptype_full.palettes_palette <- function(x, ...) "palettes_palette"
20 | #' @export
21 | vec_ptype_abbr.palettes_palette <- function(x, ...) "palette"
22 |
--------------------------------------------------------------------------------
/R/ggplot2-scales.R:
--------------------------------------------------------------------------------
1 | #' Colour scales from colour vectors and colour palettes
2 | #'
3 | #' Create discrete, continuous, and binned colour scales from colour vectors and
4 | #' colour palettes.
5 | #'
6 | #' @inheritParams pal_ramp
7 | #' @param ... Other arguments passed on to [ggplot2::discrete_scale()],
8 | #' [ggplot2::continuous_scale()], or [ggplot2::binned_scale()] to control name,
9 | #' limits, breaks, labels and so forth.
10 | #' @return A scale function that controls the mapping between data and colour or
11 | #' fill aesthetics in a [ggplot2][ggplot2::ggplot2-package] plot.
12 | #' @export
13 | #' @examples
14 | #' library(ggplot2)
15 | #'
16 | #' # Use palette_d with discrete data
17 | #' discrete_pal <- pal_colour(c("#663171", "#EA7428", "#0C7156"))
18 | #' ggplot(mtcars, aes(wt, mpg, colour = as.factor(cyl))) +
19 | #' geom_point(size = 3) +
20 | #' scale_colour_palette_d(discrete_pal)
21 | #'
22 | #' # Use palette_c with continuous data
23 | #' continuous_pal <- pal_colour(c("#3C0D03", "#E67424", "#F5C34D"))
24 | #' ggplot(mtcars, aes(wt, mpg, colour = mpg)) +
25 | #' geom_point(size = 3) +
26 | #' scale_colour_palette_c(continuous_pal)
27 | #'
28 | #' # Use palette_b to bin continuous data before mapping
29 | #' ggplot(mtcars, aes(wt, mpg, colour = mpg)) +
30 | #' geom_point(size = 3) +
31 | #' scale_colour_palette_b(continuous_pal)
32 | scale_colour_palette_d <- function(palette, direction = 1, ...) {
33 | scale_palette_d(aesthetics = "colour", palette, direction, ...)
34 | }
35 |
36 | #' @export
37 | #' @rdname scale_colour_palette_d
38 | scale_fill_palette_d <- function(palette, direction = 1, ...) {
39 | scale_palette_d(aesthetics = "fill", palette, direction, ...)
40 | }
41 |
42 | #' @export
43 | #' @rdname scale_colour_palette_d
44 | scale_colour_palette_c <- function(palette, direction = 1, ...) {
45 | scale_palette_c(aesthetics = "colour", palette, direction, ...)
46 | }
47 |
48 | #' @export
49 | #' @rdname scale_colour_palette_d
50 | scale_fill_palette_c <- function(palette, direction = 1, ...) {
51 | scale_palette_c(aesthetics = "fill", palette, direction, ...)
52 | }
53 |
54 | #' @export
55 | #' @rdname scale_colour_palette_d
56 | scale_colour_palette_b <- function(palette, direction = 1, ...) {
57 | scale_palette_b(aesthetics = "colour", palette, direction, ...)
58 | }
59 |
60 | #' @export
61 | #' @rdname scale_colour_palette_d
62 | scale_fill_palette_b <- function(palette, direction = 1, ...) {
63 | scale_palette_b(aesthetics = "fill", palette, direction, ...)
64 | }
65 |
66 | scale_palette_d <- function(aesthetics, palette, direction = 1, ...) {
67 |
68 | ggplot2::discrete_scale(
69 | aesthetics = aesthetics,
70 | palette = scales::manual_pal(
71 | get_palette_colours(palette, n = NULL, direction)
72 | ),
73 | ...
74 | )
75 |
76 | }
77 |
78 | scale_palette_c <- function(aesthetics, palette, direction = 1, ...) {
79 |
80 | ggplot2::continuous_scale(
81 | aesthetics = aesthetics,
82 | palette = scales::gradient_n_pal(
83 | get_palette_colours(palette, n = 256, direction)
84 | ),
85 | guide = "colourbar",
86 | ...
87 | )
88 |
89 | }
90 |
91 | scale_palette_b <- function(aesthetics, palette, direction = 1, ...) {
92 |
93 | ggplot2::binned_scale(
94 | aesthetics = aesthetics,
95 | palette = scales::gradient_n_pal(
96 | get_palette_colours(palette, n = 256, direction)
97 | ),
98 | guide = "coloursteps",
99 | ...
100 | )
101 |
102 | }
103 |
104 | # TODO: decide whether to turn this into a generator (n) function instead, as in, e.g.,:
105 | # https://www.wjakethompson.com/blog/taylor/2021-11-11-taylor-ggplot2/
106 | get_palette_colours <- function(x, n = NULL, direction = 1) {
107 |
108 | if (is_palette(x) & vec_size(x) > 1) {
109 | rlang::warn("Multiple palettes supplied, only the first palette will be used.")
110 | x <- vec_slice(x, 1)
111 | }
112 | x <- as_colour(x)
113 | pal_ramp(x, n, direction = direction)
114 |
115 | }
116 |
117 | # British to American spellings ----------------------------------------------
118 |
119 | #' @export
120 | #' @rdname scale_colour_palette_d
121 | #' @usage NULL
122 | scale_color_palette_d <- scale_colour_palette_d
123 |
124 | #' @export
125 | #' @rdname scale_colour_palette_d
126 | #' @usage NULL
127 | scale_color_palette_c <- scale_colour_palette_c
128 |
129 | #' @export
130 | #' @rdname scale_colour_palette_d
131 | #' @usage NULL
132 | scale_color_palette_b <- scale_colour_palette_b
133 |
--------------------------------------------------------------------------------
/R/included-palettes.R:
--------------------------------------------------------------------------------
1 | #' Cartography palettes
2 | #'
3 | #' Discrete, sequential, and diverging palettes created by cartographers.
4 | #'
5 | #' @source
6 | #' @seealso [pal_palette()], [pal_colour()]
7 | #' @name carto_palettes
8 | #' @aliases discrete_palettes, sequential_palettes, diverging_palettes
9 | #' @examples
10 | #' # Get all palettes by name.
11 | #' names(discrete_palettes)
12 | #' names(sequential_palettes)
13 | #' names(diverging_palettes)
14 | #'
15 | #' # Plot all palettes.
16 | #' plot(discrete_palettes)
17 | #' plot(sequential_palettes)
18 | #' plot(diverging_palettes)
19 | NULL
20 |
21 | #' @rdname carto_palettes
22 | #' @format ## `discrete_palettes`
23 | #' An object of class `palettes_palette` with `r vec_size(discrete_palettes)`
24 | #' colour palettes. Use `names(discrete_palettes)` to return all palette names.
25 | "discrete_palettes"
26 |
27 | #' @rdname carto_palettes
28 | #' @format ## `sequential_palettes`
29 | #' An object of class `palettes_palette` with `r vec_size(sequential_palettes)`
30 | #' colour palettes. Use `names(sequential_palettes)` to return all palette names.
31 | "sequential_palettes"
32 |
33 | #' @rdname carto_palettes
34 | #' @format ## `diverging_palettes`
35 | #' An object of class `palettes_palette` with `r vec_size(diverging_palettes)`
36 | #' colour palettes. Use `names(diverging_palettes)` to return all palette names.
37 | "diverging_palettes"
38 |
39 | #' Performance palettes
40 | #'
41 | #' A colourblind accessible palette for visualizing performance.
42 | #'
43 | #' @format ## `performance_palettes`
44 | #' An object of class `palettes_palette` with `r vec_size(performance_palettes)`
45 | #' colour palettes. All colours in each palette are distinguishable with
46 | #' deuteranopia, protanopia, and tritanopia. Use `names(performance_palettes)`
47 | #' to return all palette names.
48 | #' @source
49 | #' @seealso [pal_palette()], [pal_colour()]
50 | #' @examples
51 | #' # Get all palettes by name.
52 | #' names(performance_palettes)
53 | #'
54 | #' # Plot all palettes.
55 | #' plot(performance_palettes)
56 | "performance_palettes"
57 |
58 | #' Metropolitan Museum of Art palettes
59 | #'
60 | #' Palettes inspired by works at the Metropolitan Museum of Art in New York.
61 | #' Pieces selected come from various time periods, regions, and mediums.
62 | #'
63 | #' @format ## `met_palettes`
64 | #' An object of class `palettes_palette` with `r vec_size(met_palettes)` colour
65 | #' palettes. Use `names(met_palettes)` to return all palette names.
66 | #' @source
67 | #' @author [Blake Robert Mills](https://github.com/BlakeRMills)
68 | #' @seealso [pal_palette()], [pal_colour()], [MetBrewer::met.brewer()]
69 | #' @examples
70 | #' # Get all palettes by name.
71 | #' names(met_palettes)
72 | #'
73 | #' # Plot all palettes.
74 | #' plot(met_palettes)
75 | "met_palettes"
76 |
77 | #' @rdname met_palettes
78 | #' @format ## `met_palettes_a11y`
79 | #' An object of class `palettes_palette` limited to
80 | #' `r vec_size(met_palettes_a11y)` colourblind accessible palettes. All colours
81 | #' in each palette are distinguishable with deuteranopia, protanopia, and
82 | #' tritanopia. Use `names(met_palettes_a11y)` to return all palette names.
83 | "met_palettes_a11y"
84 |
85 | #' Palmer penguins palettes
86 | #'
87 | #' Palettes inspired by the Palmer penguins.
88 | #'
89 | #' @format ## `penguin_palettes`
90 | #' An object of class `palettes_palette` with `r vec_size(penguin_palettes)`
91 | #' colour palettes. Use `names(penguin_palettes)` to return all palette names.
92 | #' @source
93 | #' @author [Allison Horst](https://github.com/allisonhorst)
94 | #' @seealso [pal_palette()], [pal_colour()]
95 | #' @examples
96 | #' # Get all palettes by name.
97 | #' names(penguin_palettes)
98 | #'
99 | #' # Plot all palettes.
100 | #' plot(penguin_palettes)
101 | "penguin_palettes"
102 |
103 | #' Nord palettes
104 | #'
105 | #' Dimmed pastel palettes inspired by the Arctic and Canadian wilderness.
106 | #'
107 | #' @format ## `nord_palettes`
108 | #' An object of class `palettes_palette` with `r vec_size(nord_palettes)`
109 | #' colour palettes. Use `names(nord_palettes)` to return all palette names.
110 | #' @source
111 | #' @author [Jake Kaupp](https://github.com/jkaupp)
112 | #' @seealso [pal_palette()], [pal_colour()], [nord::nord()]
113 | #' @examples
114 | #' # Get all palettes by name.
115 | #' names(nord_palettes)
116 | #'
117 | #' # Plot all palettes.
118 | #' plot(nord_palettes)
119 | "nord_palettes"
120 |
121 | #' Pacific Northwest palettes
122 | #'
123 | #' Palettes inspired by Jake Lawlor's photos of the dreamiest, most colourful,
124 | #' PNW-iest places in Washington State.
125 | #'
126 | #' @format ## `pnw_palettes`
127 | #' An object of class `palettes_palette` with `r vec_size(pnw_palettes)` colour
128 | #' palettes. Use `names(pnw_palettes)` to return all palette names.
129 | #' @source
130 | #' @author [Jake Lawlor](https://github.com/jakelawlor)
131 | #' @seealso [pal_palette()], [pal_colour()], [PNWColors::pnw_palette()]
132 | #' @examples
133 | #' # Get all palettes by name.
134 | #' names(pnw_palettes)
135 | #'
136 | #' # Plot all palettes.
137 | #' plot(pnw_palettes)
138 | "pnw_palettes"
139 |
140 | #' Viridis palettes
141 | #'
142 | #' Colourblind accessible palettes that are perceptually uniform in both colour
143 | #' and black-and-white.
144 | #'
145 | #' @format ## `viridis_palettes`
146 | #' An object of class `palettes_palette` with `r vec_size(viridis_palettes)`
147 | #' colour palettes. All colours in each palette are distinguishable with
148 | #' deuteranopia, protanopia, and tritanopia. Use `names(viridis_palettes)` to
149 | #' return all palette names.
150 | #' @source
151 | #' @author [Simon Garnier](https://github.com/sjmgarnier)
152 | #' @seealso [pal_palette()], [pal_colour()], [viridisLite::viridis()]
153 | #' @examples
154 | #' # Get all palettes by name.
155 | #' names(viridis_palettes)
156 | #'
157 | #' # Plot all palettes.
158 | #' plot(viridis_palettes, n = 256)
159 | "viridis_palettes"
160 |
--------------------------------------------------------------------------------
/R/options.R:
--------------------------------------------------------------------------------
1 | #' Package options
2 | #'
3 | #' Options that adjust the behaviour of the palettes package.
4 | #'
5 | #' These options can be set via [options()] and queried via [getOption()].
6 | #'
7 | #' @section Options for the palettes package:
8 | #'
9 | #' \describe{
10 | #'
11 | #' \item{\code{palettes.print_symbol}:}{
12 | #'
13 | #' Character string setting the symbol used for colour previews. See
14 | #' [list_colour_symbols()] for a list of symbol choices. Defaults to
15 | #' `"circle_small"`. Set to `FALSE` to disable printing symbols.
16 | #'
17 | #' }
18 | #' \item{\code{palettes.print_hex}:}{
19 | #'
20 | #' Logical setting whether to print hex codes in colour previews. Defaults to
21 | #' `TRUE`.
22 | #'
23 | #' }
24 | #' \item{\code{palettes.print_alpha}:}{
25 | #'
26 | #' Logical setting whether to print the hex code alpha channel in colour
27 | #' previews. Defaults to `FALSE`. Colours without an alpha channel will be
28 | #' assumed to be full opacity.
29 | #'
30 | #' }
31 | #' \item{\code{palettes.print_sep}:}{
32 | #'
33 | #' Character string to separate colours by in colour previews. Defaults to `""`.
34 | #'
35 | #' }
36 | #' \item{\code{palettes.print_width}:}{
37 | #'
38 | #' Integer setting the maximum number of colours on a line in colour previews.
39 | #' Defaults to `1`.
40 | #'
41 | #' }
42 | #' \item{\code{palettes.print_index}:}{
43 | #'
44 | #' Logical setting whether to print the index of the first colour on each line
45 | #' in colour previews. Defaults to `FALSE`.
46 | #'
47 | #' }
48 | #'
49 | #' }
50 | #' @section Note:
51 | #' To disable formatting in colour previews set both `palettes.print_symbol` and
52 | #' `palettes.print_hex` to `FALSE`.
53 | #' @name palettes-options
54 | #' @examples
55 | #' options(
56 | #' palettes.print_symbol = "square",
57 | #' palettes.print_hex = FALSE,
58 | #' palettes.print_sep = " ",
59 | #' palettes.print_width = 3,
60 | #' palettes.print_index = TRUE
61 | #' )
62 | #' met_palettes$Cross
63 | NULL
64 |
--------------------------------------------------------------------------------
/R/pal_colour.R:
--------------------------------------------------------------------------------
1 | # for compatibility with the S4 system
2 | methods::setOldClass(c("palettes_colour", "vctrs_vctr"))
3 |
4 | #' Colour vectors
5 | #'
6 | #' This creates a character vector that represents colours so when it is
7 | #' printed, colours will be formatted as hexadecimal strings.
8 | #'
9 | #' @param x
10 | #' * For `pal_colour()`: A character vector of any of the three kinds of R colour
11 | #' specifications.
12 | #' * For `as_colour()`: An object to be coerced.
13 | #' * For `is_colour()`: An object to test.
14 | #' @details
15 | #' Colours can be specified using either:
16 | #' - Hexadecimal strings of the form `"#RRGGBB"` or `"#RRGGBBAA"`
17 | #' - Colour names from [grDevices::colors()]
18 | #' - Positive integers `i` that index into [grDevices::palette()]`[i]`
19 | #' @return An S3 vector of class `palettes_colour`.
20 | #' @seealso [pal_palette()]
21 | #' @export
22 | #' @examples
23 | #' pal_colour(c("darkred", "#0F7BA2"))
24 | #'
25 | #' is_colour("darkred")
26 | #' is_colour(pal_colour("darkred"))
27 | #'
28 | #' as_colour("#0F7BA2")
29 | pal_colour <- function(x = character()) {
30 | x <- vec_cast(x, character())
31 | validate_colour(new_colour(x))
32 | }
33 |
34 | new_colour <- function(x = character()) {
35 | vec_assert(x, character())
36 | new_vctr(x, class = "palettes_colour")
37 | }
38 |
39 | validate_colour <- function(x) {
40 | values <- vec_data(x)
41 | valid_colours <- is_valid_colour(values)
42 |
43 | if (!all(valid_colours) && vec_size(values) != 0) {
44 | invalid_colours <- values[!valid_colours]
45 | cli::cli_abort(c(
46 | "All `x` values must be hexadecimal strings or colour names.",
47 | "x" = "The following value{?s} {?is/are} not {?a/} valid colour{?s}: {.str {invalid_colours}}."
48 | ))
49 | }
50 |
51 | x
52 | }
53 |
54 | #' @export
55 | #' @rdname pal_colour
56 | is_colour <- function(x) {
57 | inherits(x, "palettes_colour")
58 | }
59 |
60 | #' @export
61 | #' @rdname pal_colour
62 | as_colour <- function(x) {
63 | UseMethod("as_colour")
64 | }
65 |
66 | #' @export
67 | #' @rdname pal_colour
68 | as_colour.default <- function(x) {
69 | vec_cast(x, new_colour())
70 | }
71 |
72 | #' @export
73 | #' @rdname pal_colour
74 | as_colour.palettes_palette <- function(x) {
75 | value <- unname(unlist(x))
76 | new_colour(value)
77 | }
78 |
79 | # Coerce ----------------------------------------------------------------------
80 |
81 | #' @export
82 | vec_ptype2.palettes_colour.palettes_colour <- function(x, y, ...) new_colour()
83 | #' @export
84 | vec_ptype2.palettes_colour.character <- function(x, y, ...) character()
85 | #' @export
86 | vec_ptype2.character.palettes_colour <- function(x, y, ...) character()
87 |
88 | # Cast ------------------------------------------------------------------------
89 |
90 | #' @export
91 | vec_cast.palettes_colour.palettes_colour <- function(x, to, ...) x
92 | #' @export
93 | vec_cast.palettes_colour.character <- function(x, to, ...) pal_colour(x)
94 | #' @export
95 | vec_cast.character.palettes_colour <- function(x, to, ...) vec_data(x)
96 |
97 | # British to American spellings ----------------------------------------------
98 |
99 | #' @export
100 | #' @rdname pal_colour
101 | #' @usage NULL
102 | pal_color <- pal_colour
103 |
104 | #' @export
105 | #' @rdname pal_colour
106 | #' @usage NULL
107 | is_color <- is_colour
108 |
109 | #' @export
110 | #' @rdname pal_colour
111 | #' @usage NULL
112 | as_color <- as_colour
113 |
--------------------------------------------------------------------------------
/R/pal_mix.R:
--------------------------------------------------------------------------------
1 | pal_mix <- function(x) {
2 | x_mat <- farver::decode_colour(x, to = "xyz")
3 | sum_mix <- t(as.matrix(colSums(x_mat)))
4 | hex <- farver::encode_colour(sum_mix, from = "xyz")
5 | # This probably isn't realistic, but ensures that red + yellow gives orange,
6 | # because sum mix gives yellow
7 | if (hex %in% str_to_hex(x)) {
8 | mean_mix <- t(as.matrix(colMeans(x_mat)))
9 | hex <- farver::encode_colour(mean_mix, from = "xyz")
10 | }
11 | as_colour(hex)
12 | }
13 |
--------------------------------------------------------------------------------
/R/pal_palette.R:
--------------------------------------------------------------------------------
1 | # for compatibility with the S4 system
2 | methods::setOldClass(c("palettes_palette", "vctrs_list_of"))
3 |
4 | #' Colour palettes
5 | #'
6 | #' This creates a list of colour vectors.
7 | #'
8 | #' @param ...
9 | #' * For `pal_palette()`: A named list of character vectors of any of the three
10 | #' kinds of R colour specifications, or a named list of colour vectors of
11 | #' class [`palettes_colour`][pal_colour()].
12 | #' @param x
13 | #' * For `as_palette()`: An object to be coerced.
14 | #' * For `is_palette()`: An object to test.
15 | #' @details
16 | #' Colours can be specified using either:
17 | #' - Hexadecimal strings of the form `"#RRGGBB"` or `"#RRGGBBAA"`
18 | #' - Colour names from [`grDevices::colors()`]
19 | #' - Positive integers `i` that index into [grDevices::palette()]`[i]`
20 | #' @return An S3 list of class `palettes_palette`.
21 | #' @seealso [pal_colour()]
22 | #' @export
23 | #' @examples
24 | #' pal_palette(
25 | #' Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"),
26 | #' Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156")
27 | #' )
28 | #'
29 | #' x <- list(
30 | #' Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"),
31 | #' Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156")
32 | #' )
33 | #' as_palette(x)
34 | pal_palette <- function(...) {
35 | x <- vec_cast_common(..., .to = pal_colour())
36 | new_palette(x)
37 | }
38 |
39 | new_palette <- function(x = list()) {
40 | new_list_of(x, ptype = pal_colour(), class = "palettes_palette")
41 | }
42 |
43 | #' @export
44 | #' @rdname pal_palette
45 | is_palette <- function(x) {
46 | inherits(x, "palettes_palette")
47 | }
48 |
49 | #' @export
50 | #' @rdname pal_palette
51 | as_palette <- function(x) {
52 | vec_cast(x, to = new_palette())
53 | }
54 |
55 | # Coerce ----------------------------------------------------------------------
56 |
57 | #' @export
58 | vec_ptype2.palettes_palette.palettes_palette <- function(x, y, ...) new_palette()
59 | #' @export
60 | vec_ptype2.palettes_palette.list <- function(x, y, ...) list()
61 | #' @export
62 | vec_ptype2.list.palettes_palette <- function(x, y, ...) list()
63 |
64 | # Cast ------------------------------------------------------------------------
65 |
66 | #' @export
67 | vec_cast.palettes_palette.palettes_palette <- function(x, to, ...) x
68 | #' @export
69 | vec_cast.palettes_palette.list <- function(x, to, ...) do.call(pal_palette, x)
70 | #' @export
71 | vec_cast.list.palettes_palette <- function(x, to, ...) vec_data(x)
72 |
--------------------------------------------------------------------------------
/R/pal_ramp.R:
--------------------------------------------------------------------------------
1 | #' Colour vector and colour palette interpolation
2 | #'
3 | #' Interpolate the set of colours in [`palettes_palette`][pal_palette()] or
4 | #' [`palettes_colour`][pal_colour()] objects to create new colour palettes.
5 | #'
6 | #' @param palette An object of class [`palettes_palette`][pal_palette()] or
7 | #' [`palettes_colour`][pal_colour()].
8 | #' @param n An integer specifying the number of colours to return.
9 | #' @param direction Sets the order of colours in the scale. If 1, the default,
10 | #' colours are ordered from first to last. If -1, the order of colours is
11 | #' reversed.
12 | #' @param space The colour space to interpolate in. One of: `"cmy"`, `"hsl"`,
13 | #' `"hsb"`, `"hsv"`, `"lab"` (CIE L*ab), `"hunterlab"` (Hunter Lab),
14 | #' `"oklab"`, `"lch"` (CIE Lch(ab) / polarLAB), `"luv"`, `"rgb"` (sRGB),
15 | #' `"xyz"`, `"yxy"` (CIE xyY), `"hcl"` (CIE Lch(uv) / polarLuv), or `"oklch"`
16 | #' (Polar form of oklab).
17 | #' @param interpolate The interpolation method. Either "linear" (default) or
18 | #' "spline".
19 | #' @return
20 | #' An object of the same type as `palette`. The output has the following properties:
21 | #'
22 | #' * For objects of class [`palettes_colour`][pal_colour()]: A colour vector
23 | #' with `n` colours.
24 | #' * For objects of class [`palettes_palette`][pal_palette()]: Colour palettes
25 | #' with `n` colours in each palette.
26 | #' @seealso [pal_colour()], [pal_palette()]
27 | #' @export
28 | #' @examples
29 | #' # The class returned after interpolation matches the input class.
30 | #' x <- pal_colour(c("darkslateblue", "cornflowerblue", "slategray1"))
31 | #' y <- pal_palette(blues = x)
32 | #' class(pal_ramp(x))
33 | #' class(pal_ramp(y))
34 | #'
35 | #' # Choose between linear and spline interpolation.
36 | #' pal_ramp(x, n = 7, interpolate = "linear")
37 | #' pal_ramp(x, n = 7, interpolate = "spline")
38 | #'
39 | #' # Palettes will have the same length after interpolation, regardless of the
40 | #' # number of colours in the original palette.
41 | #' z <- pal_palette(
42 | #' Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"),
43 | #' Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156")
44 | #' )
45 | #' pal_ramp(z, n = 5)
46 | pal_ramp <- function(
47 | palette,
48 | n = NULL,
49 | direction = 1,
50 | space = "lab",
51 | interpolate = c("linear", "spline")
52 | ) {
53 | UseMethod("pal_ramp")
54 | }
55 |
56 | #' @export
57 | #' @rdname pal_ramp
58 | pal_ramp.palettes_colour <- function(
59 | palette,
60 | n = NULL,
61 | direction = 1,
62 | space = "lab",
63 | interpolate = c("linear", "spline")
64 | ) {
65 | colour_ramp(palette, n, direction, space, interpolate)
66 | }
67 |
68 | #' @export
69 | #' @rdname pal_ramp
70 | pal_ramp.palettes_palette <- function(
71 | palette,
72 | n = NULL,
73 | direction = 1,
74 | space = "lab",
75 | interpolate = c("linear", "spline")
76 | ) {
77 | palette <- purrr::map(
78 | palette, function(x) colour_ramp(x, n, direction, space, interpolate)
79 | )
80 | new_palette(palette)
81 | }
82 |
83 | colour_ramp <- function(
84 | x,
85 | n = NULL,
86 | direction = 1,
87 | space = "lab",
88 | interpolate = c("linear", "spline")
89 | ) {
90 |
91 | # This is used in favour of `ifelse()` because that function only returns the
92 | # first colour in the palette since its logical is of length 1.
93 | x <- if (direction >= 0) x else rev(x)
94 |
95 | # No need to go through any computations if n is NULL, since the resultant
96 | # vector will be the same length as x.
97 | if (is.null(n)) {
98 | return(x)
99 | }
100 | # If only a single colour is supplied, this allows it to be repeated n times.
101 | if (vec_size(x) == 1) {
102 | x <- rep(x, times = 2)
103 | }
104 |
105 | space <- rlang::arg_match(space, values = colourspaces)
106 | # Note: This action is destructive in that if the original colour included
107 | # colour names, they will be converted to hexadecimal strings in the
108 | # interpolated vector.
109 | space_in <- farver::decode_colour(
110 | colour = x, to = space, na_value = "transparent"
111 | )
112 |
113 | # Colours need to be mapped to the interval [0,1]
114 | x_in <- seq(0, 1, length.out = length(x))
115 |
116 | # Choose interpolation method
117 | interpolate <- rlang::arg_match(interpolate)
118 | interpolate <- switch(
119 | interpolate,
120 | linear = stats::approxfun,
121 | spline = stats::splinefun
122 | )
123 |
124 | # Interpolation functions
125 | x_interp <- interpolate(x_in, space_in[, 1])
126 | y_interp <- interpolate(x_in, space_in[, 2])
127 | z_interp <- interpolate(x_in, space_in[, 3])
128 |
129 | # Function to map the interval [0,1] to a set of colours
130 | ramp <- function(x) {
131 | space_out <- cbind(x_interp(x), y_interp(x), z_interp(x))
132 | out <- farver::encode_colour(space_out, from = space)
133 | out
134 | }
135 |
136 | as_colour(ramp(seq(0, 1, length.out = n)))
137 |
138 | }
139 |
--------------------------------------------------------------------------------
/R/palettes-package.R:
--------------------------------------------------------------------------------
1 | #' @keywords internal
2 | "_PACKAGE"
3 |
4 | # The following block is used by usethis to automatically manage
5 | # roxygen namespace tags. Modify with care!
6 | ## usethis namespace: start
7 | #' @importFrom tibble tibble as_tibble
8 | #' @importFrom methods setOldClass
9 | ## usethis namespace: end
10 | NULL
11 |
12 | #' Internal vctrs methods
13 | #'
14 | #' @import vctrs
15 | #' @keywords internal
16 | #' @name palettes-vctrs
17 | NULL
18 |
19 | #' Internal rlang methods
20 | #'
21 | #' @import rlang
22 | #' @keywords internal
23 | #' @name palettes-rlang
24 | NULL
25 |
--------------------------------------------------------------------------------
/R/pillar.R:
--------------------------------------------------------------------------------
1 | #' @importFrom pillar pillar_shaft
2 | #' @export
3 | pillar_shaft.palettes_colour <- function(x, ...) {
4 | # Hex codes should always be printed in tibbles.
5 | op <- options(palettes.print_hex = TRUE)
6 | on.exit(options(op))
7 | pillar::new_pillar_shaft_simple(colour_format_symbol(x))
8 | }
9 |
--------------------------------------------------------------------------------
/R/plot.R:
--------------------------------------------------------------------------------
1 | # TODO: Add accessibility check functions
2 | #' Plot colour vectors and colour palettes
3 | #'
4 | #' Plot colour vectors and colour palettes as swatches.
5 | #'
6 | #' @inheritParams pal_ramp
7 | #' @param x An object of class [`palettes_palette`][pal_palette()] or
8 | #' [`palettes_colour`][pal_colour()].
9 | #' @param ... Not used.
10 | #' @return
11 | #' A [ggplot2][ggplot2::ggplot2-package] object. The output has the following properties:
12 | #'
13 | #' * For objects of class [`palettes_colour`][pal_colour()]: A plot of colour
14 | #' swatches.
15 | #' * For objects of class [`palettes_palette`][pal_palette()] with one palette:
16 | #' A plot of colour swatches with the palette name spanned across the swatches.
17 | #' * For objects of class [`palettes_palette`][pal_palette()] with more than one
18 | #' palette: A faceted plot of colour swatches with palette names as facet
19 | #' titles.
20 | #' @seealso [pal_colour()], [pal_palette()], [pal_ramp()]
21 | #' @export
22 | #' @examples
23 | #' # Objects of class `palettes_colour` are plotted as swatches.
24 | #' x <- pal_colour(c("darkslateblue", "cornflowerblue", "slategray1"))
25 | #' plot(x)
26 | #'
27 | #' # Objects of class `palettes_palette` with one palette are plotted with
28 | #' # the palette name spanned across the swatches.
29 | #' y <- pal_palette(Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"))
30 | #' plot(y)
31 | #'
32 | #' # Objects of class `palettes_palette` with multiple palettes are faceted.
33 | #' z <- pal_palette(
34 | #' Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"),
35 | #' Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156")
36 | #' )
37 | #' plot(z)
38 | #'
39 | #' # Colours can also be interpolated.
40 | #' plot(x, n = 5)
41 | #' plot(y, n = 5)
42 | #' plot(z, n = 5)
43 | plot.palettes_colour <- function(
44 | x,
45 | n = NULL,
46 | direction = 1,
47 | space = "lab",
48 | interpolate = c("linear", "spline"),
49 | ...
50 | ) {
51 | plot_colour(x, n, direction, space, interpolate, ...)
52 | }
53 |
54 | #' @export
55 | #' @rdname plot.palettes_colour
56 | plot.palettes_palette <- function(
57 | x,
58 | n = NULL,
59 | direction = 1,
60 | space = "lab",
61 | interpolate = c("linear", "spline"),
62 | ...
63 | ) {
64 | plot_palette(x, n, direction, space, interpolate, ...)
65 | }
66 |
67 | plot_colour <- function(
68 | x,
69 | n = NULL,
70 | direction = 1,
71 | space = "lab",
72 | interpolate = c("linear", "spline")
73 | ) {
74 |
75 | x <- pal_ramp(x, n, direction, space, interpolate)
76 | x <- tibble::as_tibble(x)
77 | # The order of palettes in pal_palette() objects should be preserved.
78 | if ("palette" %in% colnames(x)) {
79 | x$palette <- factor(x$palette, levels = unique(x$palette))
80 | }
81 | # When the same colour is repeated in a pal_colour() or pal_palette() object
82 | # it needs a unique position identifier in order to be plotted in the same
83 | # order as it appears in the vector.
84 | x$position <- make.unique(x$colour)
85 | x$position <- factor(x$position, levels = x$position)
86 |
87 | ggplot2::ggplot(
88 | x,
89 | mapping = ggplot2::aes(
90 | x = .data$position,
91 | y = 0.5,
92 | fill = .data$colour
93 | )
94 | ) +
95 | ggplot2::geom_raster() +
96 | ggplot2::scale_fill_identity() +
97 | ggplot2::coord_cartesian(expand = FALSE) +
98 | ggplot2::theme_void() +
99 | ggplot2::theme(
100 | plot.margin = ggplot2::margin(0.5, 0.5, 0.5, 0.5, "lines"),
101 | panel.spacing = ggplot2::unit(1, "lines"),
102 | strip.text.x = ggplot2::element_text(
103 | family = "serif",
104 | size = 11,
105 | margin = ggplot2::margin(0, 0, 0.5, 0, "lines")
106 | )
107 | )
108 |
109 | }
110 |
111 | plot_palette <- function(
112 | x,
113 | n = NULL,
114 | direction = 1,
115 | space = "lab",
116 | interpolate = c("linear", "spline")
117 | ) {
118 |
119 | if (vec_size(x) == 1) {
120 |
121 | cols <- get_palette_colours(x)
122 | cols_length <- ifelse(is.null(n), vec_size(cols), n)
123 |
124 | plot_colour(x, n, direction, space, interpolate) +
125 | ggplot2::annotate(
126 | "rect",
127 | xmin = -Inf, xmax = Inf,
128 | ymin = 0.4, ymax = 0.6,
129 | fill = "white",
130 | alpha = 0.8
131 | ) +
132 | ggplot2::annotate(
133 | "text",
134 | x = (cols_length + 1) / 2,
135 | y = 0.5,
136 | hjust = "inward",
137 | vjust = "inward",
138 | label = vec_names(x),
139 | size = 10.55,
140 | family = "serif"
141 | )
142 |
143 | } else {
144 | plot_colour(x, n, direction, space, interpolate) +
145 | ggplot2::facet_wrap(~ palette, scales = "free") +
146 | # When there are a lot of palettes a smaller font size is needed to
147 | # prevent longer strip text from getting cut off.
148 | ggplot2::theme(
149 | strip.text.x = ggplot2::element_text(
150 | size = ifelse(vec_size(x) > 50, 9, 11)
151 | )
152 | )
153 | }
154 |
155 | }
156 |
--------------------------------------------------------------------------------
/R/pretty.R:
--------------------------------------------------------------------------------
1 | colour_symbol <- function(x) {
2 | style_box <- cli::make_ansi_style(x)
3 | style_box(pal_symbol[getOption("palettes.print_symbol")])
4 | }
5 |
6 | colour_format_symbol <- function(x, ...) {
7 | print_symbol <- !isFALSE(getOption("palettes.print_symbol"))
8 | print_hex <- isTRUE(getOption("palettes.print_hex"))
9 |
10 | out <- purrr::map_chr(
11 | x,
12 | function(.x) {
13 | colour_index <- !is.na(.x)
14 | if (print_hex && print_symbol) {
15 | ifelse(colour_index, paste(colour_symbol(.x), format(.x)), "")
16 | } else if (!print_hex && print_symbol) {
17 | ifelse(colour_index, colour_symbol(.x), "")
18 | } else if (print_hex && !print_symbol) {
19 | ifelse(colour_index, format(.x), "")
20 | } else {
21 | ifelse(colour_index, paste0("\"", .x, "\""), "NA")
22 | }
23 | }
24 | )
25 |
26 | out_width <- max(cli::ansi_nchar(out, type = "width"))
27 | cli::ansi_align(out, width = out_width, align = "right", type = "width")
28 | }
29 |
--------------------------------------------------------------------------------
/R/print.R:
--------------------------------------------------------------------------------
1 | #' @export
2 | obj_print_data.palettes_colour <- function(x, ...) {
3 | if (vec_is_empty(x)) return(invisible(x))
4 |
5 | print_sep <- getOption("palettes.print_sep")
6 | print_width <- getOption("palettes.print_width")
7 | print_index <- getOption("palettes.print_index")
8 |
9 | out <- colour_format_symbol(x)
10 | out_nchar <- max(cli::ansi_nchar(out, type = "width")) + nchar(print_sep)
11 | out_width <- print_width * out_nchar
12 |
13 | if (print_index) {
14 | index <- paste0("[", seq(from = 1, to = vec_size(x), by = print_width), "]")
15 | index_nchar <- max(nchar(index))
16 | index <- format(index, width = index_nchar, justify = "left")
17 | out_width <- out_width + index_nchar + 1
18 | } else {
19 | index <- NULL
20 | }
21 |
22 | # Formatting each row manually ensures that the the maximum number of colours
23 | # on a line and their indexes will always be correct; otherwise, when letting
24 | # cat() determine what's on a line, there are edge cases when there are NAs in
25 | # the colour vector or formatting is disabled where the maximum number of
26 | # colours on a line isn't respected. Likewise, although cli::ansi_columns()
27 | # performs a similar function to the code below, it has some quirks that make
28 | # it unsuitable here (such as putting a separator after the last item).
29 | out <- split(out, ceiling(seq_along(out)/print_width))
30 | out <- purrr::map_chr(out, paste0, collapse = print_sep)
31 | out[1:vec_size(out) - 1] <- purrr::map_chr(
32 | out[1:vec_size(out) - 1], function(.x) paste0(.x, print_sep)
33 | )
34 |
35 | cat(out, sep = "", fill = out_width, labels = index)
36 | invisible(x)
37 | }
38 |
39 | colour_print <- function(x) {
40 | obj_print(x)
41 | invisible(x)
42 | }
43 |
44 | #' @export
45 | print.palettes_colour <- function(x, ...) {
46 | colour_print(x)
47 | }
48 |
--------------------------------------------------------------------------------
/R/reexport.R:
--------------------------------------------------------------------------------
1 | #' @importFrom tibble as_tibble
2 | #' @export
3 | tibble::as_tibble
4 |
--------------------------------------------------------------------------------
/R/scales-colour-mapping.R:
--------------------------------------------------------------------------------
1 | #' Colour vector and colour palette mapping
2 | #'
3 | #' Conveniently maps data values (numeric or factor/character) to colours
4 | #' according to a given colour vector or colour palette.
5 | #'
6 | #' @details `pal_numeric` is a simple linear mapping from continuous numeric
7 | #' data to an interpolated palette.
8 | #'
9 | #' @param palette An object of class [`palettes_colour`][pal_colour()] or
10 | #' [`palettes_colour`][pal_colour()].
11 | #' @param domain The possible values that can be mapped.
12 | #'
13 | #' For `pal_numeric` and `pal_bin`, this can be a simple numeric
14 | #' range (e.g. `c(0, 100)`); `pal_quantile` needs representative
15 | #' numeric data; and `pal_factor` needs categorical data.
16 | #'
17 | #' If `NULL`, then whenever the resulting colour function is called, the
18 | #' `x` value will represent the domain. This implies that if the function
19 | #' is invoked multiple times, the encoding between values and colours may not
20 | #' be consistent; if consistency is needed, you must provide a non-`NULL`
21 | #' domain.
22 | #' @param na.color The colour to return for `NA` values. Note that
23 | #' `na.color = NA` is valid.
24 | #' @param alpha Whether alpha channels should be respected or ignored. If `TRUE`
25 | #' then colors without explicit alpha information will be treated as fully
26 | #' opaque.
27 | #' @param reverse Whether the colours in `palette` should be
28 | #' used in reverse order. For example, if the default order of a palette goes
29 | #' from blue to green, then `reverse = TRUE` will result in the colors going
30 | #' from green to blue.
31 | #' @return A function that takes a single parameter `x`; when called with a
32 | #' vector of numbers (except for `pal_factor`, which expects
33 | #' factors/characters), #RRGGBB colour strings are returned (unless
34 | #' `alpha = TRUE` in which case #RRGGBBAA may also be possible).
35 | #' @seealso [scales::col_numeric()]
36 | #' @export
37 | pal_numeric <- function(
38 | palette,
39 | domain,
40 | na.color = "#808080",
41 | alpha = FALSE,
42 | reverse = FALSE
43 | ) { # nocov start
44 |
45 | palette <- get_palette_colours(palette)
46 | palette <- as.character(palette)
47 |
48 | scales::col_numeric(
49 | palette = palette,
50 | domain = domain,
51 | na.color = na.color,
52 | alpha = alpha,
53 | reverse = reverse
54 | )
55 |
56 | } # nocov end
57 |
58 | #' @details `pal_bin` also maps continuous numeric data, but performs
59 | #' binning based on value (see the [base::cut()] function). `pal_bin`
60 | #' defaults for the `cut` function are `include.lowest = TRUE` and
61 | #' `right = FALSE`.
62 | #' @param bins Either a numeric vector of two or more unique cut points or a
63 | #' single number (greater than or equal to 2) giving the number of intervals
64 | #' into which the domain values are to be cut.
65 | #' @param pretty Whether to use the function [pretty()] to generate
66 | #' the bins when the argument `bins` is a single number. When
67 | #' `pretty = TRUE`, the actual number of bins may not be the number of
68 | #' bins you specified. When `pretty = FALSE`, [seq()] is used
69 | #' to generate the bins and the breaks may not be "pretty".
70 | #' @param right parameter supplied to [base::cut()]. See Details
71 | #' @seealso [scales::col_bin()]
72 | #' @rdname pal_numeric
73 | #' @export
74 | pal_bin <- function(
75 | palette,
76 | domain,
77 | bins = 7,
78 | pretty = TRUE,
79 | na.color = "#808080",
80 | alpha = FALSE,
81 | reverse = FALSE,
82 | right = FALSE
83 | ) { # nocov start
84 |
85 | palette <- get_palette_colours(palette)
86 | palette <- as.character(palette)
87 |
88 | scales::col_bin(
89 | palette = palette,
90 | domain = domain,
91 | bins = bins,
92 | pretty = pretty,
93 | na.color = na.color,
94 | alpha = alpha,
95 | reverse = reverse,
96 | right = right
97 | )
98 |
99 | } # nocov end
100 |
101 | #' @details `pal_quantile` similarly bins numeric data, but via the
102 | #' [stats::quantile()] function.
103 | #' @param n Number of equal-size quantiles desired. For more precise control,
104 | #' use the `probs` argument instead.
105 | #' @param probs See [stats::quantile()]. If provided, the `n`
106 | #' argument is ignored.
107 | #' @seealso [scales::col_quantile()]
108 | #' @rdname pal_numeric
109 | #' @export
110 | pal_quantile <- function(
111 | palette,
112 | domain,
113 | n = 4,
114 | probs = seq(0, 1, length.out = n + 1),
115 | na.color = "#808080",
116 | alpha = FALSE,
117 | reverse = FALSE,
118 | right = FALSE
119 | ) { # nocov start
120 |
121 | palette <- get_palette_colours(palette)
122 | palette <- as.character(palette)
123 |
124 | scales::col_quantile(
125 | palette = palette,
126 | domain = domain,
127 | na.color = na.color,
128 | alpha = alpha,
129 | reverse = reverse,
130 | right = right
131 | )
132 |
133 | } # nocov end
134 |
135 | #' @details `pal_factor` maps factors to colours. If the palette is
136 | #' discrete and has a different number of colours than the number of factors,
137 | #' interpolation is used.
138 | #' @param levels An alternate way of specifying levels; if specified, domain is
139 | #' ignored
140 | #' @param ordered If `TRUE` and `domain` needs to be coerced to a
141 | #' factor, treat it as already in the correct order
142 | #' @seealso [scales::col_factor()]
143 | #' @rdname pal_numeric
144 | #' @export
145 | pal_factor <- function(
146 | palette,
147 | domain,
148 | levels = NULL,
149 | ordered = FALSE,
150 | na.color = "#808080",
151 | alpha = FALSE,
152 | reverse = FALSE
153 | ) { # nocov start
154 |
155 | palette <- get_palette_colours(palette)
156 | palette <- as.character(palette)
157 |
158 | scales::col_factor(
159 | palette = palette,
160 | domain = domain,
161 | levels = levels,
162 | ordered = ordered,
163 | na.color = na.color,
164 | alpha = alpha,
165 | reverse = reverse
166 | )
167 |
168 | } # nocov end
169 |
170 | #' @examples
171 | #' pal <- pal_bin(met_palettes$Tam, domain = 0:100)
172 | #' plot(as_colour(pal(sort(runif(16, 0, 100)))))
173 | #'
174 | #' # Exponential distribution, mapped continuously
175 | #' pal <- pal_numeric(met_palettes$Tam, domain = NULL)
176 | #' plot(as_colour(pal(sort(rexp(16)))))
177 | #'
178 | #' # Exponential distribution, mapped by interval
179 | #' pal <- pal_bin(met_palettes$Tam, domain = NULL, bins = 4)
180 | #' plot(as_colour(pal(sort(rexp(16)))))
181 | #'
182 | #' # Exponential distribution, mapped by quantile
183 | #' pal <- pal_quantile(met_palettes$Tam, domain = NULL)
184 | #' plot(as_colour(pal(sort(rexp(16)))))
185 | #'
186 | #' # Categorical data; by default, the values being coloured span the gamut...
187 | #' pal <- pal_factor(met_palettes$Java, domain = NULL)
188 | #' plot(as_colour(pal(LETTERS[1:5])))
189 | #'
190 | #' # ...unless the data is a factor, without droplevels...
191 | #' pal <- pal_factor(met_palettes$Java, domain = NULL)
192 | #' plot(as_colour(pal(factor(LETTERS[1:5], levels = LETTERS))))
193 | #'
194 | #' # ...or the domain is stated explicitly.
195 | #' pal <- pal_factor(met_palettes$Java, domain = NULL, levels = LETTERS)
196 | #' plot(as_colour(pal(LETTERS[1:5])))
197 | #' @rdname pal_numeric
198 | #' @name pal_numeric
199 | NULL
200 |
--------------------------------------------------------------------------------
/R/symbol.R:
--------------------------------------------------------------------------------
1 | #' Symbols to use in colour previews
2 | #'
3 | #' List the symbols available to use in colour previews.
4 | #'
5 | #' @details
6 | #' By default, Unicode characters are used for symbols in colour previews in
7 | #' UTF-8 supported outputs. They automatically fall back to ASCII characters
8 | #' when the output does not support them.
9 | #'
10 | #' To change the symbol used for colour previews, set the `palettes.print_symbol`
11 | #' option to a symbol name listed in `list_colour_symbols()`.
12 | #' @return This function is called for its side effects and has no return value.
13 | #' @seealso [`help("palettes-options")`][palettes-options], [cli::is_utf8_output()]
14 | #' @export
15 | #'
16 | #' @examples
17 | #' list_colour_symbols()
18 | list_colour_symbols <- function() {
19 | width <- 50
20 | cat(
21 | cli::rule(
22 | left = "options(palettes.print_symbol = ...)",
23 | width = width + 10
24 | ),
25 | cli::ansi_columns(
26 | paste0(pal_symbol, "\t", names(pal_symbol)),
27 | width = width,
28 | fill = "cols",
29 | max_cols = 2,
30 | align = "left"
31 | ),
32 | sep = "\n"
33 | )
34 | }
35 |
36 | #' @export
37 | #' @rdname list_colour_symbols
38 | #' @usage NULL
39 | list_color_symbols <- list_colour_symbols
40 |
41 | symbol_utf8 <- list(
42 | "circle_small" = "\u2022",
43 | "circle" = "\u25CF",
44 | "circle_medium" = "\u26AB",
45 | "circle_large" = "\u2B24",
46 |
47 | "ellipse_horizontal" = "\u2B2C",
48 | "ellipse_vertical" = "\u2B2E",
49 |
50 | "triangle_up" = "\u25B2",
51 | "triangle_down" = "\u25BC",
52 | "triangle_left" = "\u25C0",
53 | "triangle_right" = "\u25B6",
54 |
55 | "square_small" = "\u25AA",
56 | "square" = "\u25A0",
57 | "square_medium" = "\u25FC",
58 | "square_large" = "\u2B1B",
59 |
60 | "block" = "\u2587",
61 | "block_full" = "\u2588",
62 |
63 | "diamond_small" = "\u2B29",
64 | "diamond" = "\u25C6",
65 | "diamond_medium" = "\u2B25",
66 |
67 | "pentagon" = "\u2B1F",
68 | "hexagon" = "\u2B22",
69 |
70 | "star" = "\u2605",
71 | "heart" = "\u2665",
72 | "smiley" = "\u263A",
73 | "moustache" = "\u0DF4"
74 | )
75 |
76 | symbol_ascii <- list(
77 | "circle_small" = "*",
78 | "circle" = "*",
79 | "circle_medium" = "*",
80 | "circle_large" = "*",
81 |
82 | "ellipse_horizontal" = "*",
83 | "ellipse_vertical" = "*",
84 |
85 | "triangle_up" = "^",
86 | "triangle_down" = "v",
87 | "triangle_left" = "<",
88 | "triangle_right" = ">",
89 |
90 | "square_small" = "[x]",
91 | "square" = "[x]",
92 | "square_medium" = "[x]",
93 | "square_large" = "[x]",
94 |
95 | "block" = "#",
96 | "block_full" = "#",
97 |
98 | "diamond_small" = "[+]",
99 | "diamond" = "[+]",
100 | "diamond_medium" = "[+]",
101 |
102 | "pentagon" = "*",
103 | "hexagon" = "*",
104 |
105 | "star" = "*",
106 | "heart" = "<3",
107 | "smiley" = ":)",
108 | "moustache" = "/\\/"
109 | )
110 |
--------------------------------------------------------------------------------
/R/sysdata.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/R/sysdata.rda
--------------------------------------------------------------------------------
/R/utils.R:
--------------------------------------------------------------------------------
1 | str_to_hex <- function(x) {
2 | print_alpha <- getOption("palettes.print_alpha")
3 | alpha <- NULL
4 | col_rgb <- farver::decode_colour(x, alpha = print_alpha)
5 | if (print_alpha) alpha <- col_rgb[,"alpha"]
6 | farver::encode_colour(col_rgb, alpha = alpha)
7 | }
8 |
9 | is_valid_colour <- function(x) {
10 | is_named_colour <- x %in% colour_names # From sysdata.rda
11 | is_hex_colour <- grepl(x, pattern = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$")
12 | is_na_colour <- is.na(x)
13 | is_valid <- is_named_colour | is_hex_colour | is_na_colour
14 | is_valid
15 | }
16 |
--------------------------------------------------------------------------------
/R/vctrs-arithmetic.R:
--------------------------------------------------------------------------------
1 | #' Mix colour vectors with arithmetic operators
2 | #'
3 | #' These binary operators mix colour vectors with arithmetic operators.
4 | #'
5 | #' @param e1,e2 Colour vectors of class [`palettes_colour`][pal_colour()].
6 | #' @return
7 | #' The binary operators return colour vectors of class
8 | #' [`palettes_colour`][pal_colour()] containing the result of the element by
9 | #' element operations. If involving a zero-length vector the result has length
10 | #' zero. Otherwise, the elements of shorter vectors are recycled as necessary.
11 | #' The `+` operator is for additive colour mixing.
12 | #' @name colour-mixing-arithmetic
13 | #' @aliases color-mixing-arithmetic
14 | #' @examples
15 | #' x <- pal_colour("red")
16 | #' y <- pal_colour("blue")
17 | #' x + y
18 | NULL
19 |
20 | #' @export
21 | #' @rdname colour-mixing-arithmetic
22 | "+.palettes_colour" <- function(e1, e2) {
23 | vec_arith("+", e1, e2)
24 | }
25 |
26 | #' @export
27 | #' @method vec_arith palettes_colour
28 | vec_arith.palettes_colour <- function(op, x, y, ...) {
29 | UseMethod("vec_arith.palettes_colour", y)
30 | }
31 |
32 | #' @export
33 | #' @method vec_arith.palettes_colour default
34 | vec_arith.palettes_colour.default <- function(op, x, y, ...) {
35 | stop_incompatible_op(op, x, y)
36 | }
37 |
38 | #' @export
39 | #' @method vec_arith.palettes_colour palettes_colour
40 | vec_arith.palettes_colour.palettes_colour <- function(op, x, y, ...) {
41 | switch(
42 | op,
43 | "+" = colour_plus(x, y),
44 | # TODO: Add other arithmetic operator methods
45 | # "-" = colour_minus(x, y),
46 | stop_incompatible_op(op, x, y)
47 | )
48 | }
49 |
50 | colour_plus <- function(x, y) {
51 | colour_arith(x, y, colour_plus_farver)
52 | }
53 | # colour_minus <- function(x, y) {
54 | # colour_arith(x, y, colour_minus_farver)
55 | # }
56 |
57 | colour_arith <- function(x, y, fn) {
58 | if (!is_colour(x) || !is_colour(y)) {
59 | abort("`x` and `y` must both be 'colour' objects.")
60 | }
61 |
62 | args <- vec_cast_common(x = x, y = y)
63 | args <- vec_recycle_common(!!!args)
64 | x <- args[[1]]
65 | y <- args[[2]]
66 |
67 | # TODO: Restore NAs in output. Adding to an NA should return NA.
68 | # TODO: Replace with purrr
69 | fields <- vapply(
70 | seq_along(x),
71 | function(i) {
72 | fn(x[[i]], y[[i]])
73 | },
74 | FUN.VALUE = character(1)
75 | )
76 |
77 | pal_colour(fields)
78 | }
79 |
80 | colour_plus_farver <- function(x, y) {
81 | pal_mix(c(x, y))
82 | }
83 |
--------------------------------------------------------------------------------
/R/vctrs-math.R:
--------------------------------------------------------------------------------
1 | #' Mix colour vectors with math functions
2 | #'
3 | #' These functions mix colour vectors with math functions.
4 | #'
5 | #' @param x An object of class [`palettes_colour`][pal_colour()].
6 | #' @param ... Colour vectors of class [`palettes_colour`][pal_colour()].
7 | #' @param na.rm Whether to include missing values. Either `TRUE` or `FALSE`.
8 | #' @return
9 | #' These functions return colour vectors of class
10 | #' [`palettes_colour`][pal_colour()]:
11 | #' - `sum()` returns the sum of all the colours present in its arguments with
12 | #' additive colour mixing.
13 | #' - `cumsum()` returns a vector whose elements are the cumulative sums of the
14 | #' elements of the argument with additive colour mixing.
15 | #' @name colour-mixing-math
16 | #' @aliases color-mixing-math
17 | #' @examples
18 | #' x <- pal_colour(c("red", "blue"))
19 | #' sum(x)
20 | #'
21 | #' x <- pal_colour(c("red", "blue", "yellow"))
22 | #' cumsum(x)
23 | NULL
24 |
25 | #' @export
26 | #' @rdname colour-mixing-math
27 | sum.palettes_colour <- function(..., na.rm = FALSE) {
28 | vec_math("sum", ..., na.rm)
29 | }
30 |
31 | #' @export
32 | #' @rdname colour-mixing-math
33 | cumsum.palettes_colour <- function(x) {
34 | vec_math("cumsum", x)
35 | }
36 |
37 | #' @export
38 | #' @method vec_math palettes_colour
39 | vec_math.palettes_colour <- function(.fn, .x, ...) {
40 | switch(
41 | .fn,
42 | "cumsum" = colour_cumsum(.x),
43 | "sum" = colour_sum(.x),
44 | NextMethod() # No `stop_unsupported` yet. See https://github.com/r-lib/vctrs/issues/1415
45 | )
46 | }
47 |
48 | colour_cumsum <- function(.x) {
49 | pal_colour(purrr::accumulate(.x, `+`))
50 | }
51 |
52 | colour_sum <- function(.x) {
53 | utils::tail(colour_cumsum(.x), n = 1)
54 | }
55 |
--------------------------------------------------------------------------------
/R/zzz.R:
--------------------------------------------------------------------------------
1 | ## nocov start
2 |
3 | package_env <- environment()
4 |
5 | .onLoad <- function(libname, pkgname) {
6 | op <- options()
7 | op.palettes <- list(
8 | palettes.print_symbol = "circle_small",
9 | palettes.print_hex = TRUE,
10 | palettes.print_alpha = FALSE,
11 | palettes.print_sep = "",
12 | palettes.print_width = 1,
13 | palettes.print_index = FALSE
14 | )
15 | toset <- !(names(op.palettes) %in% names(op))
16 | if (any(toset)) options(op.palettes[toset])
17 |
18 | makeActiveBinding(
19 | "pal_symbol",
20 | function() {
21 | if (cli::is_utf8_output()) {
22 | symbol_utf8
23 | } else {
24 | symbol_ascii
25 | }
26 | },
27 | package_env
28 | )
29 |
30 | invisible()
31 | }
32 |
33 | ## nocov end
34 |
--------------------------------------------------------------------------------
/README.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | output: github_document
3 | always_allow_html: true
4 | ---
5 |
6 |
7 |
8 | ```{r, include = FALSE, cache = FALSE}
9 | knitr::opts_chunk$set(
10 | collapse = TRUE,
11 | comment = "#>",
12 | fig.path = "man/figures/README-",
13 | out.width = "100%",
14 | cache = FALSE,
15 | asciicast_theme = "pkgdown"
16 | )
17 | asciicast::init_knitr_engine(
18 | startup = quote({
19 | library(tibble)
20 | library(palettes)
21 | set.seed(1) }),
22 | echo = TRUE,
23 | echo_input = FALSE
24 | )
25 | ```
26 |
27 | # palettes
28 |
29 |
30 | [](https://github.com/mccarthy-m-g/palettes/actions/workflows/R-CMD-check.yaml)
31 | [](https://app.codecov.io/gh/mccarthy-m-g/palettes?branch=main)
32 | [](https://CRAN.R-project.org/package=palettes)
33 | [](https://mccarthy-m-g.r-universe.dev/palettes)
34 | [](https://cran.r-project.org/package=palettes)
35 |
36 |
37 | palettes is an R package for working with colour vectors and colour palettes. There are three main goals to the palettes package, each described in a vignette:
38 |
39 | - To provide a new family of colour classes (`palettes_colour` and `palettes_palette`) that always print as hex codes with colour previews; `vignette("palettes")`.
40 |
41 | - To provide a comprehensive library of methods for working with colour vectors and colour palettes, including methods for ggplot2, `vignette("ggplot2")`; gt, `vignette("gt")`; biscale, `vignette("biscale")`; and other colour packages, `vignette("compatibility")`.
42 |
43 | - To make it easy for anyone to make their own colour palette package; `vignette("creating-packages")`. Colour palette packages made with palettes exist solely for the purpose of distributing colour palettes and get access to all the features of palettes for free.
44 |
45 | ## Installation
46 |
47 | Install palettes from [CRAN](https://CRAN.R-project.org/package=palettes) with:
48 |
49 | ```r
50 | install.packages("palettes")
51 | ```
52 |
53 | Install the development version from [GitHub](https://github.com/mccarthy-m-g/palettes) or [R-universe](https://mccarthy-m-g.r-universe.dev/palettes) with:
54 |
55 | ```r
56 | # Install from GitHub
57 | # install.packages("remotes")
58 | remotes::install_github("mccarthy-m-g/palettes")
59 |
60 | # Install from R-universe
61 | install.packages("palettes", repos = "https://mccarthy-m-g.r-universe.dev")
62 | ```
63 |
64 | Install the [WebAssembly](https://webassembly.org) version for use in [WebR](https://docs.r-wasm.org/webr/latest/) applications with:
65 |
66 | ```r
67 | # Install CRAN version from r-wasm
68 | install.packages("palettes", repos = "https://repo.r-wasm.org")
69 |
70 | # Install development version from R-universe
71 | install.packages(
72 | "palettes",
73 | repos = c("https://mccarthy-m-g.r-universe.dev", "https://repo.r-wasm.org")
74 | )
75 | ```
76 |
77 | ## Usage
78 |
79 | ```{r}
80 | library(palettes)
81 | ```
82 |
83 | Colours can be specified by name (as listed by `grDevices::colours()`):
84 |
85 | ```{asciicast pal-colour-character}
86 | pal_colour("red")
87 | ```
88 |
89 | Or by hex code:
90 |
91 | ```{asciicast pal-colour-hex}
92 | pal_colour("#FF0000")
93 | ```
94 |
95 | Multiple colours can be specified at once as a character vector:
96 |
97 | ```{asciicast pal-colour-vector}
98 | pal_colour(c("#a00e00", "#d04e00", "#f6c200", "#0086a8", "#132b69"))
99 | ```
100 |
101 | Named colour palettes can be specified in the same way:
102 |
103 | ```{asciicast pal-palette}
104 | pal_palette(
105 | egypt = c("#dd5129", "#0f7ba2", "#43b284", "#fab255"),
106 | java = c("#663171", "#cf3a36", "#ea7428", "#e2998a", "#0c7156")
107 | )
108 | ```
109 |
110 | Colours also print nicely in tibbles:
111 |
112 | ```{asciicast tibble}
113 | as_tibble(pal_colour(c("#dd5129", "#0f7ba2", "#43b284", "#fab255")))
114 | ```
115 |
116 | ## Documentation
117 |
118 | See at [`https://mccarthy-m-g.github.io/palettes/`](https://mccarthy-m-g.github.io/palettes/reference/index.html) and also in the installed package: `help(package = "palettes")`.
119 |
120 | ## License
121 |
122 | MIT © Michael McCarthy
123 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | # palettes
5 |
6 |
7 |
8 | [](https://github.com/mccarthy-m-g/palettes/actions/workflows/R-CMD-check.yaml)
9 | [](https://app.codecov.io/gh/mccarthy-m-g/palettes?branch=main)
11 | [](https://CRAN.R-project.org/package=palettes)
13 | [](https://mccarthy-m-g.r-universe.dev/palettes)
15 | [](https://cran.r-project.org/package=palettes)
17 |
18 |
19 | palettes is an R package for working with colour vectors and colour
20 | palettes. There are three main goals to the palettes package, each
21 | described in a vignette:
22 |
23 | - To provide a new family of colour classes (`palettes_colour` and
24 | `palettes_palette`) that always print as hex codes with colour
25 | previews; `vignette("palettes")`.
26 |
27 | - To provide a comprehensive library of methods for working with
28 | colour vectors and colour palettes, including methods for ggplot2,
29 | `vignette("ggplot2")`; gt, `vignette("gt")`; biscale,
30 | `vignette("biscale")`; and other colour packages,
31 | `vignette("compatibility")`.
32 |
33 | - To make it easy for anyone to make their own colour palette package;
34 | `vignette("creating-packages")`. Colour palette packages made with
35 | palettes exist solely for the purpose of distributing colour
36 | palettes and get access to all the features of palettes for free.
37 |
38 | ## Installation
39 |
40 | Install palettes from
41 | [CRAN](https://CRAN.R-project.org/package=palettes) with:
42 |
43 | ``` r
44 | install.packages("palettes")
45 | ```
46 |
47 | Install the development version from
48 | [GitHub](https://github.com/mccarthy-m-g/palettes) or
49 | [R-universe](https://mccarthy-m-g.r-universe.dev/palettes) with:
50 |
51 | ``` r
52 | # Install from GitHub
53 | # install.packages("remotes")
54 | remotes::install_github("mccarthy-m-g/palettes")
55 |
56 | # Install from R-universe
57 | install.packages("palettes", repos = "https://mccarthy-m-g.r-universe.dev")
58 | ```
59 |
60 | Install the [WebAssembly](https://webassembly.org) version for use in
61 | [WebR](https://docs.r-wasm.org/webr/latest/) applications with:
62 |
63 | ``` r
64 | # Install CRAN version from r-wasm
65 | install.packages("palettes", repos = "https://repo.r-wasm.org")
66 |
67 | # Install development version from R-universe
68 | install.packages(
69 | "palettes",
70 | repos = c("https://mccarthy-m-g.r-universe.dev", "https://repo.r-wasm.org")
71 | )
72 | ```
73 |
74 | ## Usage
75 |
76 | ``` r
77 | library(palettes)
78 | ```
79 |
80 | Colours can be specified by name (as listed by `grDevices::colours()`):
81 |
82 | ``` r
83 | pal_colour("red")
84 | ```
85 |
86 |
87 |
88 | Or by hex code:
89 |
90 | ``` r
91 | pal_colour("#FF0000")
92 | ```
93 |
94 |
95 |
96 | Multiple colours can be specified at once as a character vector:
97 |
98 | ``` r
99 | pal_colour(c("#a00e00", "#d04e00", "#f6c200", "#0086a8", "#132b69"))
100 | ```
101 |
102 |
103 |
104 | Named colour palettes can be specified in the same way:
105 |
106 | ``` r
107 | pal_palette(
108 | egypt = c("#dd5129", "#0f7ba2", "#43b284", "#fab255"),
109 | java = c("#663171", "#cf3a36", "#ea7428", "#e2998a", "#0c7156")
110 | )
111 | ```
112 |
113 |
114 |
115 | Colours also print nicely in tibbles:
116 |
117 | ``` r
118 | as_tibble(pal_colour(c("#dd5129", "#0f7ba2", "#43b284", "#fab255")))
119 | ```
120 |
121 |
122 |
123 | ## Documentation
124 |
125 | See at
126 | [`https://mccarthy-m-g.github.io/palettes/`](https://mccarthy-m-g.github.io/palettes/reference/index.html)
127 | and also in the installed package: `help(package = "palettes")`.
128 |
129 | ## License
130 |
131 | MIT © Michael McCarthy
132 |
--------------------------------------------------------------------------------
/codecov.yml:
--------------------------------------------------------------------------------
1 | comment: false
2 |
3 | coverage:
4 | status:
5 | project:
6 | default:
7 | target: auto
8 | threshold: 1%
9 | informational: true
10 | patch:
11 | default:
12 | target: auto
13 | threshold: 1%
14 | informational: true
15 |
--------------------------------------------------------------------------------
/cran-comments.md:
--------------------------------------------------------------------------------
1 | ## R CMD check results
2 |
3 | 0 errors | 0 warnings | 0 notes
4 |
5 | ## Reverse dependencies
6 |
7 | None.
8 |
--------------------------------------------------------------------------------
/data-raw/README.md:
--------------------------------------------------------------------------------
1 | ## Package Listings
2 |
3 | If you have a colour palette package that you would like included in the listing of [packages made with palettes](https://mccarthy-m-g.github.io/palettes/articles/made-with-palettes.html), please send me a pull request that includes an addition to the `made-with-palettes.R` file in this directory.
4 |
5 | ### Requirements
6 |
7 | Colour palette packages submitted for listing should meet the following requirements:
8 |
9 | 1. Hosted from a GitHub repository.
10 | 2. Made using the palettes package.
11 |
12 | ### Instructions
13 |
14 | In your pull request, add the package info to the the `made_with_palettes <- tibble::tribble(...)` on line 8. The columns in the tribble are as follows:
15 |
16 | - `package`: R package name.
17 | - `gh_owner`: The account owner of the repository.
18 | - `gh_repo`: The name of the repository.
19 |
20 | Please add new entries in alphabetical order, according to the package name.
21 |
--------------------------------------------------------------------------------
/data-raw/colour-names.R:
--------------------------------------------------------------------------------
1 | colour_names <- grDevices::colors()
2 |
3 | saveRDS(colour_names, "data-raw/sysdata/colour_names.rds")
4 |
--------------------------------------------------------------------------------
/data-raw/included-palettes.R:
--------------------------------------------------------------------------------
1 | ## code to prepare `DATASET` dataset goes here
2 | load_all()
3 | library(purrr)
4 |
5 | # Cartography palettes ----
6 | discrete_palettes <- pal_palette(
7 | Antique = c("#855C75", "#D9AF6B", "#AF6458", "#736F4C", "#526A83", "#625377", "#68855C", "#9C9C5E", "#A06177", "#8C785D", "#467378", "#7C7C7C"),
8 | Bold = c("#7F3C8D", "#11A579", "#3969AC", "#F2B701", "#E73F74", "#80BA5A", "#E68310", "#008695", "#CF1C90", "#f97b72", "#4b4b8f", "#A5AA99"),
9 | Pastel = c("#66C5CC", "#F6CF71", "#F89C74", "#DCB0F2", "#87C55F", "#9EB9F3", "#FE88B1", "#C9DB74", "#8BE0A4", "#B497E7", "#D3B484", "#B3B3B3"),
10 | Prism = c("#5F4690", "#1D6996", "#38A6A5", "#0F8554", "#73AF48", "#EDAD08", "#E17C05", "#CC503E", "#94346E", "#6F4070", "#994E95", "#666666"),
11 | Safe = c("#88CCEE", "#CC6677", "#DDCC77", "#117733", "#332288", "#AA4499", "#44AA99", "#999933", "#882255", "#661100", "#6699CC", "#888888"),
12 | Vivid = c("#E58606", "#5D69B1", "#52BCA3", "#99C945", "#CC61B0", "#24796C", "#DAA51B", "#2F8AC4", "#764E9F", "#ED645A", "#CC3A8E", "#A5AA99"),
13 | )
14 |
15 | sequential_palettes <- pal_palette(
16 | Burg = c("#ffc6c4", "#f4a3a8", "#e38191", "#cc607d", "#ad466c", "#8b3058", "#672044"),
17 | BurgYl = c("#fbe6c5", "#f5ba98", "#ee8a82", "#dc7176", "#c8586c", "#9c3f5d", "#70284a"),
18 | RedOr = c("#f6d2a9", "#f5b78e", "#f19c7c", "#ea8171", "#dd686c", "#ca5268", "#b13f64"),
19 | OrYel = c("#ecda9a", "#efc47e", "#f3ad6a", "#f7945d", "#f97b57", "#f66356", "#ee4d5a"),
20 | Peach = c("#fde0c5", "#facba6", "#f8b58b", "#f59e72", "#f2855d", "#ef6a4c", "#eb4a40"),
21 | PinkYl = c("#fef6b5", "#ffdd9a", "#ffc285", "#ffa679", "#fa8a76", "#f16d7a", "#e15383"),
22 | Mint = c("#e4f1e1", "#b4d9cc", "#89c0b6", "#63a6a0", "#448c8a", "#287274", "#0d585f"),
23 | BluGrn = c("#c4e6c3", "#96d2a4", "#6dbc90", "#4da284", "#36877a", "#266b6e", "#1d4f60"),
24 | DarkMint = c("#d2fbd4", "#a5dbc2", "#7bbcb0", "#559c9e", "#3a7c89", "#235d72", "#123f5a"),
25 | Emrld = c("#d3f2a3", "#97e196", "#6cc08b", "#4c9b82", "#217a79", "#105965", "#074050"),
26 | BluYl = c("#f7feae", "#b7e6a5", "#7ccba2", "#46aea0", "#089099", "#00718b", "#045275"),
27 | Teal = c("#d1eeea", "#a8dbd9", "#85c4c9", "#68abb8", "#4f90a6", "#3b738f", "#2a5674"),
28 | TealGrn = c("#b0f2bc", "#89e8ac", "#67dba5", "#4cc8a3", "#38b2a3", "#2c98a0", "#257d98"),
29 | Purp = c("#f3e0f7", "#e4c7f1", "#d1afe8", "#b998dd", "#9f82ce", "#826dba", "#63589f"),
30 | PurpOr = c("#f9ddda", "#f2b9c4", "#e597b9", "#ce78b3", "#ad5fad", "#834ba0", "#573b88"),
31 | Sunset = c("#f3e79b", "#fac484", "#f8a07e", "#eb7f86", "#ce6693", "#a059a0", "#5c53a5"),
32 | Magenta = c("#f3cbd3", "#eaa9bd", "#dd88ac", "#ca699d", "#b14d8e", "#91357d", "#6c2167"),
33 | SunsetDark = c("#fcde9c", "#faa476", "#f0746e", "#e34f6f", "#dc3977", "#b9257a", "#7c1d6f"),
34 | BrwnYl = c("#ede5cf", "#e0c2a2", "#d39c83", "#c1766f", "#a65461", "#813753", "#541f3f")
35 | )
36 |
37 | diverging_palettes <- pal_palette(
38 | ArmyRose = c("#798234", "#a3ad62", "#d0d3a2", "#fdfbe4", "#f0c6c3", "#df91a3", "#d46780"),
39 | Fall = c("#3d5941", "#778868", "#b5b991", "#f6edbd", "#edbb8a", "#de8a5a", "#ca562c"),
40 | Geyser = c("#008080", "#70a494", "#b4c8a8", "#f6edbd", "#edbb8a", "#de8a5a", "#ca562c"),
41 | Temps = c("#009392", "#39b185", "#9ccb86", "#e9e29c", "#eeb479", "#e88471", "#cf597e"),
42 | TealRose = c("#009392", "#72aaa1", "#b1c7b3", "#f1eac8", "#e5b9ad", "#d98994", "#d0587e"),
43 | Tropic = c("#009B9E", "#42B7B9", "#A7D3D4", "#F1F1F1", "#E4C1D9", "#D691C1", "#C75DAB"),
44 | Earth = c("#A16928", "#bd925a", "#d6bd8d", "#edeac2", "#b5c8b8", "#79a7ac", "#2887a1"),
45 | )
46 |
47 | usethis::use_data(discrete_palettes, overwrite = TRUE)
48 | usethis::use_data(sequential_palettes, overwrite = TRUE)
49 | usethis::use_data(diverging_palettes, overwrite = TRUE)
50 |
51 | # performance palettes ----
52 | performance_palettes <- pal_palette(
53 | dark = c("#3AAF85", "#1B6CA8", "#CD201F")
54 | )
55 |
56 | usethis::use_data(performance_palettes, overwrite = TRUE)
57 |
58 | # MetBrewer palettes ----
59 | met_palettes <- MetBrewer::MetPalettes |>
60 | map(1) |>
61 | map(as_colour) |>
62 | new_palette()
63 |
64 | met_palettes_a11y <- met_palettes[
65 | names(met_palettes) %in% MetBrewer::colorblind_palettes
66 | ]
67 |
68 | usethis::use_data(met_palettes, overwrite = TRUE)
69 | usethis::use_data(met_palettes_a11y, overwrite = TRUE)
70 |
71 | # Penguin palettes ----
72 | penguin_palettes <- pal_palette(
73 | dark = c("#F37A00", "#6A3D9A", "#33A02C"),
74 | medium = c("#F89D38", "#9A78B8", "#73C05B"),
75 | light = c("#FDBF6F", "#CAB2D6", "#B2DF8A"),
76 | adelie = c("#F37A00", "#F89D38", "#FDBF6F"),
77 | chinstrap = c("#6A3D9A", "#9A78B8", "#CAB2D6"),
78 | gentoo = c("#33A02C", "#73C05B", "#B2DF8A")
79 | )
80 |
81 | usethis::use_data(penguin_palettes, overwrite = TRUE)
82 |
83 | # nord palettes ----
84 | nord_palettes <- nord::nord_palettes |>
85 | map(as_colour) |>
86 | new_palette()
87 |
88 | usethis::use_data(nord_palettes, overwrite = TRUE)
89 |
90 | # PNWColors palettes ----
91 | pnw_palettes <- PNWColors::pnw_palettes |>
92 | map(function(x) as_colour(x[1,])) |>
93 | new_palette()
94 |
95 | usethis::use_data(pnw_palettes, overwrite = TRUE)
96 |
97 | # viridisLite palettes ----
98 | n_colours <- 27
99 |
100 | viridis_palettes <- pal_palette(
101 | viridis = viridisLite::viridis(n_colours),
102 | magma = viridisLite::magma(n_colours),
103 | inferno = viridisLite::inferno(n_colours),
104 | plasma = viridisLite::plasma(n_colours),
105 | cividis = viridisLite::cividis(n_colours),
106 | rocket = viridisLite::rocket(n_colours),
107 | mako = viridisLite::mako(n_colours),
108 | turbo = viridisLite::turbo(n_colours)
109 | )
110 |
111 | usethis::use_data(viridis_palettes, overwrite = TRUE)
112 |
--------------------------------------------------------------------------------
/data-raw/made-with-palettes.R:
--------------------------------------------------------------------------------
1 | library(dplyr)
2 | library(purrr)
3 | library(gh)
4 | library(desc)
5 | library(readr)
6 | library(knitr)
7 |
8 | # Please add new entries in alphabetical order, according to the package name.
9 | made_with_palettes <- tibble::tribble(
10 | ~package, ~gh_owner, ~gh_repo,
11 | "AnVILplot", "fhdsl", "AnVILplot",
12 | "CCMHr", "CCMH-PSU", "CCMHr",
13 | "cubepalette", "zerogetsamgow", "cubepalette",
14 | "dohactheme", "zerogetsamgow", "dohactheme",
15 | "hutchplot", "fhdsl", "hutchplot",
16 | "icampcolors", "jcunha2023", "icampcolors",
17 | "palettes", "mccarthy-m-g", "palettes",
18 | "warwickplots", "Warwick-Stats-Resources", "warwickplots"
19 | )
20 |
21 | cran_pkgs <- tools::CRAN_package_db()$Package
22 |
23 | made_with_palettes <- made_with_palettes |>
24 | rowwise() |>
25 | mutate(
26 | # Check whether package is also on CRAN
27 | on_cran = package %in% cran_pkgs,
28 | # Get and read DESCRIPTION from package GitHub repositories
29 | pkg_description = list(gh::gh(
30 | "/repos/:owner/:repo/contents/:path",
31 | owner = gh_owner,
32 | repo = gh_repo,
33 | path = "DESCRIPTION",
34 | .accept = "application/vnd.github.raw+json"
35 | )),
36 | pkg_description = purrr::pluck(pkg_description, 1),
37 | pkg_description = list(desc(text = pkg_description)),
38 | # Get and format fields from DESCRIPTION
39 | title = pkg_description$get("Title"),
40 | authors = as.character(combine_words(format(
41 | pkg_description$get_author("aut"),
42 | include = c("given", "family")
43 | ))),
44 | # Get URLs
45 | # url = pkg_description$get_field("URL", default = NA),
46 | gh_url = paste0("https://github.com/", gh_owner, "/", gh_repo, ""),
47 | cran_url = ifelse(
48 | on_cran, paste0("https://CRAN.R-project.org/package=", package), NA
49 | )
50 | ) |>
51 | select(-c(gh_owner:pkg_description)) |>
52 | arrange(package)
53 |
54 | saveRDS(made_with_palettes, "data-raw/sysdata/made-with-palettes.rds")
55 |
--------------------------------------------------------------------------------
/data-raw/sysdata.R:
--------------------------------------------------------------------------------
1 | colour_names <- readRDS("data-raw/sysdata/colour_names.rds")
2 | made_with_palettes<- readRDS("data-raw/sysdata/made-with-palettes.rds")
3 |
4 | usethis::use_data(
5 | colour_names,
6 | made_with_palettes,
7 | overwrite = TRUE,
8 | internal = TRUE
9 | )
10 |
--------------------------------------------------------------------------------
/data-raw/sysdata/colour_names.rds:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data-raw/sysdata/colour_names.rds
--------------------------------------------------------------------------------
/data-raw/sysdata/made-with-palettes.rds:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data-raw/sysdata/made-with-palettes.rds
--------------------------------------------------------------------------------
/data/discrete_palettes.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data/discrete_palettes.rda
--------------------------------------------------------------------------------
/data/diverging_palettes.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data/diverging_palettes.rda
--------------------------------------------------------------------------------
/data/met_palettes.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data/met_palettes.rda
--------------------------------------------------------------------------------
/data/met_palettes_a11y.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data/met_palettes_a11y.rda
--------------------------------------------------------------------------------
/data/nord_palettes.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data/nord_palettes.rda
--------------------------------------------------------------------------------
/data/penguin_palettes.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data/penguin_palettes.rda
--------------------------------------------------------------------------------
/data/performance_palettes.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data/performance_palettes.rda
--------------------------------------------------------------------------------
/data/pnw_palettes.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data/pnw_palettes.rda
--------------------------------------------------------------------------------
/data/sequential_palettes.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data/sequential_palettes.rda
--------------------------------------------------------------------------------
/data/viridis_palettes.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/data/viridis_palettes.rda
--------------------------------------------------------------------------------
/inst/WORDLIST:
--------------------------------------------------------------------------------
1 | CIE
2 | CMD
3 | Codecov
4 | Colourblind
5 | Kaupp
6 | Lawlor
7 | Lawlor's
8 | Lch
9 | Nord
10 | PNW
11 | RRGGBB
12 | RRGGBBAA
13 | Viridis
14 | asciicast
15 | biscale
16 | colors
17 | colorspace
18 | colourblind
19 | com
20 | devtools
21 | farver
22 | ggplot
23 | github
24 | grDevices
25 | gt
26 | iest
27 | ing
28 | knitr
29 | math
30 | mccarthy
31 | oklab
32 | pkgdown
33 | polarLAB
34 | polarLuv
35 | protanopia
36 | rlang
37 | roxygen
38 | rrggbb
39 | tibble
40 | tibbles
41 | tritanopia
42 | uv
43 | vctrs
44 | xyY
45 |
--------------------------------------------------------------------------------
/man/as_tibble.palettes_colour.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/as_tibble.R
3 | \name{as_tibble.palettes_colour}
4 | \alias{as_tibble.palettes_colour}
5 | \alias{as_tibble.palettes_palette}
6 | \title{Cast colour vectors and colour palettes to tibbles}
7 | \usage{
8 | \method{as_tibble}{palettes_colour}(x, ...)
9 |
10 | \method{as_tibble}{palettes_palette}(x, ...)
11 | }
12 | \arguments{
13 | \item{x}{An object of class \code{\link[=pal_palette]{palettes_palette}} or
14 | \code{\link[=pal_colour]{palettes_colour}}.}
15 |
16 | \item{...}{Not used.}
17 | }
18 | \value{
19 | A \link[tibble:tibble-package]{tibble}. The output has the following properties:
20 | \itemize{
21 | \item For objects of class \code{\link[=pal_colour]{palettes_colour}}: A tibble with
22 | column \code{colour} containing the colour vector.
23 | \item For objects of class \code{\link[=pal_palette]{palettes_palette}}: A tibble with
24 | columns \code{palette} and \code{colour} containing palette names and colour vectors.
25 | }
26 | }
27 | \description{
28 | \code{as_tibble()} turns an existing colour vector or colour palette into a
29 | so-called \link[tibble:tibble-package]{tibble}, a data frame with class \code{tbl_df}.
30 | }
31 | \examples{
32 | x <- pal_colour(c("#663171", "#EA7428", "#0C7156"))
33 | as_tibble(x)
34 |
35 | y <- pal_palette(
36 | Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"),
37 | Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156")
38 | )
39 | as_tibble(y)
40 | }
41 | \seealso{
42 | \code{\link[=pal_colour]{pal_colour()}}, \code{\link[=pal_palette]{pal_palette()}}
43 | }
44 |
--------------------------------------------------------------------------------
/man/carto_palettes.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \docType{data}
4 | \name{carto_palettes}
5 | \alias{carto_palettes}
6 | \alias{discrete_palettes,}
7 | \alias{sequential_palettes,}
8 | \alias{diverging_palettes}
9 | \alias{discrete_palettes}
10 | \alias{sequential_palettes}
11 | \title{Cartography palettes}
12 | \format{
13 | \subsection{\code{discrete_palettes}}{
14 |
15 | An object of class \code{palettes_palette} with 7
16 | colour palettes. Use \code{names(discrete_palettes)} to return all palette names.
17 | }
18 |
19 | \subsection{\code{sequential_palettes}}{
20 |
21 | An object of class \code{palettes_palette} with 19
22 | colour palettes. Use \code{names(sequential_palettes)} to return all palette names.
23 | }
24 |
25 | \subsection{\code{diverging_palettes}}{
26 |
27 | An object of class \code{palettes_palette} with 7
28 | colour palettes. Use \code{names(diverging_palettes)} to return all palette names.
29 | }
30 | }
31 | \source{
32 | \url{https://github.com/CartoDB/CartoColor/}
33 | }
34 | \usage{
35 | discrete_palettes
36 |
37 | sequential_palettes
38 |
39 | diverging_palettes
40 | }
41 | \description{
42 | Discrete, sequential, and diverging palettes created by cartographers.
43 | }
44 | \examples{
45 | # Get all palettes by name.
46 | names(discrete_palettes)
47 | names(sequential_palettes)
48 | names(diverging_palettes)
49 |
50 | # Plot all palettes.
51 | plot(discrete_palettes)
52 | plot(sequential_palettes)
53 | plot(diverging_palettes)
54 | }
55 | \seealso{
56 | \code{\link[=pal_palette]{pal_palette()}}, \code{\link[=pal_colour]{pal_colour()}}
57 | }
58 | \keyword{datasets}
59 |
--------------------------------------------------------------------------------
/man/colour-mixing-arithmetic.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/vctrs-arithmetic.R
3 | \name{colour-mixing-arithmetic}
4 | \alias{colour-mixing-arithmetic}
5 | \alias{color-mixing-arithmetic}
6 | \alias{+.palettes_colour}
7 | \title{Mix colour vectors with arithmetic operators}
8 | \usage{
9 | \method{+}{palettes_colour}(e1, e2)
10 | }
11 | \arguments{
12 | \item{e1, e2}{Colour vectors of class \code{\link[=pal_colour]{palettes_colour}}.}
13 | }
14 | \value{
15 | The binary operators return colour vectors of class
16 | \code{\link[=pal_colour]{palettes_colour}} containing the result of the element by
17 | element operations. If involving a zero-length vector the result has length
18 | zero. Otherwise, the elements of shorter vectors are recycled as necessary.
19 | The \code{+} operator is for additive colour mixing.
20 | }
21 | \description{
22 | These binary operators mix colour vectors with arithmetic operators.
23 | }
24 | \examples{
25 | x <- pal_colour("red")
26 | y <- pal_colour("blue")
27 | x + y
28 | }
29 |
--------------------------------------------------------------------------------
/man/colour-mixing-math.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/vctrs-math.R
3 | \name{colour-mixing-math}
4 | \alias{colour-mixing-math}
5 | \alias{color-mixing-math}
6 | \alias{sum.palettes_colour}
7 | \alias{cumsum.palettes_colour}
8 | \title{Mix colour vectors with math functions}
9 | \usage{
10 | \method{sum}{palettes_colour}(..., na.rm = FALSE)
11 |
12 | \method{cumsum}{palettes_colour}(x)
13 | }
14 | \arguments{
15 | \item{...}{Colour vectors of class \code{\link[=pal_colour]{palettes_colour}}.}
16 |
17 | \item{na.rm}{Whether to include missing values. Either \code{TRUE} or \code{FALSE}.}
18 |
19 | \item{x}{An object of class \code{\link[=pal_colour]{palettes_colour}}.}
20 | }
21 | \value{
22 | These functions return colour vectors of class
23 | \code{\link[=pal_colour]{palettes_colour}}:
24 | \itemize{
25 | \item \code{sum()} returns the sum of all the colours present in its arguments with
26 | additive colour mixing.
27 | \item \code{cumsum()} returns a vector whose elements are the cumulative sums of the
28 | elements of the argument with additive colour mixing.
29 | }
30 | }
31 | \description{
32 | These functions mix colour vectors with math functions.
33 | }
34 | \examples{
35 | x <- pal_colour(c("red", "blue"))
36 | sum(x)
37 |
38 | x <- pal_colour(c("red", "blue", "yellow"))
39 | cumsum(x)
40 | }
41 |
--------------------------------------------------------------------------------
/man/figures/README-/pal-colour-character.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/man/figures/README-/pal-colour-hex.svg:
--------------------------------------------------------------------------------
1 | <palettes_colour[1]>•#FF0000
--------------------------------------------------------------------------------
/man/figures/README-/pal-colour-vector.svg:
--------------------------------------------------------------------------------
1 | <palettes_colour[5]>•#A00E00•#D04E00•#F6C200•#0086A8•#132B69
--------------------------------------------------------------------------------
/man/figures/README-/pal-palette.svg:
--------------------------------------------------------------------------------
1 | <palettes_palette[2]>$egypt<palettes_colour[4]>•#DD5129•#0F7BA2•#43B284•#FAB255$java<palettes_colour[5]>•#663171•#CF3A36•#EA7428•#E2998A•#0C7156
--------------------------------------------------------------------------------
/man/figures/README-/tibble.svg:
--------------------------------------------------------------------------------
1 | #Atibble:4×1colour<colour>1•#DD51292•#0F7BA23•#43B2844•#FAB255
--------------------------------------------------------------------------------
/man/figures/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/man/figures/logo.png
--------------------------------------------------------------------------------
/man/list_colour_symbols.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/symbol.R
3 | \name{list_colour_symbols}
4 | \alias{list_colour_symbols}
5 | \alias{list_color_symbols}
6 | \title{Symbols to use in colour previews}
7 | \usage{
8 | list_colour_symbols()
9 | }
10 | \value{
11 | This function is called for its side effects and has no return value.
12 | }
13 | \description{
14 | List the symbols available to use in colour previews.
15 | }
16 | \details{
17 | By default, Unicode characters are used for symbols in colour previews in
18 | UTF-8 supported outputs. They automatically fall back to ASCII characters
19 | when the output does not support them.
20 |
21 | To change the symbol used for colour previews, set the \code{palettes.print_symbol}
22 | option to a symbol name listed in \code{list_colour_symbols()}.
23 | }
24 | \examples{
25 | list_colour_symbols()
26 | }
27 | \seealso{
28 | \code{\link[=palettes-options]{help("palettes-options")}}, \code{\link[cli:is_utf8_output]{cli::is_utf8_output()}}
29 | }
30 |
--------------------------------------------------------------------------------
/man/met_palettes.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \docType{data}
4 | \name{met_palettes}
5 | \alias{met_palettes}
6 | \alias{met_palettes_a11y}
7 | \title{Metropolitan Museum of Art palettes}
8 | \format{
9 | \subsection{\code{met_palettes}}{
10 |
11 | An object of class \code{palettes_palette} with 56 colour
12 | palettes. Use \code{names(met_palettes)} to return all palette names.
13 | }
14 |
15 | \subsection{\code{met_palettes_a11y}}{
16 |
17 | An object of class \code{palettes_palette} limited to
18 | 24 colourblind accessible palettes. All colours
19 | in each palette are distinguishable with deuteranopia, protanopia, and
20 | tritanopia. Use \code{names(met_palettes_a11y)} to return all palette names.
21 | }
22 | }
23 | \source{
24 | \url{https://github.com/BlakeRMills/MetBrewer}
25 | }
26 | \usage{
27 | met_palettes
28 |
29 | met_palettes_a11y
30 | }
31 | \description{
32 | Palettes inspired by works at the Metropolitan Museum of Art in New York.
33 | Pieces selected come from various time periods, regions, and mediums.
34 | }
35 | \examples{
36 | # Get all palettes by name.
37 | names(met_palettes)
38 |
39 | # Plot all palettes.
40 | plot(met_palettes)
41 | }
42 | \seealso{
43 | \code{\link[=pal_palette]{pal_palette()}}, \code{\link[=pal_colour]{pal_colour()}}, \code{\link[MetBrewer:met.brewer]{MetBrewer::met.brewer()}}
44 | }
45 | \author{
46 | \href{https://github.com/BlakeRMills}{Blake Robert Mills}
47 | }
48 | \keyword{datasets}
49 |
--------------------------------------------------------------------------------
/man/nord_palettes.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \docType{data}
4 | \name{nord_palettes}
5 | \alias{nord_palettes}
6 | \title{Nord palettes}
7 | \format{
8 | \subsection{\code{nord_palettes}}{
9 |
10 | An object of class \code{palettes_palette} with 16
11 | colour palettes. Use \code{names(nord_palettes)} to return all palette names.
12 | }
13 | }
14 | \source{
15 | \url{https://github.com/jkaupp/nord}
16 | }
17 | \usage{
18 | nord_palettes
19 | }
20 | \description{
21 | Dimmed pastel palettes inspired by the Arctic and Canadian wilderness.
22 | }
23 | \examples{
24 | # Get all palettes by name.
25 | names(nord_palettes)
26 |
27 | # Plot all palettes.
28 | plot(nord_palettes)
29 | }
30 | \seealso{
31 | \code{\link[=pal_palette]{pal_palette()}}, \code{\link[=pal_colour]{pal_colour()}}, \code{\link[nord:nord]{nord::nord()}}
32 | }
33 | \author{
34 | \href{https://github.com/jkaupp}{Jake Kaupp}
35 | }
36 | \keyword{datasets}
37 |
--------------------------------------------------------------------------------
/man/pal_colour.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/colour.R
3 | \name{pal_colour}
4 | \alias{pal_colour}
5 | \alias{is_colour}
6 | \alias{as_colour}
7 | \alias{as_colour.default}
8 | \alias{as_colour.palettes_palette}
9 | \alias{pal_color}
10 | \alias{is_color}
11 | \alias{as_color}
12 | \title{Colour vectors}
13 | \usage{
14 | pal_colour(x = character())
15 |
16 | is_colour(x)
17 |
18 | as_colour(x)
19 |
20 | \method{as_colour}{default}(x)
21 |
22 | \method{as_colour}{palettes_palette}(x)
23 | }
24 | \arguments{
25 | \item{x}{\itemize{
26 | \item For \code{pal_colour()}: A character vector of any of the three kinds of R colour
27 | specifications.
28 | \item For \code{as_colour()}: An object to be coerced.
29 | \item For \code{is_colour()}: An object to test.
30 | }}
31 | }
32 | \value{
33 | An S3 vector of class \code{palettes_colour}.
34 | }
35 | \description{
36 | This creates a character vector that represents colours so when it is
37 | printed, colours will be formatted as hexadecimal strings.
38 | }
39 | \details{
40 | Colours can be specified using either:
41 | \itemize{
42 | \item Hexadecimal strings of the form \code{"#RRGGBB"} or \code{"#RRGGBBAA"}
43 | \item Colour names from \code{\link[grDevices:colors]{grDevices::colors()}}
44 | \item Positive integers \code{i} that index into \code{\link[grDevices:palette]{grDevices::palette()}}\verb{[i]}
45 | }
46 | }
47 | \examples{
48 | pal_colour(c("darkred", "#0F7BA2"))
49 |
50 | is_colour("darkred")
51 | is_colour(pal_colour("darkred"))
52 |
53 | as_colour("#0F7BA2")
54 | }
55 | \seealso{
56 | \code{\link[=pal_palette]{pal_palette()}}
57 | }
58 |
--------------------------------------------------------------------------------
/man/pal_numeric.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/scales-colour-mapping.R
3 | \name{pal_numeric}
4 | \alias{pal_numeric}
5 | \alias{pal_bin}
6 | \alias{pal_quantile}
7 | \alias{pal_factor}
8 | \title{Colour vector and colour palette mapping}
9 | \usage{
10 | pal_numeric(
11 | palette,
12 | domain,
13 | na.color = "#808080",
14 | alpha = FALSE,
15 | reverse = FALSE
16 | )
17 |
18 | pal_bin(
19 | palette,
20 | domain,
21 | bins = 7,
22 | pretty = TRUE,
23 | na.color = "#808080",
24 | alpha = FALSE,
25 | reverse = FALSE,
26 | right = FALSE
27 | )
28 |
29 | pal_quantile(
30 | palette,
31 | domain,
32 | n = 4,
33 | probs = seq(0, 1, length.out = n + 1),
34 | na.color = "#808080",
35 | alpha = FALSE,
36 | reverse = FALSE,
37 | right = FALSE
38 | )
39 |
40 | pal_factor(
41 | palette,
42 | domain,
43 | levels = NULL,
44 | ordered = FALSE,
45 | na.color = "#808080",
46 | alpha = FALSE,
47 | reverse = FALSE
48 | )
49 | }
50 | \arguments{
51 | \item{palette}{An object of class \code{\link[=pal_colour]{palettes_colour}} or
52 | \code{\link[=pal_colour]{palettes_colour}}.}
53 |
54 | \item{domain}{The possible values that can be mapped.
55 |
56 | For \code{pal_numeric} and \code{pal_bin}, this can be a simple numeric
57 | range (e.g. \code{c(0, 100)}); \code{pal_quantile} needs representative
58 | numeric data; and \code{pal_factor} needs categorical data.
59 |
60 | If \code{NULL}, then whenever the resulting colour function is called, the
61 | \code{x} value will represent the domain. This implies that if the function
62 | is invoked multiple times, the encoding between values and colours may not
63 | be consistent; if consistency is needed, you must provide a non-\code{NULL}
64 | domain.}
65 |
66 | \item{na.color}{The colour to return for \code{NA} values. Note that
67 | \code{na.color = NA} is valid.}
68 |
69 | \item{alpha}{Whether alpha channels should be respected or ignored. If \code{TRUE}
70 | then colors without explicit alpha information will be treated as fully
71 | opaque.}
72 |
73 | \item{reverse}{Whether the colours in \code{palette} should be
74 | used in reverse order. For example, if the default order of a palette goes
75 | from blue to green, then \code{reverse = TRUE} will result in the colors going
76 | from green to blue.}
77 |
78 | \item{bins}{Either a numeric vector of two or more unique cut points or a
79 | single number (greater than or equal to 2) giving the number of intervals
80 | into which the domain values are to be cut.}
81 |
82 | \item{pretty}{Whether to use the function \code{\link[=pretty]{pretty()}} to generate
83 | the bins when the argument \code{bins} is a single number. When
84 | \code{pretty = TRUE}, the actual number of bins may not be the number of
85 | bins you specified. When \code{pretty = FALSE}, \code{\link[=seq]{seq()}} is used
86 | to generate the bins and the breaks may not be "pretty".}
87 |
88 | \item{right}{parameter supplied to \code{\link[base:cut]{base::cut()}}. See Details}
89 |
90 | \item{n}{Number of equal-size quantiles desired. For more precise control,
91 | use the \code{probs} argument instead.}
92 |
93 | \item{probs}{See \code{\link[stats:quantile]{stats::quantile()}}. If provided, the \code{n}
94 | argument is ignored.}
95 |
96 | \item{levels}{An alternate way of specifying levels; if specified, domain is
97 | ignored}
98 |
99 | \item{ordered}{If \code{TRUE} and \code{domain} needs to be coerced to a
100 | factor, treat it as already in the correct order}
101 | }
102 | \value{
103 | A function that takes a single parameter \code{x}; when called with a
104 | vector of numbers (except for \code{pal_factor}, which expects
105 | factors/characters), #RRGGBB colour strings are returned (unless
106 | \code{alpha = TRUE} in which case #RRGGBBAA may also be possible).
107 | }
108 | \description{
109 | Conveniently maps data values (numeric or factor/character) to colours
110 | according to a given colour vector or colour palette.
111 | }
112 | \details{
113 | \code{pal_numeric} is a simple linear mapping from continuous numeric
114 | data to an interpolated palette.
115 |
116 | \code{pal_bin} also maps continuous numeric data, but performs
117 | binning based on value (see the \code{\link[base:cut]{base::cut()}} function). \code{pal_bin}
118 | defaults for the \code{cut} function are \code{include.lowest = TRUE} and
119 | \code{right = FALSE}.
120 |
121 | \code{pal_quantile} similarly bins numeric data, but via the
122 | \code{\link[stats:quantile]{stats::quantile()}} function.
123 |
124 | \code{pal_factor} maps factors to colours. If the palette is
125 | discrete and has a different number of colours than the number of factors,
126 | interpolation is used.
127 | }
128 | \examples{
129 | pal <- pal_bin(met_palettes$Tam, domain = 0:100)
130 | plot(as_colour(pal(sort(runif(16, 0, 100)))))
131 |
132 | # Exponential distribution, mapped continuously
133 | pal <- pal_numeric(met_palettes$Tam, domain = NULL)
134 | plot(as_colour(pal(sort(rexp(16)))))
135 |
136 | # Exponential distribution, mapped by interval
137 | pal <- pal_bin(met_palettes$Tam, domain = NULL, bins = 4)
138 | plot(as_colour(pal(sort(rexp(16)))))
139 |
140 | # Exponential distribution, mapped by quantile
141 | pal <- pal_quantile(met_palettes$Tam, domain = NULL)
142 | plot(as_colour(pal(sort(rexp(16)))))
143 |
144 | # Categorical data; by default, the values being coloured span the gamut...
145 | pal <- pal_factor(met_palettes$Java, domain = NULL)
146 | plot(as_colour(pal(LETTERS[1:5])))
147 |
148 | # ...unless the data is a factor, without droplevels...
149 | pal <- pal_factor(met_palettes$Java, domain = NULL)
150 | plot(as_colour(pal(factor(LETTERS[1:5], levels = LETTERS))))
151 |
152 | # ...or the domain is stated explicitly.
153 | pal <- pal_factor(met_palettes$Java, domain = NULL, levels = LETTERS)
154 | plot(as_colour(pal(LETTERS[1:5])))
155 | }
156 | \seealso{
157 | \code{\link[scales:col_numeric]{scales::col_numeric()}}
158 |
159 | \code{\link[scales:col_numeric]{scales::col_bin()}}
160 |
161 | \code{\link[scales:col_numeric]{scales::col_quantile()}}
162 |
163 | \code{\link[scales:col_numeric]{scales::col_factor()}}
164 | }
165 |
--------------------------------------------------------------------------------
/man/pal_palette.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/palette.R
3 | \name{pal_palette}
4 | \alias{pal_palette}
5 | \alias{is_palette}
6 | \alias{as_palette}
7 | \title{Colour palettes}
8 | \usage{
9 | pal_palette(...)
10 |
11 | is_palette(x)
12 |
13 | as_palette(x)
14 | }
15 | \arguments{
16 | \item{...}{\itemize{
17 | \item For \code{pal_palette()}: A named list of character vectors of any of the three
18 | kinds of R colour specifications, or a named list of colour vectors of
19 | class \code{\link[=pal_colour]{palettes_colour}}.
20 | }}
21 |
22 | \item{x}{\itemize{
23 | \item For \code{as_palette()}: An object to be coerced.
24 | \item For \code{is_palette()}: An object to test.
25 | }}
26 | }
27 | \value{
28 | An S3 list of class \code{palettes_palette}.
29 | }
30 | \description{
31 | This creates a list of colour vectors.
32 | }
33 | \details{
34 | Colours can be specified using either:
35 | \itemize{
36 | \item Hexadecimal strings of the form \code{"#RRGGBB"} or \code{"#RRGGBBAA"}
37 | \item Colour names from \code{\link[grDevices:colors]{grDevices::colors()}}
38 | \item Positive integers \code{i} that index into \code{\link[grDevices:palette]{grDevices::palette()}}\verb{[i]}
39 | }
40 | }
41 | \examples{
42 | pal_palette(
43 | Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"),
44 | Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156")
45 | )
46 |
47 | x <- list(
48 | Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"),
49 | Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156")
50 | )
51 | as_palette(x)
52 | }
53 | \seealso{
54 | \code{\link[=pal_colour]{pal_colour()}}
55 | }
56 |
--------------------------------------------------------------------------------
/man/pal_ramp.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/pal_ramp.R
3 | \name{pal_ramp}
4 | \alias{pal_ramp}
5 | \alias{pal_ramp.palettes_colour}
6 | \alias{pal_ramp.palettes_palette}
7 | \title{Colour vector and colour palette interpolation}
8 | \usage{
9 | pal_ramp(
10 | palette,
11 | n = NULL,
12 | direction = 1,
13 | space = "lab",
14 | interpolate = c("linear", "spline")
15 | )
16 |
17 | \method{pal_ramp}{palettes_colour}(
18 | palette,
19 | n = NULL,
20 | direction = 1,
21 | space = "lab",
22 | interpolate = c("linear", "spline")
23 | )
24 |
25 | \method{pal_ramp}{palettes_palette}(
26 | palette,
27 | n = NULL,
28 | direction = 1,
29 | space = "lab",
30 | interpolate = c("linear", "spline")
31 | )
32 | }
33 | \arguments{
34 | \item{palette}{An object of class \code{\link[=pal_palette]{palettes_palette}} or
35 | \code{\link[=pal_colour]{palettes_colour}}.}
36 |
37 | \item{n}{An integer specifying the number of colours to return.}
38 |
39 | \item{direction}{Sets the order of colours in the scale. If 1, the default,
40 | colours are ordered from first to last. If -1, the order of colours is
41 | reversed.}
42 |
43 | \item{space}{The colour space to interpolate in. One of: \code{"cmy"}, \code{"hsl"},
44 | \code{"hsb"}, \code{"hsv"}, \code{"lab"} (CIE L*ab), \code{"hunterlab"} (Hunter Lab),
45 | \code{"oklab"}, \code{"lch"} (CIE Lch(ab) / polarLAB), \code{"luv"}, \code{"rgb"} (sRGB),
46 | \code{"xyz"}, \code{"yxy"} (CIE xyY), \code{"hcl"} (CIE Lch(uv) / polarLuv), or \code{"oklch"}
47 | (Polar form of oklab).}
48 |
49 | \item{interpolate}{The interpolation method. Either "linear" (default) or
50 | "spline".}
51 | }
52 | \value{
53 | An object of the same type as \code{palette}. The output has the following properties:
54 | \itemize{
55 | \item For objects of class \code{\link[=pal_colour]{palettes_colour}}: A colour vector
56 | with \code{n} colours.
57 | \item For objects of class \code{\link[=pal_palette]{palettes_palette}}: Colour palettes
58 | with \code{n} colours in each palette.
59 | }
60 | }
61 | \description{
62 | Interpolate the set of colours in \code{\link[=pal_palette]{palettes_palette}} or
63 | \code{\link[=pal_colour]{palettes_colour}} objects to create new colour palettes.
64 | }
65 | \examples{
66 | # The class returned after interpolation matches the input class.
67 | x <- pal_colour(c("darkslateblue", "cornflowerblue", "slategray1"))
68 | y <- pal_palette(blues = x)
69 | class(pal_ramp(x))
70 | class(pal_ramp(y))
71 |
72 | # Choose between linear and spline interpolation.
73 | pal_ramp(x, n = 7, interpolate = "linear")
74 | pal_ramp(x, n = 7, interpolate = "spline")
75 |
76 | # Palettes will have the same length after interpolation, regardless of the
77 | # number of colours in the original palette.
78 | z <- pal_palette(
79 | Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"),
80 | Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156")
81 | )
82 | pal_ramp(z, n = 5)
83 | }
84 | \seealso{
85 | \code{\link[=pal_colour]{pal_colour()}}, \code{\link[=pal_palette]{pal_palette()}}
86 | }
87 |
--------------------------------------------------------------------------------
/man/palettes-options.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/options.R
3 | \name{palettes-options}
4 | \alias{palettes-options}
5 | \title{Package options}
6 | \description{
7 | Options that adjust the behaviour of the palettes package.
8 | }
9 | \details{
10 | These options can be set via \code{\link[=options]{options()}} and queried via \code{\link[=getOption]{getOption()}}.
11 | }
12 | \section{Options for the palettes package}{
13 |
14 |
15 | \describe{
16 |
17 | \item{\code{palettes.print_symbol}:}{
18 |
19 | Character string setting the symbol used for colour previews. See
20 | \code{\link[=list_colour_symbols]{list_colour_symbols()}} for a list of symbol choices. Defaults to
21 | \code{"circle_small"}. Set to \code{FALSE} to disable printing symbols.
22 |
23 | }
24 | \item{\code{palettes.print_hex}:}{
25 |
26 | Logical setting whether to print hex codes in colour previews. Defaults to
27 | \code{TRUE}.
28 |
29 | }
30 | \item{\code{palettes.print_alpha}:}{
31 |
32 | Logical setting whether to print the hex code alpha channel in colour
33 | previews. Defaults to \code{FALSE}. Colours without an alpha channel will be
34 | assumed to be full opacity.
35 |
36 | }
37 | \item{\code{palettes.print_sep}:}{
38 |
39 | Character string to separate colours by in colour previews. Defaults to \code{""}.
40 |
41 | }
42 | \item{\code{palettes.print_width}:}{
43 |
44 | Integer setting the maximum number of colours on a line in colour previews.
45 | Defaults to \code{1}.
46 |
47 | }
48 | \item{\code{palettes.print_index}:}{
49 |
50 | Logical setting whether to print the index of the first colour on each line
51 | in colour previews. Defaults to \code{FALSE}.
52 |
53 | }
54 |
55 | }
56 | }
57 |
58 | \section{Note}{
59 |
60 | To disable formatting in colour previews set both \code{palettes.print_symbol} and
61 | \code{palettes.print_hex} to \code{FALSE}.
62 | }
63 |
64 | \examples{
65 | options(
66 | palettes.print_symbol = "square",
67 | palettes.print_hex = FALSE,
68 | palettes.print_sep = " ",
69 | palettes.print_width = 3,
70 | palettes.print_index = TRUE
71 | )
72 | met_palettes$Cross
73 | }
74 |
--------------------------------------------------------------------------------
/man/palettes-package.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/palettes-package.R
3 | \docType{package}
4 | \name{palettes-package}
5 | \alias{palettes}
6 | \alias{palettes-package}
7 | \title{palettes: Methods for Colour Vectors and Colour Palettes}
8 | \description{
9 | \if{html}{\figure{logo.png}{options: style='float: right' alt='logo' width='120'}}
10 |
11 | Provides a comprehensive library for colour vectors and colour palettes using a new family of colour classes (palettes_colour and palettes_palette) that always print as hex codes with colour previews. Capabilities include: formatting, casting and coercion, extraction and updating of components, plotting, colour mixing arithmetic, and colour interpolation.
12 | }
13 | \seealso{
14 | Useful links:
15 | \itemize{
16 | \item \url{https://mccarthy-m-g.github.io/palettes/}
17 | \item \url{https://github.com/mccarthy-m-g/palettes}
18 | \item Report bugs at \url{https://github.com/mccarthy-m-g/palettes/issues}
19 | }
20 |
21 | }
22 | \author{
23 | \strong{Maintainer}: Michael McCarthy \email{m.mccarthy1624@gmail.com} [copyright holder]
24 |
25 | }
26 | \keyword{internal}
27 |
--------------------------------------------------------------------------------
/man/palettes-rlang.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/palettes-package.R
3 | \name{palettes-rlang}
4 | \alias{palettes-rlang}
5 | \title{Internal rlang methods}
6 | \description{
7 | Internal rlang methods
8 | }
9 | \keyword{internal}
10 |
--------------------------------------------------------------------------------
/man/palettes-vctrs.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/palettes-package.R
3 | \name{palettes-vctrs}
4 | \alias{palettes-vctrs}
5 | \title{Internal vctrs methods}
6 | \description{
7 | Internal vctrs methods
8 | }
9 | \keyword{internal}
10 |
--------------------------------------------------------------------------------
/man/penguin_palettes.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \docType{data}
4 | \name{penguin_palettes}
5 | \alias{penguin_palettes}
6 | \title{Palmer penguins palettes}
7 | \format{
8 | \subsection{\code{penguin_palettes}}{
9 |
10 | An object of class \code{palettes_palette} with 6
11 | colour palettes. Use \code{names(penguin_palettes)} to return all palette names.
12 | }
13 | }
14 | \source{
15 | \url{https://github.com/allisonhorst/palmerpenguins/}
16 | }
17 | \usage{
18 | penguin_palettes
19 | }
20 | \description{
21 | Palettes inspired by the Palmer penguins.
22 | }
23 | \examples{
24 | # Get all palettes by name.
25 | names(penguin_palettes)
26 |
27 | # Plot all palettes.
28 | plot(penguin_palettes)
29 | }
30 | \seealso{
31 | \code{\link[=pal_palette]{pal_palette()}}, \code{\link[=pal_colour]{pal_colour()}}
32 | }
33 | \author{
34 | \href{https://github.com/allisonhorst}{Allison Horst}
35 | }
36 | \keyword{datasets}
37 |
--------------------------------------------------------------------------------
/man/performance_palettes.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \docType{data}
4 | \name{performance_palettes}
5 | \alias{performance_palettes}
6 | \title{Performance palettes}
7 | \format{
8 | \subsection{\code{performance_palettes}}{
9 |
10 | An object of class \code{palettes_palette} with 1
11 | colour palettes. All colours in each palette are distinguishable with
12 | deuteranopia, protanopia, and tritanopia. Use \code{names(performance_palettes)}
13 | to return all palette names.
14 | }
15 | }
16 | \source{
17 | \url{https://github.com/easystats/performance/}
18 | }
19 | \usage{
20 | performance_palettes
21 | }
22 | \description{
23 | A colourblind accessible palette for visualizing performance.
24 | }
25 | \examples{
26 | # Get all palettes by name.
27 | names(performance_palettes)
28 |
29 | # Plot all palettes.
30 | plot(performance_palettes)
31 | }
32 | \seealso{
33 | \code{\link[=pal_palette]{pal_palette()}}, \code{\link[=pal_colour]{pal_colour()}}
34 | }
35 | \keyword{datasets}
36 |
--------------------------------------------------------------------------------
/man/plot.palettes_colour.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/plot.R
3 | \name{plot.palettes_colour}
4 | \alias{plot.palettes_colour}
5 | \alias{plot.palettes_palette}
6 | \title{Plot colour vectors and colour palettes}
7 | \usage{
8 | \method{plot}{palettes_colour}(
9 | x,
10 | n = NULL,
11 | direction = 1,
12 | space = "lab",
13 | interpolate = c("linear", "spline"),
14 | ...
15 | )
16 |
17 | \method{plot}{palettes_palette}(
18 | x,
19 | n = NULL,
20 | direction = 1,
21 | space = "lab",
22 | interpolate = c("linear", "spline"),
23 | ...
24 | )
25 | }
26 | \arguments{
27 | \item{x}{An object of class \code{\link[=pal_palette]{palettes_palette}} or
28 | \code{\link[=pal_colour]{palettes_colour}}.}
29 |
30 | \item{n}{An integer specifying the number of colours to return.}
31 |
32 | \item{direction}{Sets the order of colours in the scale. If 1, the default,
33 | colours are ordered from first to last. If -1, the order of colours is
34 | reversed.}
35 |
36 | \item{space}{The colour space to interpolate in. One of: \code{"cmy"}, \code{"hsl"},
37 | \code{"hsb"}, \code{"hsv"}, \code{"lab"} (CIE L*ab), \code{"hunterlab"} (Hunter Lab),
38 | \code{"oklab"}, \code{"lch"} (CIE Lch(ab) / polarLAB), \code{"luv"}, \code{"rgb"} (sRGB),
39 | \code{"xyz"}, \code{"yxy"} (CIE xyY), \code{"hcl"} (CIE Lch(uv) / polarLuv), or \code{"oklch"}
40 | (Polar form of oklab).}
41 |
42 | \item{interpolate}{The interpolation method. Either "linear" (default) or
43 | "spline".}
44 |
45 | \item{...}{Not used.}
46 | }
47 | \value{
48 | A \link[ggplot2:ggplot2-package]{ggplot2} object. The output has the following properties:
49 | \itemize{
50 | \item For objects of class \code{\link[=pal_colour]{palettes_colour}}: A plot of colour
51 | swatches.
52 | \item For objects of class \code{\link[=pal_palette]{palettes_palette}} with one palette:
53 | A plot of colour swatches with the palette name spanned across the swatches.
54 | \item For objects of class \code{\link[=pal_palette]{palettes_palette}} with more than one
55 | palette: A faceted plot of colour swatches with palette names as facet
56 | titles.
57 | }
58 | }
59 | \description{
60 | Plot colour vectors and colour palettes as swatches.
61 | }
62 | \examples{
63 | # Objects of class `palettes_colour` are plotted as swatches.
64 | x <- pal_colour(c("darkslateblue", "cornflowerblue", "slategray1"))
65 | plot(x)
66 |
67 | # Objects of class `palettes_palette` with one palette are plotted with
68 | # the palette name spanned across the swatches.
69 | y <- pal_palette(Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"))
70 | plot(y)
71 |
72 | # Objects of class `palettes_palette` with multiple palettes are faceted.
73 | z <- pal_palette(
74 | Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"),
75 | Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156")
76 | )
77 | plot(z)
78 |
79 | # Colours can also be interpolated.
80 | plot(x, n = 5)
81 | plot(y, n = 5)
82 | plot(z, n = 5)
83 | }
84 | \seealso{
85 | \code{\link[=pal_colour]{pal_colour()}}, \code{\link[=pal_palette]{pal_palette()}}, \code{\link[=pal_ramp]{pal_ramp()}}
86 | }
87 |
--------------------------------------------------------------------------------
/man/pnw_palettes.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \docType{data}
4 | \name{pnw_palettes}
5 | \alias{pnw_palettes}
6 | \title{Pacific Northwest palettes}
7 | \format{
8 | \subsection{\code{pnw_palettes}}{
9 |
10 | An object of class \code{palettes_palette} with 14 colour
11 | palettes. Use \code{names(pnw_palettes)} to return all palette names.
12 | }
13 | }
14 | \source{
15 | \url{https://github.com/jakelawlor/PNWColors}
16 | }
17 | \usage{
18 | pnw_palettes
19 | }
20 | \description{
21 | Palettes inspired by Jake Lawlor's photos of the dreamiest, most colourful,
22 | PNW-iest places in Washington State.
23 | }
24 | \examples{
25 | # Get all palettes by name.
26 | names(pnw_palettes)
27 |
28 | # Plot all palettes.
29 | plot(pnw_palettes)
30 | }
31 | \seealso{
32 | \code{\link[=pal_palette]{pal_palette()}}, \code{\link[=pal_colour]{pal_colour()}}, \code{\link[PNWColors:pnw_palette]{PNWColors::pnw_palette()}}
33 | }
34 | \author{
35 | \href{https://github.com/jakelawlor}{Jake Lawlor}
36 | }
37 | \keyword{datasets}
38 |
--------------------------------------------------------------------------------
/man/reexports.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/reexport.R
3 | \docType{import}
4 | \name{reexports}
5 | \alias{reexports}
6 | \alias{as_tibble}
7 | \title{Objects exported from other packages}
8 | \keyword{internal}
9 | \description{
10 | These objects are imported from other packages. Follow the links
11 | below to see their documentation.
12 |
13 | \describe{
14 | \item{tibble}{\code{\link[tibble]{as_tibble}}}
15 | }}
16 |
17 |
--------------------------------------------------------------------------------
/man/scale_colour_palette_d.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/ggplot2-scales.R
3 | \name{scale_colour_palette_d}
4 | \alias{scale_colour_palette_d}
5 | \alias{scale_fill_palette_d}
6 | \alias{scale_colour_palette_c}
7 | \alias{scale_fill_palette_c}
8 | \alias{scale_colour_palette_b}
9 | \alias{scale_fill_palette_b}
10 | \alias{scale_color_palette_d}
11 | \alias{scale_color_palette_c}
12 | \alias{scale_color_palette_b}
13 | \title{Colour scales from colour vectors and colour palettes}
14 | \usage{
15 | scale_colour_palette_d(palette, direction = 1, ...)
16 |
17 | scale_fill_palette_d(palette, direction = 1, ...)
18 |
19 | scale_colour_palette_c(palette, direction = 1, ...)
20 |
21 | scale_fill_palette_c(palette, direction = 1, ...)
22 |
23 | scale_colour_palette_b(palette, direction = 1, ...)
24 |
25 | scale_fill_palette_b(palette, direction = 1, ...)
26 | }
27 | \arguments{
28 | \item{palette}{An object of class \code{\link[=pal_palette]{palettes_palette}} or
29 | \code{\link[=pal_colour]{palettes_colour}}.}
30 |
31 | \item{direction}{Sets the order of colours in the scale. If 1, the default,
32 | colours are ordered from first to last. If -1, the order of colours is
33 | reversed.}
34 |
35 | \item{...}{Other arguments passed on to \code{\link[ggplot2:discrete_scale]{ggplot2::discrete_scale()}},
36 | \code{\link[ggplot2:continuous_scale]{ggplot2::continuous_scale()}}, or \code{\link[ggplot2:binned_scale]{ggplot2::binned_scale()}} to control name,
37 | limits, breaks, labels and so forth.}
38 | }
39 | \value{
40 | A scale function that controls the mapping between data and colour or
41 | fill aesthetics in a \link[ggplot2:ggplot2-package]{ggplot2} plot.
42 | }
43 | \description{
44 | Create discrete, continuous, and binned colour scales from colour vectors and
45 | colour palettes.
46 | }
47 | \examples{
48 | library(ggplot2)
49 |
50 | # Use palette_d with discrete data
51 | discrete_pal <- pal_colour(c("#663171", "#EA7428", "#0C7156"))
52 | ggplot(mtcars, aes(wt, mpg, colour = as.factor(cyl))) +
53 | geom_point(size = 3) +
54 | scale_colour_palette_d(discrete_pal)
55 |
56 | # Use palette_c with continuous data
57 | continuous_pal <- pal_colour(c("#3C0D03", "#E67424", "#F5C34D"))
58 | ggplot(mtcars, aes(wt, mpg, colour = mpg)) +
59 | geom_point(size = 3) +
60 | scale_colour_palette_c(continuous_pal)
61 |
62 | # Use palette_b to bin continuous data before mapping
63 | ggplot(mtcars, aes(wt, mpg, colour = mpg)) +
64 | geom_point(size = 3) +
65 | scale_colour_palette_b(continuous_pal)
66 | }
67 |
--------------------------------------------------------------------------------
/man/viridis_palettes.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \docType{data}
4 | \name{viridis_palettes}
5 | \alias{viridis_palettes}
6 | \title{Viridis palettes}
7 | \format{
8 | \subsection{\code{viridis_palettes}}{
9 |
10 | An object of class \code{palettes_palette} with 8
11 | colour palettes. All colours in each palette are distinguishable with
12 | deuteranopia, protanopia, and tritanopia. Use \code{names(viridis_palettes)} to
13 | return all palette names.
14 | }
15 | }
16 | \source{
17 | \url{https://github.com/sjmgarnier/viridisLite}
18 | }
19 | \usage{
20 | viridis_palettes
21 | }
22 | \description{
23 | Colourblind accessible palettes that are perceptually uniform in both colour
24 | and black-and-white.
25 | }
26 | \examples{
27 | # Get all palettes by name.
28 | names(viridis_palettes)
29 |
30 | # Plot all palettes.
31 | plot(viridis_palettes, n = 256)
32 | }
33 | \seealso{
34 | \code{\link[=pal_palette]{pal_palette()}}, \code{\link[=pal_colour]{pal_colour()}}, \code{\link[viridisLite:viridis]{viridisLite::viridis()}}
35 | }
36 | \author{
37 | \href{https://github.com/sjmgarnier}{Simon Garnier}
38 | }
39 | \keyword{datasets}
40 |
--------------------------------------------------------------------------------
/palettes.Rproj:
--------------------------------------------------------------------------------
1 | Version: 1.0
2 |
3 | RestoreWorkspace: Default
4 | SaveWorkspace: Default
5 | AlwaysSaveHistory: Default
6 |
7 | EnableCodeIndexing: Yes
8 | UseSpacesForTab: Yes
9 | NumSpacesForTab: 2
10 | Encoding: UTF-8
11 |
12 | RnwWeave: Sweave
13 | LaTeX: pdfLaTeX
14 |
15 | AutoAppendNewline: Yes
16 | StripTrailingWhitespace: Yes
17 |
18 | BuildType: Package
19 | PackageUseDevtools: Yes
20 | PackageInstallArgs: --no-multiarch --with-keep.source
21 |
--------------------------------------------------------------------------------
/pkgdown/_pkgdown.yml:
--------------------------------------------------------------------------------
1 | url: https://mccarthy-m-g.github.io/palettes/
2 |
3 | development:
4 | mode: auto
5 |
6 | template:
7 | bootstrap: 5
8 |
9 | reference:
10 | - title: Colour vectors and colour palettes
11 | desc:
12 | contents:
13 | - pal_colour
14 | - pal_palette
15 | - as_tibble.palettes_colour
16 | - plot.palettes_colour
17 |
18 | - title: Colour mapping and colour scales
19 | desc:
20 | contents:
21 | - pal_numeric
22 | - scale_colour_palette_d
23 |
24 | - title: Colour interpolation and colour mixing
25 | desc:
26 | contents:
27 | - pal_ramp
28 | - colour-mixing-arithmetic
29 | - colour-mixing-math
30 |
31 | # - title: Colour manipulation
32 | # desc:
33 | # contents:
34 | # - pal_lighten
35 |
36 | - title: Included palettes
37 | desc:
38 | contents:
39 | - carto_palettes
40 | - performance_palettes
41 | - met_palettes
42 | - penguin_palettes
43 | - nord_palettes
44 | - pnw_palettes
45 | - viridis_palettes
46 |
47 | - title: Miscellaneous
48 | desc:
49 | contents:
50 | - palettes-options
51 | - list_colour_symbols
52 |
53 | articles:
54 | - title: User
55 | navbar: ~
56 | contents:
57 | - ggplot2
58 | - gt
59 | - biscale
60 | - compatibility
61 |
62 | - title: Developer
63 | navbar: Developer
64 | contents:
65 | - creating-packages
66 |
67 | - title: Community
68 | navbar: Community
69 | contents:
70 | - articles/made-with-palettes
71 |
72 | news:
73 | releases:
74 | - text: "Version 0.2.0"
75 | href: https://tidytales.ca/posts/2024-02-05_palettes-v0.2.0/
76 | - text: "Version 0.1.0"
77 | href: https://tidytales.ca/posts/2022-12-20_palettes/
78 |
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-120x120.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/pkgdown/favicon/apple-touch-icon-120x120.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-152x152.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/pkgdown/favicon/apple-touch-icon-152x152.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-180x180.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/pkgdown/favicon/apple-touch-icon-180x180.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-60x60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/pkgdown/favicon/apple-touch-icon-60x60.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-76x76.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/pkgdown/favicon/apple-touch-icon-76x76.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/pkgdown/favicon/apple-touch-icon.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon-16x16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/pkgdown/favicon/favicon-16x16.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon-32x32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/pkgdown/favicon/favicon-32x32.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mccarthy-m-g/palettes/329a872b06eb419d4df7bb7fd44dddc07a6c579c/pkgdown/favicon/favicon.ico
--------------------------------------------------------------------------------
/revdep/.gitignore:
--------------------------------------------------------------------------------
1 | checks
2 | library
3 | checks.noindex
4 | library.noindex
5 | cloud.noindex
6 | data.sqlite
7 | *.html
8 |
--------------------------------------------------------------------------------
/revdep/README.md:
--------------------------------------------------------------------------------
1 | # Platform
2 |
3 | |field |value |
4 | |:--------|:---------------------------------------------------------------------------|
5 | |version |R version 4.2.2 (2022-10-31) |
6 | |os |macOS Mojave 10.14.6 |
7 | |system |x86_64, darwin17.0 |
8 | |ui |RStudio |
9 | |language |(EN) |
10 | |collate |en_CA.UTF-8 |
11 | |ctype |en_CA.UTF-8 |
12 | |tz |America/Vancouver |
13 | |date |2024-07-13 |
14 | |rstudio |2021.09.2+382 Ghost Orchid (desktop) |
15 | |pandoc |2.14.0.3 @ /Applications/RStudio.app/Contents/MacOS/pandoc/ (via rmarkdown) |
16 |
17 | # Dependencies
18 |
19 | |package |old |new |Δ |
20 | |:------------|:-----|:----------|:--|
21 | |palettes |0.2.0 |0.2.0.9000 |* |
22 | |cli |3.6.2 |3.6.2 | |
23 | |colorspace |2.1-0 |2.1-0 | |
24 | |fansi |1.0.6 |1.0.6 | |
25 | |farver |2.1.1 |2.1.1 | |
26 | |ggplot2 |3.5.1 |3.5.1 | |
27 | |glue |1.6.2 |1.6.2 | |
28 | |gtable |0.3.5 |0.3.5 | |
29 | |isoband |0.2.7 |0.2.7 | |
30 | |labeling |0.4.3 |0.4.3 | |
31 | |lifecycle |1.0.4 |1.0.4 | |
32 | |magrittr |2.0.3 |2.0.3 | |
33 | |munsell |0.5.1 |0.5.1 | |
34 | |pillar |1.9.0 |1.9.0 | |
35 | |pkgconfig |2.0.3 |2.0.3 | |
36 | |prismatic |1.1.2 |1.1.2 | |
37 | |purrr |1.0.2 |1.0.2 | |
38 | |R6 |2.5.1 |2.5.1 | |
39 | |RColorBrewer |1.1-3 |1.1-3 | |
40 | |rlang |1.1.2 |1.1.2 | |
41 | |scales |1.3.0 |1.3.0 | |
42 | |tibble |3.2.1 |3.2.1 | |
43 | |utf8 |1.2.4 |1.2.4 | |
44 | |vctrs |0.6.5 |0.6.5 | |
45 | |viridisLite |0.4.2 |0.4.2 | |
46 | |withr |3.0.0 |3.0.0 | |
47 |
48 | # Revdeps
49 |
50 |
--------------------------------------------------------------------------------
/revdep/cran.md:
--------------------------------------------------------------------------------
1 | ## revdepcheck results
2 |
3 | We checked 0 reverse dependencies, comparing R CMD check results across CRAN and dev versions of this package.
4 |
5 | * We saw 0 new problems
6 | * We failed to check 0 packages
7 |
8 |
--------------------------------------------------------------------------------
/revdep/email.yml:
--------------------------------------------------------------------------------
1 | release_date: ???
2 | rel_release_date: ???
3 | my_news_url: ???
4 | release_version: ???
5 | release_details: ???
6 |
--------------------------------------------------------------------------------
/revdep/failures.md:
--------------------------------------------------------------------------------
1 | *Wow, no problems at all. :)*
--------------------------------------------------------------------------------
/revdep/problems.md:
--------------------------------------------------------------------------------
1 | *Wow, no problems at all. :)*
--------------------------------------------------------------------------------
/tests/testthat.R:
--------------------------------------------------------------------------------
1 | library(testthat)
2 | library(palettes)
3 |
4 | test_check("palettes")
5 |
--------------------------------------------------------------------------------
/tests/testthat/_snaps/colour.md:
--------------------------------------------------------------------------------
1 | # format - can format() a colour
2 |
3 | Code
4 | format(pal_colour("black"))
5 | Output
6 | [1] "#000000"
7 |
8 | # pillar shaft works [plain]
9 |
10 | Code
11 | pillar_shaft(x$colour)
12 | Output
13 |
14 | * #FF0000
15 |
16 | # pillar shaft works [ansi]
17 |
18 | Code
19 | pillar_shaft(x$colour)
20 | Output
21 |
22 | [31m*[39m #FF0000
23 |
24 | # pillar shaft works [unicode]
25 |
26 | Code
27 | pillar_shaft(x$colour)
28 | Output
29 |
30 | • #FF0000
31 |
32 | # pillar shaft works [fancy]
33 |
34 | Code
35 | pillar_shaft(x$colour)
36 | Output
37 |
38 | [31m•[39m #FF0000
39 |
40 | # normal print method works [plain]
41 |
42 | Code
43 | x
44 | Output
45 |
46 | * #FF0000
47 |
48 | # normal print method works [ansi]
49 |
50 | Code
51 | x
52 | Output
53 |
54 | [31m*[39m #FF0000
55 |
56 | # normal print method works [unicode]
57 |
58 | Code
59 | x
60 | Output
61 |
62 | • #FF0000
63 |
64 | # normal print method works [fancy]
65 |
66 | Code
67 | x
68 | Output
69 |
70 | [31m•[39m #FF0000
71 |
72 | # zero-length normal print method works
73 |
74 | Code
75 | x
76 | Output
77 |
78 |
79 | # palettes.print_symbol option works [plain]
80 |
81 | Code
82 | x
83 | Output
84 |
85 | [x] #FF0000
86 |
87 | # palettes.print_symbol option works [ansi]
88 |
89 | Code
90 | x
91 | Output
92 |
93 | [31m[x][39m #FF0000
94 |
95 | # palettes.print_symbol option works [unicode]
96 |
97 | Code
98 | x
99 | Output
100 |
101 | ■ #FF0000
102 |
103 | # palettes.print_symbol option works [fancy]
104 |
105 | Code
106 | x
107 | Output
108 |
109 | [31m■[39m #FF0000
110 |
111 | # palettes.print_symbol works with FALSE
112 |
113 | Code
114 | x
115 | Output
116 |
117 | #FF0000
118 |
119 | # palettes.print_hex option works [plain]
120 |
121 | Code
122 | x
123 | Output
124 |
125 | *
126 |
127 | # palettes.print_hex option works [ansi]
128 |
129 | Code
130 | x
131 | Output
132 |
133 | [31m*[39m
134 |
135 | # palettes.print_hex option works [unicode]
136 |
137 | Code
138 | x
139 | Output
140 |
141 | •
142 |
143 | # palettes.print_hex option works [fancy]
144 |
145 | Code
146 | x
147 | Output
148 |
149 | [31m•[39m
150 |
151 | # palettes.print_alpha option works
152 |
153 | Code
154 | x
155 | Output
156 |
157 | * #88A0DCF0
158 | * #381A61CC
159 | * #7C4B73FF
160 |
161 | # palettes.print_sep option works
162 |
163 | Code
164 | x
165 | Output
166 |
167 | * #FF0000,
168 | * #0000FF
169 |
170 | # palettes.print_width option works
171 |
172 | Code
173 | x
174 | Output
175 |
176 | * #FF0000* #0000FF
177 | * #00FF00
178 |
179 | # palettes.print_index option works
180 |
181 | Code
182 | x
183 | Output
184 |
185 | [1] * #FF0000
186 | [2] * #0000FF
187 | [3] * #00FF00
188 |
189 | # disabling formatting works
190 |
191 | Code
192 | x
193 | Output
194 |
195 | "red"
196 | "#ff0000"
197 | "#FF0000"
198 |
199 |
--------------------------------------------------------------------------------
/tests/testthat/_snaps/ggplot2-scales.md:
--------------------------------------------------------------------------------
1 | # binned scales only support continuous data
2 |
3 | Binned scales only support continuous data.
4 |
5 | ---
6 |
7 | Binned scales only support continuous data.
8 |
9 |
--------------------------------------------------------------------------------
/tests/testthat/_snaps/symbol.md:
--------------------------------------------------------------------------------
1 | # unicode and ascii symbols work [plain]
2 |
3 | Code
4 | list_colour_symbols()
5 | Output
6 | -- options(palettes.print_symbol = ...) --------------------
7 | * circle_small [x] square_large
8 | * circle # block
9 | * circle_medium # block_full
10 | * circle_large [+] diamond_small
11 | * ellipse_horizontal [+] diamond
12 | * ellipse_vertical [+] diamond_medium
13 | ^ triangle_up * pentagon
14 | v triangle_down * hexagon
15 | < triangle_left * star
16 | > triangle_right <3 heart
17 | [x] square_small :) smiley
18 | [x] square /\/ moustache
19 | [x] square_medium
20 |
21 | # unicode and ascii symbols work [ansi]
22 |
23 | Code
24 | list_colour_symbols()
25 | Output
26 | -- options(palettes.print_symbol = ...) --------------------
27 | * circle_small [x] square_large
28 | * circle # block
29 | * circle_medium # block_full
30 | * circle_large [+] diamond_small
31 | * ellipse_horizontal [+] diamond
32 | * ellipse_vertical [+] diamond_medium
33 | ^ triangle_up * pentagon
34 | v triangle_down * hexagon
35 | < triangle_left * star
36 | > triangle_right <3 heart
37 | [x] square_small :) smiley
38 | [x] square /\/ moustache
39 | [x] square_medium
40 |
41 | # unicode and ascii symbols work [unicode]
42 |
43 | Code
44 | list_colour_symbols()
45 | Output
46 | ── options(palettes.print_symbol = ...) ────────────────────
47 | • circle_small ⬛ square_large
48 | ● circle ▇ block
49 | ⚫ circle_medium █ block_full
50 | ⬤ circle_large ⬩ diamond_small
51 | ⬬ ellipse_horizontal ◆ diamond
52 | ⬮ ellipse_vertical ⬥ diamond_medium
53 | ▲ triangle_up ⬟ pentagon
54 | ▼ triangle_down ⬢ hexagon
55 | ◀ triangle_left ★ star
56 | ▶ triangle_right ♥ heart
57 | ▪ square_small ☺ smiley
58 | ■ square ෴ moustache
59 | ◼ square_medium
60 |
61 | # unicode and ascii symbols work [fancy]
62 |
63 | Code
64 | list_colour_symbols()
65 | Output
66 | ── options(palettes.print_symbol = ...) ────────────────────
67 | • circle_small ⬛ square_large
68 | ● circle ▇ block
69 | ⚫ circle_medium █ block_full
70 | ⬤ circle_large ⬩ diamond_small
71 | ⬬ ellipse_horizontal ◆ diamond
72 | ⬮ ellipse_vertical ⬥ diamond_medium
73 | ▲ triangle_up ⬟ pentagon
74 | ▼ triangle_down ⬢ hexagon
75 | ◀ triangle_left ★ star
76 | ▶ triangle_right ♥ heart
77 | ▪ square_small ☺ smiley
78 | ■ square ෴ moustache
79 | ◼ square_medium
80 |
81 |
--------------------------------------------------------------------------------
/tests/testthat/helper.R:
--------------------------------------------------------------------------------
1 | # Call in tests to locally set options for maximal reproducibility.
2 | local_palettes_options <- function(
3 | print_symbol = "circle_small",
4 | print_hex = TRUE,
5 | print_alpha = FALSE,
6 | print_sep = "",
7 | print_width = 1,
8 | print_index = FALSE,
9 | .local_envir = parent.frame()
10 | ) {
11 | withr::local_options(
12 | palettes.print_symbol = print_symbol,
13 | palettes.print_hex = print_hex,
14 | palettes.print_alpha = print_alpha,
15 | palettes.print_sep = print_sep,
16 | palettes.print_width = print_width,
17 | palettes.print_index = print_index,
18 | .local_envir = .local_envir
19 | )
20 | }
21 |
--------------------------------------------------------------------------------
/tests/testthat/test-colour.R:
--------------------------------------------------------------------------------
1 | # ------------------------------------------------------------------------------
2 | # as_colour()
3 |
4 | test_that("can convert colour -> colour", {
5 | expect_identical(as_colour(new_colour("black")), new_colour("black"))
6 | })
7 |
8 | test_that("can convert palette -> colour", {
9 | colour_palette <- pal_palette(black = "black")
10 | expect_identical(as_colour(colour_palette), new_colour("black"))
11 | })
12 |
13 | # ------------------------------------------------------------------------------
14 | # as.character()
15 |
16 | test_that("as.character() works", {
17 | x <- pal_colour("black")
18 | expect_identical(as.character(x), "black")
19 | })
20 |
21 | test_that("as.character() works with NA", {
22 | expect_identical(as.character(pal_colour(NA)), NA_character_)
23 | })
24 |
25 | # ------------------------------------------------------------------------------
26 | # as_tibble()
27 |
28 | test_that("as_tibble() works", {
29 | colour_vector <- pal_colour("black")
30 | colour_tibble <- tibble(colour = pal_colour("black"))
31 | expect_identical(as_tibble(colour_vector), colour_tibble)
32 | })
33 |
34 | # ------------------------------------------------------------------------------
35 | # format()
36 |
37 | test_that("format - can format() a colour", {
38 | local_palettes_options()
39 | expect_snapshot(format(pal_colour("black")))
40 | })
41 |
42 | # ------------------------------------------------------------------------------
43 | # pal_colour()
44 |
45 | test_that("zero-length input works", {
46 | expect_s3_class(pal_colour(), "palettes_colour")
47 | expect_length(pal_colour(), 0)
48 | expect_true(is_colour(pal_colour()))
49 | })
50 |
51 | test_that("hexadecimal string input works", {
52 | x <- pal_colour("#FFFFFF")
53 | x_alpha <- pal_colour("#FFFFFFFF")
54 | expect_true(is_colour(pal_colour(x)))
55 | expect_true(is_colour(pal_colour(x_alpha)))
56 | })
57 |
58 | test_that("colour name input works", {
59 | x <- pal_colour("red")
60 | expect_true(is_colour(pal_colour(x)))
61 | })
62 |
63 | test_that("invalid values fail with an error", {
64 | # No "#"
65 | expect_error(pal_colour("a"), class = "rlang_error")
66 | # Wrong number of values
67 | expect_error(pal_colour("#a"), class = "rlang_error")
68 | expect_error(pal_colour("#ab"), class = "rlang_error")
69 | expect_error(pal_colour("#abc"), class = "rlang_error")
70 | expect_error(pal_colour("#abcd"), class = "rlang_error")
71 | expect_error(pal_colour("#abcde"), class = "rlang_error")
72 | expect_error(pal_colour("#abcdefa"), class = "rlang_error")
73 | expect_error(pal_colour("#abcdefabc"), class = "rlang_error")
74 | })
75 |
76 | # ------------------------------------------------------------------------------
77 | # pillar_shaft()
78 |
79 | cli::test_that_cli("pillar shaft works", {
80 | local_palettes_options()
81 | x <- as_tibble(pal_colour("red"))
82 | expect_snapshot(pillar_shaft(x$colour))
83 | })
84 |
85 | # ------------------------------------------------------------------------------
86 | # plot()
87 |
88 | test_that("plotting works", {
89 | x <- pal_colour(c("red", "blue"))
90 | expect_s3_class(plot(x), "gg")
91 | })
92 |
93 | # ------------------------------------------------------------------------------
94 | # print() / obj_print_data() / obj_print_footer()
95 |
96 | cli::test_that_cli("normal print method works", {
97 | local_palettes_options()
98 | x <- pal_colour("red")
99 | expect_snapshot(x)
100 | })
101 |
102 | test_that("zero-length normal print method works", {
103 | x <- pal_colour()
104 | expect_snapshot(x)
105 | })
106 |
107 | cli::test_that_cli("palettes.print_symbol option works", {
108 | local_palettes_options(print_symbol = "square")
109 | x <- pal_colour("red")
110 | expect_snapshot(x)
111 | })
112 |
113 | test_that("palettes.print_symbol works with FALSE", {
114 | local_palettes_options(print_symbol = FALSE)
115 | x <- pal_colour("red")
116 | expect_snapshot(x)
117 | })
118 |
119 | cli::test_that_cli("palettes.print_hex option works", {
120 | local_palettes_options(print_hex = FALSE)
121 | x <- pal_colour("red")
122 | expect_snapshot(x)
123 | })
124 |
125 | test_that("palettes.print_alpha option works", {
126 | local_palettes_options(print_alpha = TRUE)
127 | x <- pal_colour(c("#88A0DCF0", "#381A61CC", "#7C4B73"))
128 | expect_snapshot(x)
129 | })
130 |
131 | test_that("palettes.print_sep option works", {
132 | local_palettes_options(print_sep = ", ")
133 | x <- pal_colour(c("red", "blue"))
134 | expect_snapshot(x)
135 | })
136 |
137 | test_that("palettes.print_width option works", {
138 | local_palettes_options(print_width = 2)
139 | x <- pal_colour(c("red", "blue", "green"))
140 | expect_snapshot(x)
141 | })
142 |
143 | test_that("palettes.print_index option works", {
144 | local_palettes_options(print_index = TRUE)
145 | x <- pal_colour(c("red", "blue", "green"))
146 | expect_snapshot(x)
147 | })
148 |
149 | test_that("disabling formatting works", {
150 | local_palettes_options(print_symbol = FALSE, print_hex = FALSE)
151 | x <- pal_colour(c("red", "#ff0000", "#FF0000"))
152 | expect_snapshot(x)
153 | })
154 |
155 | # ------------------------------------------------------------------------------
156 | # vec_c()
157 |
158 | test_that("vec_c(x, y) is class palettes_colour", {
159 | expect_s3_class(vec_c(pal_colour(), pal_colour()), "palettes_colour")
160 | })
161 |
162 | test_that("vec_c(x, y) and vec_c(y, x) are type character", {
163 | expect_type(vec_c(pal_colour(), character()), "character")
164 | expect_type(vec_c(character(), pal_colour()), "character")
165 | })
166 |
167 | test_that("inputs that cannot be combined fail with an error", {
168 | expect_error(vec_c(1, pal_colour()), class = "vctrs_error_incompatible_type")
169 | expect_error(vec_c(TRUE, pal_colour()), class = "vctrs_error_incompatible_type")
170 | })
171 |
172 | # ------------------------------------------------------------------------------
173 | # vec_ptype()
174 |
175 | test_that("ptype is correct", {
176 | expect_identical(vec_ptype(pal_colour("black")), pal_colour(character()))
177 | })
178 |
179 | # ------------------------------------------------------------------------------
180 | # vec_ptype_abbr()
181 |
182 | test_that("ptype abbreviation is correct", {
183 | expect_identical(vec_ptype_abbr(pal_colour()), "colour")
184 | })
185 |
186 | # ------------------------------------------------------------------------------
187 | # vec_arith()
188 |
189 | test_that("zero-length input works", {
190 | x <- pal_colour("black")
191 | expect_identical(x + pal_colour(character()), pal_colour(character()))
192 | expect_length(x + pal_colour(character()), 0)
193 | expect_true(is_colour(x + pal_colour(character())))
194 | })
195 |
196 | test_that("operators that are not supported fail with an error", {
197 | x <- pal_colour("black")
198 | expect_error(x - x, class = "vctrs_error_incompatible_op")
199 | })
200 |
201 | # ------------------------------------------------------------------------------
202 | # vec_math()
203 |
204 | test_that("sum works", {
205 | x <- pal_colour(c("red", "blue"))
206 | expect_identical(sum(x), x[1] + x[2])
207 | })
208 |
209 | test_that("cumsum works", {
210 | x <- pal_colour(c("red", "blue"))
211 | expect_length(cumsum(x), 2)
212 | expect_identical(cumsum(x)[2], sum(x))
213 | })
214 |
215 | test_that("functions that are not supported fail with an error", {
216 | x <- pal_colour(c("red", "blue"))
217 | expect_error(sqrt(x), "not implemented")
218 | })
219 |
--------------------------------------------------------------------------------
/tests/testthat/test-ggplot2-scales.R:
--------------------------------------------------------------------------------
1 | test_that("palettes scale changes point colour", {
2 | x <- pal_palette(pal = c("red", "green", "blue"))
3 | p1 <- ggplot2::ggplot(mtcars, ggplot2::aes(disp, mpg, colour = as.factor(cyl), fill = as.factor(cyl))) + ggplot2::geom_point()
4 | p2 <- p1 + scale_colour_palette_d(x)
5 | p3 <- p1 + scale_fill_palette_d(x)
6 | expect_false(any(ggplot2::layer_data(p1)$colour == ggplot2::layer_data(p2)$colour))
7 | expect_false(any(ggplot2::layer_data(p1)$fill == ggplot2::layer_data(p3)$fill))
8 | })
9 |
10 | test_that("binned scales only support continuous data", {
11 | x <- pal_colour(c("red", "green", "blue"))
12 | p1 <- ggplot2::ggplot(mtcars, ggplot2::aes(disp, mpg, colour = as.character(gear), fill = as.character(gear))) + ggplot2::geom_point()
13 | p2 <- p1 + scale_colour_palette_b(x)
14 | p3 <- p1 + scale_fill_palette_b(x)
15 | expect_snapshot_error(ggplot2::ggplot_build(p2))
16 | expect_snapshot_error(ggplot2::ggplot_build(p3))
17 | })
18 |
19 | test_that("only the first palette is used", {
20 | x <- pal_palette(pal1 = c("red", "green", "blue"), pal2 = c("blue", "green", "red"))
21 | y <- pal_colour(c("red", "green", "blue"))
22 | p1 <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg, colour = mpg, fill = mpg)) + ggplot2::geom_point()
23 | expect_warning(p1 + scale_colour_palette_c(x), "only the first")
24 | expect_warning(p1 + scale_fill_palette_c(x), "only the first")
25 | })
26 |
--------------------------------------------------------------------------------
/tests/testthat/test-pal_ramp.R:
--------------------------------------------------------------------------------
1 | test_that("colour vectors work", {
2 | x <- pal_colour(c("red", "blue"))
3 | expect_length(pal_ramp(x, n = 5), 5)
4 | })
5 |
6 | test_that("single colour vectors are repeated", {
7 | x <- pal_colour("#FF0000")
8 | expect_identical(unique(pal_ramp(x, n = 5)), x)
9 | })
10 |
11 | test_that("direction works", {
12 | x <- pal_colour(c("#FF0000", "#0000FF"))
13 | expect_identical(pal_ramp(x, direction = -1), rev(x))
14 | })
15 |
16 | test_that("colour palettes work", {
17 | x <- pal_palette(
18 | pal1 = c("red", "blue"),
19 | pal2 = c("purple", "yellow")
20 | )
21 | expect_length(pal_ramp(x, n = 5), 2)
22 | })
23 |
24 | test_that("objects that are not supported fail with an error", {
25 | x <- c("red", "blue")
26 | expect_error(pal_ramp(x), "no applicable method")
27 | })
28 |
--------------------------------------------------------------------------------
/tests/testthat/test-palette.R:
--------------------------------------------------------------------------------
1 | # ------------------------------------------------------------------------------
2 | # as_palette()
3 |
4 | test_that("can convert palette -> palette", {
5 | x <- list(black = "black")
6 | expect_identical(as_palette(new_palette(x)), new_palette(x))
7 | })
8 |
9 | test_that("can convert list -> palette", {
10 | colour_palette <- pal_palette(black = "black")
11 | colour_list <- list(black = "black")
12 | expect_identical(as_palette(colour_list), colour_palette)
13 | })
14 |
15 | # ------------------------------------------------------------------------------
16 | # as.list()
17 |
18 | test_that("as.list() works", {
19 | colour_palette <- pal_palette(black = "black")
20 | colour_list <- list(black = pal_colour("black"))
21 | expect_identical(as.list(colour_palette), colour_list)
22 | })
23 |
24 | test_that("as.list() works with NA", {
25 | expect_identical(as.list(pal_palette(NA)), list(pal_colour(NA_character_)))
26 | })
27 |
28 | # ------------------------------------------------------------------------------
29 | # as_tibble()
30 |
31 | test_that("as_tibble() works", {
32 | colour_palette <- pal_palette(palette = "black")
33 | colour_tibble <- tibble(palette = "palette", colour = pal_colour("black"))
34 | expect_identical(as_tibble(colour_palette), colour_tibble)
35 | })
36 |
37 | # ------------------------------------------------------------------------------
38 | # pal_palette()
39 |
40 | test_that("zero-length input works", {
41 | expect_s3_class(pal_palette(), "palettes_palette")
42 | expect_length(pal_palette(), 0)
43 | expect_true(is_palette(pal_palette()))
44 | })
45 |
46 | # ------------------------------------------------------------------------------
47 | # plot()
48 |
49 | test_that("plotting works", {
50 | x <- pal_palette(pal = c("red", "blue"))
51 | y <- pal_palette(pal1 = c("red", "blue"), pal2 = c("yellow", "purple"))
52 | expect_s3_class(plot(x), "gg")
53 | expect_s3_class(plot(y), "gg")
54 | })
55 |
56 | # ------------------------------------------------------------------------------
57 | # vec_c()
58 |
59 | test_that("vec_c(x, y) is class palettes_palette", {
60 | expect_s3_class(vec_c(pal_palette(), pal_palette()), "palettes_palette")
61 | })
62 |
63 | test_that("vec_c(x, y) and vec_c(y, x) are type list", {
64 | expect_type(vec_c(pal_palette(), list()), "list")
65 | expect_type(vec_c(list(), pal_palette()), "list")
66 | })
67 |
68 | test_that("inputs that cannot be combined fail with an error", {
69 | expect_error(vec_c("a", pal_palette()), class = "vctrs_error_incompatible_type")
70 | expect_error(vec_c(1, pal_palette()), class = "vctrs_error_incompatible_type")
71 | expect_error(vec_c(TRUE, pal_palette()), class = "vctrs_error_incompatible_type")
72 | })
73 |
74 | # ------------------------------------------------------------------------------
75 | # vec_is_list()
76 |
77 | test_that("palette is considered a list in the vctrs sense", {
78 | expect_identical(vec_is_list(pal_palette("black")), TRUE)
79 | })
80 |
81 | # ------------------------------------------------------------------------------
82 | # vec_ptype_abbr()
83 |
84 | test_that("ptype abbreviation is correct", {
85 | expect_identical(vec_ptype_abbr(pal_palette()), "palette")
86 | })
87 |
88 | # ------------------------------------------------------------------------------
89 | # vec_ptype_common()
90 |
91 | test_that("ptype is correct", {
92 | expect_identical(
93 | vec_ptype(pal_palette("black")),
94 | vec_ptype(pal_palette(character()))
95 | )
96 | })
97 |
--------------------------------------------------------------------------------
/tests/testthat/test-symbol.R:
--------------------------------------------------------------------------------
1 | cli::test_that_cli("unicode and ascii symbols work", {
2 | expect_snapshot(list_colour_symbols())
3 | })
4 |
--------------------------------------------------------------------------------
/vignettes/.gitignore:
--------------------------------------------------------------------------------
1 | *.html
2 | *.R
3 |
--------------------------------------------------------------------------------
/vignettes/articles/made-with-palettes.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Packages made with palettes"
3 | ---
4 |
5 | Colour palette packages are a nice way to distribute colour palettes while getting access to all the features of **palettes** for free. Below is a listing of colour palette packages made with palettes (please [let me know](https://github.com/mccarthy-m-g/palettes/tree/main/data-raw) if you have a package you’d like to see added to the list).
6 |
7 | See the vignette on creating a colour palette package to learn how to develop your own colour palette package; `vignette("creating-packages")`.
8 |
9 | ```{r, echo=FALSE, message=FALSE, warning=FALSE}
10 | library(palettes)
11 | library(dplyr)
12 | library(gt)
13 | library(fontawesome)
14 |
15 | palettes:::made_with_palettes |>
16 | rename(description = title, links = gh_url) |>
17 | gt(id = "one") |>
18 | cols_label(
19 | package = md("**Package**"),
20 | links = md("**Links**"),
21 | description = md("**Description**"),
22 | authors = md("**Author**")
23 | ) |>
24 | fmt_url(
25 | columns = links,
26 | label = html(fontawesome::fa("github")),
27 | color = "#0d6efd",
28 | show_underline = FALSE
29 | ) |>
30 | fmt_url(
31 | columns = cran_url,
32 | label = html(fontawesome::fa("r-project")),
33 | color = "#0d6efd",
34 | show_underline = FALSE
35 | ) |>
36 | sub_missing() |>
37 | cols_merge(
38 | columns = c(links, cran_url),
39 | pattern = "{1} \u2006 {2}"
40 | ) |>
41 | # cols_width(
42 | # package ~ pct(25),
43 | # title ~ pct(40),
44 | # authors ~ pct(35)
45 | # ) |>
46 | tab_style(
47 | style = cell_text(whitespace = "nowrap"),
48 | locations = cells_body(columns = links)
49 | ) |>
50 | tab_options(
51 | table.width = pct(100),
52 | table.font.size = "100%"
53 | ) |>
54 | opt_css(
55 | css = "
56 | table {
57 | hyphens: none !important;
58 | }
59 |
60 | #one [id='Package'] {
61 | width: 20%;
62 | }
63 |
64 | #one [id='Description'] {
65 | width: 40%;
66 | }
67 |
68 | #one [id='Author'] {
69 | width: 20%;
70 | }
71 |
72 | #one [id='Links'] {
73 | width: 20%;
74 | }
75 |
76 | #one svg {
77 | height: 1.25rem !important;
78 | width: 1.25rem !important;
79 | box-sizing: content-box;
80 | }
81 |
82 | @media screen and (max-width: 768px) {
83 |
84 | table th,
85 | table td {
86 | padding: .625em;
87 | }
88 |
89 | #one table thead {
90 | border: none;
91 | clip: rect(0 0 0 0);
92 | height: 1px;
93 | margin: -1px;
94 | overflow: hidden;
95 | padding: 0;
96 | position: absolute;
97 | width: 1px;
98 | }
99 |
100 | #one table tr {
101 | border-top-width: 2px;
102 | border-top-style: solid !important;
103 | border-top-color: #D3D3D3;
104 | border-bottom-width: 2px;
105 | border-bottom-style: solid !important;
106 | border-bottom-color: #D3D3D3;
107 | display: block;
108 | margin-bottom: .625em;
109 | padding: .35em;
110 | }
111 |
112 | #one table td,
113 | #one table svg {
114 | display: inline-block;
115 | width: calc(100% - 7rem - .625rem);
116 | padding-left: 7rem !important;
117 | text-indent: -7rem;
118 | vertical-align: middle !important;
119 | box-sizing: content-box;
120 | }
121 |
122 | #one table td::before {
123 | content: attr(headers);
124 | float: left;
125 | width: 7rem;
126 | font-weight: bold;
127 | text-transform: capitalize;
128 | }
129 |
130 | #one table td:last-child {
131 | border-bottom-width: 0px !important;
132 | }
133 |
134 | #one .gt_table,
135 | #one .gt_col_headings,
136 | #one .gt_table_body {
137 | border-top-style: none !important;
138 | border-bottom-style: none !important;
139 | }
140 |
141 | #one .gt_row {
142 | border-top-style: none !important;
143 | border-bottom-width: 1px;
144 | border-bottom-style: solid;
145 | border-bottom-color: #D3D3D3;
146 | margin: 0px !important;
147 | }
148 |
149 | }
150 | "
151 | )
152 | ```
153 |
--------------------------------------------------------------------------------
/vignettes/biscale.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Using palettes with biscale"
3 | output: rmarkdown::html_vignette
4 | vignette: >
5 | %\VignetteIndexEntry{Using palettes with biscale}
6 | %\VignetteEngine{knitr::rmarkdown}
7 | %\VignetteEncoding{UTF-8}
8 | ---
9 |
10 | ```{r, include = FALSE}
11 | # Preview vignette with: devtools::build_rmd("vignettes/biscale.Rmd")
12 | knitr::opts_chunk$set(
13 | collapse = TRUE,
14 | comment = "#>"
15 | )
16 | ```
17 |
18 | This vignette shows you how to use palettes as bivariate colour and fill scales with **biscale**.
19 |
20 | ```{r setup}
21 | library(palettes)
22 | library(ggplot2)
23 | library(patchwork)
24 | library(biscale)
25 | ```
26 |
27 | ## Custom palettes
28 |
29 | Colour vectors are naturally compatible with biscale, but with two important differences:
30 |
31 | - Colour vectors must be named
32 | - Colours in the vector must be specified as hexadecimal strings of the form "#rrggbb"
33 |
34 | The name of each colour is used to specify its location in the bivariate scale.
35 |
36 | ```{r}
37 | named_colour_vector <- pal_colour(c(
38 | "1-1" = "#d3d3d3", # low x, low y
39 | "2-1" = "#9e3547", # high x, low y
40 | "1-2" = "#4279b0", # low x, high y
41 | "2-2" = "#311e3b" # high x, high y
42 | ))
43 |
44 | named_colour_vector
45 |
46 | names(named_colour_vector)
47 | ```
48 |
49 | Names can also be added to unnamed colour vectors with `names()`:
50 |
51 | ```{r}
52 | unnamed_colour_vector <- pal_colour(
53 | c("#d3d3d3", "#9e3547", "#4279b0", "#311e3b")
54 | )
55 |
56 | names(unnamed_colour_vector)
57 |
58 | names(unnamed_colour_vector) <- c("1-1", "2-1", "1-2", "2-2")
59 |
60 | names(unnamed_colour_vector)
61 | ```
62 |
63 | To preview the bivariate palette use `biscale::bi_pal()`:
64 |
65 | ```{r}
66 | bi_pal(named_colour_vector, dim = 2)
67 | ```
68 |
69 | ## Creating maps
70 |
71 | To create maps with colour vectors and colour palettes, we can follow the general workflow covered in the [Get started](https://chris-prener.github.io/biscale/articles/biscale.html) article in biscale. The article demonstrates how to create bivariate scales using race and income data from U.S. Census tracts for the City of St. Louis in Missouri. We will recreate that map here using a custom colour vector.
72 |
73 | We begin by mapping race (the percentage of white residents) and median income values to a bivariate scale with `biscale::bi_class()`:
74 |
75 | ```{r}
76 | race_income <- bi_class(
77 | stl_race_income,
78 | x = pctWhite,
79 | y = medInc,
80 | dim = 3,
81 | style = "quantile",
82 | keep_factors = TRUE
83 | )
84 | ```
85 |
86 | Then create our named colour vector. There are more colours here than in the previous example because we will be using a three-by-three bivariate map instead of a two-by-two map.
87 |
88 | ```{r}
89 | named_colour_vector <- pal_colour(c(
90 | "1-1" = "#d3d3d3", # low x, low y
91 | "2-1" = "#ba8890",
92 | "3-1" = "#9e3547", # high x, low y
93 | "1-2" = "#8aa6c2",
94 | "2-2" = "#7a6b84", # medium x, medium y
95 | "3-2" = "#682a41",
96 | "1-3" = "#4279b0", # low x, high y
97 | "2-3" = "#3a4e78",
98 | "3-3" = "#311e3b" # high x, high y
99 | ))
100 | ```
101 |
102 | The bivariate legend used in biscale is actually a ggplot2 plot, so we create the map and legend separately, then combine them. Here we combine the map and legend using `patchwork::inset_element()`:
103 |
104 | ```{r}
105 | #| fig.dim = c(8, 8),
106 | #| out.width = "100%"
107 | # Draw map with a bivariate fill scale
108 | race_income_plot <- ggplot(race_income, aes(fill = bi_class)) +
109 | geom_sf(color = "white", size = 0.1, show.legend = FALSE) +
110 | bi_scale_fill(pal = named_colour_vector, dim = 3) +
111 | labs(
112 | title = "Race and Income in St. Louis, MO",
113 | caption = "Breaks for percent white are 14.0% and 62.0% (range is 0-96.7%).
114 | Breaks for median income are $26,200 and $43,900
115 | (range is $10,500-$74,400)."
116 | ) +
117 | bi_theme()
118 |
119 | # Draw the bivariate legend
120 | bivariate_legend <- bi_legend(
121 | pal = named_colour_vector,
122 | dim = 3,
123 | xlab = "Higher % White ",
124 | ylab = "Higher Income ",
125 | size = 7
126 | )
127 |
128 | # Combine the map and bivariate legend
129 | race_income_plot +
130 | inset_element(
131 | bivariate_legend,
132 | left = 0,
133 | bottom = 0.8,
134 | right = 0.5,
135 | top = 1,
136 | align_to = "plot"
137 | )
138 | ```
139 |
--------------------------------------------------------------------------------
/vignettes/compatibility.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Compatibility with other colour packages"
3 | output: rmarkdown::html_vignette
4 | vignette: >
5 | %\VignetteIndexEntry{Compatibility with other colour packages}
6 | %\VignetteEngine{knitr::rmarkdown}
7 | %\VignetteEncoding{UTF-8}
8 | ---
9 |
10 | ```{r, include = FALSE}
11 | # Preview vignette with: devtools::build_rmd("vignettes/compatibility.Rmd")
12 | knitr::opts_chunk$set(
13 | collapse = TRUE,
14 | comment = "#>"
15 | )
16 | ```
17 |
18 | ```{r setup}
19 | library(palettes)
20 | ```
21 |
22 | ## Overview
23 |
24 | Because palettes supports casting and coercion for colour vectors, it is generally compatible with other colour packages and functions that accept or return colours as a character vector of `"#RRGGBB"` or `"#RRGGBBAA"`, colour names from `grDevices::colors()`, or a positive integer that indexes into `grDevices::palette()`.
25 |
26 | This vignette shows how to cast and coerce colour vectors with a select number of colour packages. We use the following colour vector for demonstration.
27 |
28 | ```{r}
29 | colour_vector <- pal_colour(
30 | c("#a00e00", "#d04e00", "#f6c200", "#0086a8", "#132b69")
31 | )
32 | colour_vector
33 | ```
34 |
35 | The same approaches below will also work for single colour palettes extracted as a colour vector using `[[` or `$`. See the "Subsetting" section in `vignette("palettes", package = "palettes")` for more details.
36 |
37 | ## colorspace
38 |
39 | ```{r}
40 | library(colorspace)
41 | ```
42 |
43 | To turn colour vectors into sRGB objects, pass them to `colorspace::hex2RGB()`.
44 |
45 | ```{r}
46 | colour_matrix <- hex2RGB(colour_vector)
47 | colour_matrix
48 | ```
49 |
50 | To convert colour matrices into a different colour space use `as()`.
51 |
52 | ```{r}
53 | as(colour_matrix, "HLS")
54 | ```
55 |
56 | To turn colour matrices from any colour space back into colour vectors use `colorspace::hex()` and `as_colour()`.
57 |
58 | ```{r}
59 | colour_strings <- hex(colour_matrix)
60 | colour_strings
61 |
62 | as_colour(colour_strings)
63 | ```
64 |
65 | ## farver
66 |
67 | ```{r}
68 | library(farver)
69 | ```
70 |
71 | To turn colour vectors into the standard form expected by farver, pass them to `farver::decode_colour()`.
72 |
73 | ```{r}
74 | colour_matrix <- decode_colour(colour_vector)
75 | colour_matrix
76 | ```
77 |
78 | To convert colour matrices into a different colour space use `as()`.
79 |
80 | ```{r}
81 | convert_colour(colour_matrix, "rgb", "lab")
82 | ```
83 |
84 | To turn colour matrices back into colour vectors use `farver::encode_colour()` and `as_colour()`.
85 |
86 | ```{r}
87 | colour_strings <- encode_colour(colour_matrix)
88 | colour_strings
89 |
90 | as_colour(colour_strings)
91 | ```
92 |
93 | ## grDevices
94 |
95 | ```{r}
96 | library(grDevices)
97 | ```
98 |
99 | To turn colour vectors into the standard form expected by grDevices, pass them to `grDevices::col2rgb()`.
100 |
101 | ```{r}
102 | colour_matrix <- col2rgb(colour_vector)
103 | colour_matrix
104 | ```
105 |
106 | To convert colour matrices into a different colour space use `grDevices::convertColor()` with the transpose of the colour matrix.
107 |
108 | ```{r}
109 | convertColor(t(colour_matrix), "sRGB", "Lab")
110 | ```
111 |
112 | To turn colour matrices back into colour vectors use `grDevices::rgb()` and `as_colour()`.
113 |
114 | ```{r}
115 | colour_strings <- rgb(
116 | r = colour_matrix[1, ], g = colour_matrix[2, ], b = colour_matrix[3, ],
117 | maxColorValue = 255
118 | )
119 | colour_strings
120 |
121 | as_colour(colour_strings)
122 | ```
123 |
--------------------------------------------------------------------------------
/vignettes/creating-packages.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Creating a colour palette package"
3 | output: rmarkdown::html_vignette
4 | vignette: >
5 | %\VignetteIndexEntry{Creating a colour palette package}
6 | %\VignetteEngine{knitr::rmarkdown}
7 | %\VignetteEncoding{UTF-8}
8 | ---
9 |
10 | This vignette shows you how to create a colour palette package with **palettes**.
11 |
12 | ## Set up advice
13 |
14 | This vignette assumes that you are already familiar with R package development and have [setup your system for creating R packages](https://r-pkgs.org/setup.html). If this is your first time creating an R package, the book [R Packages](https://r-pkgs.org) by Hadley Wickham and Jenny Bryan gives you all the tools you need to start, and I highly recommend using it as a supplement to this vignette.
15 |
16 | A good way to check that you are ready to create a colour palette package is to run `devtools::dev_sitrep()`, which prints info about your development setup. If this reveals that certain tools or packages are missing or out-of-date, you are encouraged to update them.
17 |
18 | ## Attach devtools
19 |
20 | All the code below assumes you've attached **devtools** in your R session:
21 |
22 | ```{r eval = FALSE}
23 | library(devtools)
24 | ```
25 |
26 | You can initiate your new package from any active R session. You don’t need to worry about whether you’re in an existing or new project or not. The functions we use take care of this.
27 |
28 | ## Example package: sunsets
29 |
30 | We will use various functions from devtools and palettes to build a small example package from scratch. We call the package **sunsets** and you can take a peek at the finished product we are working towards by visiting sunsets on GitHub: .
31 |
32 | ## Creating the package
33 |
34 | The first step is to create your R package and set up its basic structure. There are many ways to accomplish this, but here we demonstrate `usethis::create_package()`:
35 |
36 | ```{r eval=FALSE}
37 | create_package("~/path/to/sunsets")
38 | ```
39 |
40 | If `create_package()` package drops you into a fresh R session in your new package, you probably need to call `library(devtools)` again. You can also set the `open` argument to `FALSE` in `create_package()` if you are using it in the same session your R package will be created in.
41 |
42 | ### Adding package metadata
43 |
44 | At this point we can add important metadata about what our package does in `DESCRIPTION` with `usethis::use_package()`:
45 |
46 | ```{r eval=FALSE}
47 | use_description(
48 | fields = list(
49 | Title = "Sunset Colour Palettes",
50 | `Authors@R` =
51 | 'person("First", "Last", , "first.last@example.com", c("aut", "cre"))',
52 | Description = paste0(
53 | "Colour palettes inspired by sunsets in the Canadian Prairies. ",
54 | "Built using the 'palettes' package, which provides methods for ",
55 | "printing, formatting, casting and coercion, extraction and updating ",
56 | "of components, plotting, colour mixing arithmetic, and colour ",
57 | "interpolation."
58 | ),
59 | URL = "https://github.com/user/repo",
60 | BugReports = "https://github.com/user/repo/issues"
61 | )
62 | )
63 | ```
64 |
65 | ### Choosing a license
66 |
67 | Because our package will primarily contain colour palette data, not code, we recommend choosing a [Creative Commons](https://creativecommons.org) license:
68 |
69 | - For minimal restrictions choose the [CC0](https://choosealicense.com/licenses/cc0-1.0/) license with `usethis::use_cc0_license()`
70 | - To require attribution when your colour palettes are used, choose the [CC BY](https://choosealicense.com/licenses/cc-by-4.0/) license with `usethis::use_ccby_license()`
71 |
72 | For sunsets we choose the [CC0](https://choosealicense.com/licenses/cc0-1.0/) license with `usethis::use_cc0_license()`:
73 |
74 | ```{r eval=FALSE}
75 | use_cc0_license()
76 | ```
77 |
78 | ### Creating colour palettes
79 |
80 | Colour palette packages made with palettes are examples of so-called [“data packages”](https://r-pkgs.org/data.html)---they exist solely for the purpose of distributing colour palettes created with `pal_palette()`, along with their documentation.
81 |
82 | To make it easy to update or reproduce our colour palettes, we first set up a data-creating script that includes the code used to create them with `usethis::use_data_raw()`:
83 |
84 | ```{r eval=FALSE}
85 | use_data_raw(name = "sunset_palettes")
86 | ```
87 |
88 | For the sunsets package our final script for creating colour palettes looks like this:
89 |
90 | ```{r eval=FALSE}
91 | library(palettes)
92 |
93 | # Discrete palettes -----------------------------------------------------------
94 | sunset_palettes_discrete <- pal_palette(
95 | light = c("#dc8951", "#4E8AC9", "#F5D06D", "#69ca97", "#c978b5"),
96 | dark = c("#EE6720", "#0792C9", "#E4B854", "#24B079", "#9A4F80")
97 | )
98 |
99 | plot(sunset_palettes_discrete)
100 |
101 | usethis::use_data(sunset_palettes_discrete, overwrite = TRUE)
102 |
103 | # Sequential palettes ---------------------------------------------------------
104 | sunset_palettes_sequential <- pal_palette(
105 | orange = pal_ramp(pal_colour(c("#EE6720", "#FBE0D1")), n = 7),
106 | blue = pal_ramp(pal_colour(c("#0792C9", "#C1D1EB")), n = 7),
107 | yellow = pal_ramp(pal_colour(c("#E4B854", "#FFECC8")), n = 7),
108 | green = pal_ramp(pal_colour(c("#24B079", "#C9EBD7")), n = 7),
109 | purple = pal_ramp(pal_colour(c("#9A4F80", "#ECCCE2")), n = 7)
110 | )
111 |
112 | plot(sunset_palettes_sequential)
113 |
114 | usethis::use_data(sunset_palettes_sequential, overwrite = TRUE)
115 |
116 | # All palettes ----------------------------------------------------------------
117 | sunset_palettes <- c(
118 | sunset_palettes_sequential,
119 | sunset_palettes_discrete
120 | )
121 |
122 | plot(sunset_palettes)
123 |
124 | usethis::use_data(sunset_palettes, overwrite = TRUE)
125 | ```
126 |
127 | This demonstrates three important points about making colour palettes:
128 |
129 | - Palettes can be created manually, as in the "discrete palettes" section
130 | - Palettes can be made programmatically, as in the "sequential palettes" section
131 | - Multiple palette objects can coerced together, as in the "all palettes" section
132 |
133 | To store these colour palettes and make them available to users of our package, we save them as `.rda` files in the `data/` directory with `usethis::use_data()`.
134 |
135 | ### Attaching the palettes package
136 |
137 | Our colour palettes in `data/` are always effectively exported, which can be seen by running `devtools::load_all()` then running `sunset_palettes` in the console. However, to make the functions from palettes available to users of sunsets, we need to add palettes as a dependency and attach it at runtime. We can add the necessary text to `DESCRIPTION` with `usethis::use_package()`:
138 |
139 | ```{r eval=FALSE}
140 | usethis::use_package("palettes", "Depends")
141 | ```
142 |
143 | We use `Depends` instead of `Imports` because sunsets is designed to be used in conjunction with palettes and is not useful without it. This is a [valid exception](https://r-pkgs.org/dependencies-mindset-background.html#sec-dependencies-imports-vs-depends) to the general advice to always use `Imports`.
144 |
145 | We also need to import palettes in our package's `NAMESPACE` for its methods to be available when sunsets is loaded but not attached (i.e., `sunsets::my_fun()`). To import palettes in our package's `NAMESPACE`, we create a basic package-level documentation script in `R/` where we will import palettes with `usethis::use_package_doc()`:
146 |
147 | ```{r eval=FALSE}
148 | use_package_doc()
149 | ```
150 |
151 | Then in `R/sunsets-package.R` we write **roxygen2** blocks to document our colour palettes, and run `devtools::document()` when we're finished. For the sunsets package our documentation looks like this:
152 |
153 | ```{r eval=FALSE}
154 | #' @keywords internal
155 | "_PACKAGE"
156 |
157 | #' Internal palettes methods
158 | #'
159 | #' @import palettes
160 | #' @keywords internal
161 | #' @name sunsets-palettes
162 | NULL
163 | ```
164 |
165 | ### Documenting colour palettes
166 |
167 | Our colour palettes in `data/` are always effectively exported, but we still need to document them. To document our colour palettes, we create a script in `R/` where we will document the names of our colour palettes with `usethis::use_r()`:
168 |
169 | ```{r eval=FALSE}
170 | usethis::use_r("palettes")
171 | ```
172 |
173 | You can name the script anything you like, but we suggest "palettes" if all your colour palette documentation will be in a single file.
174 |
175 | Then in `R/palettes.R` we write roxygen2 blocks to document our colour palettes, and run `devtools::document()` when we're finished. For the sunsets package our documentation looks like this:
176 |
177 | ```{r eval=FALSE}
178 | #' Sunset palettes
179 | #'
180 | #' Palettes inspired by sunsets in the Canadian Prairies.
181 | #'
182 | #' @format ## `sunset_palettes`
183 | #' An object of class `palettes_palette` with `r length(sunset_palettes)`
184 | #' colour palettes. Use `names(sunset_palettes)` to return all palette names.
185 | #' @source
186 | #' @author [Michael McCarthy](https://github.com/mccarthy-m-g)
187 | #' @seealso [pal_palette()], [pal_colour()]
188 | #' @examples
189 | #' # Get all palettes by name.
190 | #' names(sunset_palettes)
191 | #'
192 | #' # Plot all palettes.
193 | #' plot(sunset_palettes)
194 | "sunset_palettes"
195 |
196 | #' @rdname sunset_palettes
197 | #' @format ## `sunset_palettes_discrete`
198 | #' An object of class `palettes_palette` with
199 | #' `r length(sunset_palettes_discrete)` discrete colour palettes.
200 | #' Use `names(sunset_palettes_discrete)` to return all palette names.
201 | "sunset_palettes_discrete"
202 |
203 | #' @rdname sunset_palettes
204 | #' @format ## `sunset_palettes_sequential`
205 | #' An object of class `palettes_palette` with
206 | #' `r length(sunset_palettes_sequential)` sequential colour palettes.
207 | #' Use `names(sunset_palettes_sequential)` to return all palette names.
208 | "sunset_palettes_sequential"
209 | ```
210 |
211 | ### Adding `README.md` and `NEWS.md`
212 |
213 | You now have a package that is ready to submit to CRAN. But before you do, you should update two important documentation files:
214 |
215 | - `README.md`, which describes what the package does
216 | - `NEWS.md`, which describes what’s changed since the previous version
217 |
218 | See the [Documentation](https://r-pkgs.org/other-markdown.html) chapter in [R Packages](https://r-pkgs.org) by Hadley Wickham and Jenny Bryan for advice on writing these files.
219 |
220 | If you want to include printed colour previews of your colour palettes in `README.md` like there are in palettes' [`README.md`](https://github.com/mccarthy-m-g/palettes/blob/main/README.md), you will need to write a `README.Rmd` file that uses the [asciicast](https://github.com/r-lib/asciicast) knitr engine. For a working example, refer to the code in palettes' [`README.Rmd`](https://github.com/r-lib/asciicast/blob/main/README.Rmd) file.
221 |
222 | Optionally, you might also consider adding [vignettes](https://r-pkgs.org/vignettes.html) or a [pkgdown website](https://r-pkgs.org/website.html) to your package.
223 |
224 | ## Package maintenance
225 |
226 | Because colour palette packages made with palettes are data packages, they are very low maintenance. You will typically only need to update them if you are changing or updating your colour palettes. Moreover, when new features are added to palettes, users of your package can gain access to them by installing the new update---you do not need to make any changes to your own package to make them available.
227 |
228 | ## Distributing your package
229 |
230 | See the [Maintenance and distribution](https://r-pkgs.org/check.html) chapter in [R Packages](https://r-pkgs.org) by Hadley Wickham and Jenny Bryan for advice on distributing your package.
231 |
--------------------------------------------------------------------------------
/vignettes/ggplot2.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Using palettes with ggplot2"
3 | output: rmarkdown::html_vignette
4 | vignette: >
5 | %\VignetteIndexEntry{Using palettes with ggplot2}
6 | %\VignetteEngine{knitr::rmarkdown}
7 | %\VignetteEncoding{UTF-8}
8 | ---
9 |
10 | ```{r, include = FALSE}
11 | # Preview vignette with: devtools::build_rmd("vignettes/ggplot2.Rmd")
12 | knitr::opts_chunk$set(
13 | collapse = TRUE,
14 | comment = "#>"
15 | )
16 | ```
17 |
18 | This vignette shows you how to use palettes as colour and fill scales with **ggplot2**.
19 |
20 | ```{r setup}
21 | library(palettes)
22 | library(ggplot2)
23 | library(scales)
24 | ```
25 |
26 | ## Colour scales
27 |
28 | For discrete colours use `scale_colour_palette_d()`.
29 |
30 | ```{r}
31 | ggplot(diamonds[sample(nrow(diamonds), 1000), ], aes(carat, price)) +
32 | geom_point(aes(colour = clarity)) +
33 | scale_colour_palette_d(met_palettes$VanGogh3)
34 | ```
35 |
36 | Continuous or binned colours can be used with continuous data. For continuous colours use `scale_colour_palette_c()`. For binned colours use `scale_colour_palette_b()`.
37 |
38 | ```{r, out.width = "45%", fig.show = "hold"}
39 | hwy_mpg <- ggplot(mpg, aes(displ, hwy, colour = hwy)) +
40 | geom_point()
41 |
42 | hwy_mpg + scale_colour_palette_c(met_palettes$Greek)
43 |
44 | hwy_mpg + scale_colour_palette_b(met_palettes$Greek)
45 | ```
46 |
47 | ## Fill scales
48 |
49 | For discrete fills use `scale_fill_palette_d()`.
50 |
51 | ```{r}
52 | ggplot(diamonds, aes(x = price, fill = cut)) +
53 | geom_histogram(position = "dodge", binwidth = 1000) +
54 | scale_fill_palette_d(pnw_palettes$Sunset)
55 | ```
56 |
57 | Continuous or binned fills can be used with continuous data. For continuous fills use `scale_fill_palette_c()`. For binned fills use `scale_fill_palette_b()`.
58 |
59 | ```{r, out.width = "45%", fig.show = "hold"}
60 | eruptions <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
61 | geom_tile()
62 |
63 | eruptions + scale_fill_palette_c(viridis_palettes$mako)
64 |
65 | eruptions + scale_fill_palette_b(viridis_palettes$mako)
66 | ```
67 |
68 | ## Customizing scales
69 |
70 | The `scale_` functions can all pass arguments to the appropriate scale constructors in ggplot2 if you need to control the name, limits, breaks, labels, and so forth of colour and fill scales. The arguments are passed with `...` to the appropriate scale constructor for each function:
71 |
72 | - `ggplot2::discrete_scale()` for `scale_colour_palette_d()` and `scale_fill_palette_d()`
73 | - `ggplot2::continuous_scale()` for `scale_colour_palette_c()` and `scale_fill_palette_c()`
74 | - `ggplot2::binned_scale()` for `scale_colour_palette_b()` and `scale_fill_palette_b()`
75 |
76 | This is particularly useful when you need to change how colours are mapped to values in a scale. For example, a common problem when working with divergent colour palettes is getting the middle colour in a palette to align with a fixed value representing the mid-point of a continuous scale.
77 |
78 | To demonstrate, say we have mean treatment outcome results from four groups in a clinical trial.
79 |
80 | ```{r}
81 | treatment_results <- data.frame(
82 | group = LETTERS[1:4],
83 | outcome = c(-0.5, -1.1, 1.9, 2.3)
84 | )
85 | ```
86 |
87 | We want to plot the outcomes as a diverging bar chart with divergent fills. By default, the fill scale's mid-point is the mean of the four groups, but we want it to be zero.
88 |
89 | ```{r}
90 | ggplot(treatment_results, aes(x = group, y = outcome, fill = outcome)) +
91 | geom_col() +
92 | scale_fill_palette_c(met_palettes$Benedictus)
93 | ```
94 |
95 | We can use the `rescaler` argument in `ggplot2::continuous_scale()`, which accepts a function used to scale the input values to the range [0, 1], to scale the fill values to have a mid-point of zero. For scaling the mid-point use `scales::rescale_mid()`.
96 |
97 | ```{r}
98 | ggplot(treatment_results, aes(x = group, y = outcome, fill = outcome)) +
99 | geom_col() +
100 | scale_fill_palette_c(
101 | met_palettes$Benedictus,
102 | rescaler = ~ rescale_mid(.x, mid = 0)
103 | )
104 | ```
105 |
106 |
107 |
--------------------------------------------------------------------------------
/vignettes/gt.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Using palettes with gt"
3 | output: rmarkdown::html_vignette
4 | vignette: >
5 | %\VignetteIndexEntry{Using palettes with gt}
6 | %\VignetteEngine{knitr::rmarkdown}
7 | %\VignetteEncoding{UTF-8}
8 | ---
9 |
10 | ```{r, include = FALSE}
11 | # Preview vignette with: devtools::build_rmd("vignettes/gt.Rmd")
12 | knitr::opts_chunk$set(
13 | collapse = TRUE,
14 | comment = "#>"
15 | )
16 | ```
17 |
18 | This vignette shows you how to use palettes to colour data cells with **gt**.
19 |
20 | ```{r setup, message = FALSE, warning = FALSE}
21 | library(palettes)
22 | library(gt)
23 | library(scales)
24 | library(dplyr)
25 | ```
26 |
27 | ## Preparing the table
28 |
29 | To demonstrate how to use palettes to colour data cells with gt, we will use `airquality` as an input data table with the following columns:
30 |
31 | - Year, Month, Day: the numeric month and day of month for the record
32 | - Temp: maximum daily air temperature in degrees Fahrenheit (°F)
33 |
34 | Temperature is a good fit for colour---we can represent cold with blue and hot with red.
35 |
36 | First we create the gt table we will add colour to. The table is slightly modified from a table in the [Introduction to Creating gt Tables](https://gt.rstudio.com/articles/intro-creating-gt-tables.html#the-column-labels) article. It looks like this.
37 |
38 | ```{r}
39 | # Modify the `airquality` dataset by adding the year
40 | # of the measurements (1973) and limiting to 10 rows
41 | airquality_m <-
42 | airquality %>%
43 | mutate(Year = 1973L) %>%
44 | slice(1:10) %>%
45 | select(Year, Month, Day, Temp)
46 |
47 | # Create a display table using the `airquality`
48 | # dataset; arrange columns into groups
49 | gt_tbl <-
50 | gt(airquality_m) %>%
51 | tab_header(
52 | title = "New York Temperature Measurements",
53 | subtitle = "Daily measurements in New York City (May 1-10, 1973)"
54 | ) %>%
55 | tab_spanner(
56 | label = "Time",
57 | columns = c(Year, Month, Day)
58 | ) %>%
59 | tab_spanner(
60 | label = "Measurement",
61 | columns = c(Temp)
62 | ) %>%
63 | cols_label(
64 | Temp = html("Temp,
°F")
65 | )
66 |
67 | # Show the gt table
68 | gt_tbl
69 | ```
70 |
71 | ## Adding colour
72 |
73 | We can use the `Hiroshige` palette for blue and red gradients, reversing it so the colours are in the correct order for this example.
74 |
75 | ```{r}
76 | colour_vector <- rev(met_palettes$Hiroshige)
77 | colour_vector
78 | ```
79 |
80 | At the moment, objects of class `palettes_palette` or `palettes_colour` cannot be used directly with the colour functions in gt. To work around this we cast the colour vector to a character vector.
81 |
82 | ```{r}
83 | character_vector <- as.character(colour_vector)
84 | character_vector
85 | ```
86 |
87 | Now we can use `gt::data_color()` to colour the temperature cells. The `colors` argument accepts either a vector of colours to use for each distinct cell value or level or a colour mapping function (e.g., from the **scales** package).
88 |
89 | Here we pass the character vector directly to the `colors` argument.
90 |
91 | ```{r}
92 | gt_tbl %>%
93 | data_color(
94 | columns = Temp,
95 | palette = character_vector
96 | )
97 | ```
98 |
99 | But this works equally well with the colour mapping functions from palettes.
100 |
101 | ```{r}
102 | gt_tbl %>%
103 | data_color(
104 | columns = Temp,
105 | fn = pal_numeric(colour_vector, domain = NULL)
106 | )
107 | ```
108 |
109 | The colour mapping functions from palettes are: `pal_numeric()`, `pal_bin()`, `pal_quantile()`, and `pal_factor()`. These functions are useful when you need finer control over colour evaluation with data.
110 |
--------------------------------------------------------------------------------
/vignettes/palettes.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "palettes"
3 | output: rmarkdown::html_vignette
4 | vignette: >
5 | %\VignetteIndexEntry{palettes}
6 | %\VignetteEngine{knitr::rmarkdown}
7 | %\VignetteEncoding{UTF-8}
8 | ---
9 |
10 | ```{r, include = FALSE}
11 | # Preview vignette with: devtools::build_rmd("vignettes/palettes.Rmd")
12 | knitr::opts_chunk$set(
13 | collapse = TRUE,
14 | comment = "#>"
15 | )
16 | ```
17 |
18 | The goal of palettes is to provide methods for working with colour palettes for users and developers.
19 |
20 | ```{r setup}
21 | library(palettes)
22 | ```
23 |
24 | ## Creating
25 |
26 | `pal_colour()` is a nice way to create a colour vector. Colours can be a character vector of hexadecimal strings of the form `"#RRGGBB"` or `"#RRGGBBAA"`, colour names from `grDevices::colors()`, or a positive integer that indexes into `grDevices::palette()`. By default, colour vectors are always printed as hex codes with colour previews.
27 |
28 | ```{r}
29 | colour_vector <- pal_colour(
30 | c("#a00e00", "#d04e00", "#f6c200", "#0086a8", "#132b69")
31 | )
32 |
33 | colour_vector
34 | ```
35 |
36 | `pal_palette()` is a nice way to create named colour palettes.
37 |
38 | ```{r}
39 | colour_palette <- pal_palette(
40 | egypt = c("#dd5129", "#0f7ba2", "#43b284", "#fab255"),
41 | java = c("#663171", "#cf3a36", "#ea7428", "#e2998a", "#0c7156")
42 | )
43 |
44 | colour_palette
45 | ```
46 |
47 | ## Casting and coercion
48 |
49 | To compliment `pal_colour()`, palettes provides `as_colour()` to cast objects into colour vectors.
50 |
51 | ```{r}
52 | colour_strings <- c("orange", "purple")
53 | as_colour(colour_strings)
54 | ```
55 |
56 | To compliment `pal_palette()`, palettes provides `as_palette()` to cast objects into colour palettes.
57 |
58 | ```{r}
59 | colour_list <- list(OrPu = c("orange", "purple"))
60 | as_palette(colour_list)
61 | ```
62 |
63 | Colour vectors and colour palettes can also be coerced into a tibble with `as_tibble()`. See `vignette("tibble", package = "tibble")` for an overview of tibbles.
64 |
65 | ```{r}
66 | as_tibble(colour_vector)
67 |
68 | as_tibble(colour_palette)
69 | ```
70 |
71 |
72 |
73 | ## Subsetting
74 |
75 | Colour vectors can be subset using `[`.
76 |
77 | - To extract one or more colours use positive integers:
78 |
79 | ```{r}
80 | colour_vector[3]
81 | ```
82 |
83 | - To drop one or more colours use negative integers:
84 |
85 | ```{r}
86 | colour_vector[-3]
87 | ```
88 |
89 | - To move one or more colours extract, drop, and combine:
90 |
91 | ```{r}
92 | c(colour_vector[-3], colour_vector[3])
93 | ```
94 |
95 | Colour palettes can be subset using `[`, `[[`, and `$`.
96 |
97 | - To extract one or more colour palettes use `[`:
98 |
99 | ```{r}
100 | colour_palette["egypt"]
101 | ```
102 |
103 | - To extract a single colour palette as a colour vector use `[[` or `$`:
104 |
105 | ```{r}
106 | colour_palette[["egypt"]]
107 |
108 | colour_palette$egypt
109 | ```
110 |
111 | - To get names of colour palettes use `names()`:
112 |
113 | ```{r}
114 | names(colour_palette)
115 | ```
116 |
117 |
130 |
131 | ## Plotting
132 |
133 | `plot()` is a nice way to showcase colour vectors and colour palettes. The appearance of the plot depends on the input.
134 |
135 | - Colour vectors are plotted as swatches:
136 |
137 | ```{r}
138 | plot(colour_vector)
139 | ```
140 |
141 | - Single colour palettes are plotted as swatches with a palette name overlay:
142 |
143 | ```{r}
144 | plot(colour_palette["egypt"])
145 | ```
146 |
147 | - Multiple colour palettes are plotted as faceted swatches with palette name titles:
148 |
149 | ```{r}
150 | plot(colour_palette)
151 | ```
152 |
153 | To interpolate or change the direction of colours in a plot, use the optional `n`, `direction`, `space`, or `interpolate` arguments.
154 |
155 | ```{r}
156 | plot(colour_vector, n = 7, direction = -1, interpolate = "linear")
157 | ```
158 |
159 | All plots are ggplot2 objects and can be customized using any of the standard ggplot2 methods. See the ggplot2 [customizing FAQ](https://ggplot2.tidyverse.org/articles/faq-customising.html) for some common examples.
160 |
161 | ## Printing
162 |
163 | ```{r, include=FALSE}
164 | op <- options()
165 | ```
166 |
167 | The printing behaviour of colour vectors can be adjusted using a variety of global options. See `help("palettes-options")` for a list of all the available options and their default values.
168 |
169 | For example, to change the symbol used for colour previews, set the `palettes.print_symbol` option.
170 |
171 | ```{r}
172 | options(palettes.print_symbol = "square")
173 | colour_vector
174 | ```
175 |
176 | ```{r, include=FALSE}
177 | options(op)
178 | ```
179 |
180 | Set multiple options together for unique printing styles.
181 |
182 | - Print colour vectors compactly:
183 |
184 | ```{r}
185 | options(
186 | palettes.print_symbol = "circle_large",
187 | palettes.print_hex = FALSE,
188 | palettes.print_width = 5
189 | )
190 | colour_palette
191 | ```
192 |
193 | ```{r, include=FALSE}
194 | options(op)
195 | ```
196 |
197 | - Mimic the appearance of a character vector:
198 |
199 | ```{r}
200 | options(
201 | palettes.print_symbol = FALSE,
202 | palettes.print_sep = ", ",
203 | palettes.print_width = 5,
204 | palettes.print_index = TRUE
205 | )
206 | colour_vector
207 | ```
208 |
209 | ```{r, include=FALSE}
210 | options(op)
211 | ```
212 |
213 | Set any of these options in your [`.Rprofile`](https://rstats.wtf/r-startup.html#rprofile) dotfile to have them persist across R sessions on a global or per-project basis.
214 |
--------------------------------------------------------------------------------