Skip to content

Commit

Permalink
Fix discreterainbow scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Dec 19, 2023
1 parent 6bb7d13 commit ab55459
Show file tree
Hide file tree
Showing 45 changed files with 184 additions and 174 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
## New functions
* Add Paul Tol `nightfall` and `incandescent` colour schemes.

## Bugfixes & changes
* Fix `discreterainbow` scheme (qualitative scheme instead of sequential).

# khroma 1.11.0
## Bugfixes & changes
* Fix `ramp()` to return a palette function with argument a vector of values that are mapped to colors.
Expand Down
14 changes: 8 additions & 6 deletions R/color.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,17 @@ colour <- function(palette, reverse = FALSE, names = TRUE, lang = "en",
fun <- function(n, range = c(0, 1)) {
if (missing(n)) n <- k
# Validate
if (n > k)
stop(
sprintf("%s color scheme supports up to %d values.",
sQuote(palette), k),
call. = FALSE
)
if (n > k) {
msg <- "%s color scheme supports up to %d values."
stop(sprintf(msg, sQuote(palette), k), call. = FALSE)
}
# Arrange color schemes
if (!is.null(col_scheme)) {
m <- col_scheme[[n]]
if (reverse) {
m <- rev(m)
col_colors <- rev(col_colors)
}
col <- col_colors[m]
} else if (col_type == "qualitative") {
m <- seq_len(n)
Expand Down
110 changes: 55 additions & 55 deletions R/scale_colour_tol.R
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,61 @@ scale_edge_fill_light <- function(..., reverse = FALSE,
scale_discrete(aesthetics, "light", reverse = reverse, ...)
}

## Discrete Rainbow ------------------------------------------------------------
#' Paul Tol's *discrete rainbow* Sequential Color Scheme
#'
#' @inheritParams scale_tol_sequential
#' @inheritSection scale_tol_sequential Interpolation
#' @inheritSection colour Rainbow Color Scheme
#' @return A [discrete][ggplot2::discrete_scale] scale.
#' @references
#' Tol, P. (2018). *Colour Schemes*. SRON. Technical Note No.
#' SRON/EPS/TN/09-002, issue 3.1.
#' URL: \url{https://personal.sron.nl/~pault/data/colourschemes.pdf}
#' @example inst/examples/ex-tol-discrete.R
#' @author N. Frerebeau
#' @family qualitative color schemes
#' @family Paul Tol's color schemes
#' @name scale_tol_discreterainbow
#' @rdname scale_tol_discreterainbow
NULL

#' @export
#' @rdname scale_tol_discreterainbow
scale_colour_discreterainbow <- function(..., reverse = FALSE,
aesthetics = "colour") {
scale_discrete(aesthetics, "discreterainbow", reverse = reverse, ...)
}

#' @export
#' @rdname scale_tol_discreterainbow
scale_color_discreterainbow <- scale_colour_discreterainbow

#' @export
#' @rdname scale_tol_discreterainbow
scale_fill_discreterainbow <- function(..., reverse = FALSE,
aesthetics = "fill") {
scale_discrete(aesthetics, "discreterainbow", reverse = reverse, ...)
}

#' @export
#' @rdname scale_tol_discreterainbow
scale_edge_colour_discreterainbow <- function(..., reverse = FALSE,
aesthetics = "edge_colour") {
scale_discrete(aesthetics, "discreterainbow", reverse = reverse, ...)
}

#' @export
#' @rdname scale_tol_discreterainbow
scale_edge_color_discreterainbow <- scale_edge_colour_discreterainbow

#' @export
#' @rdname scale_tol_discreterainbow
scale_edge_fill_discreterainbow <- function(..., reverse = FALSE,
aesthetics = "edge_fill") {
scale_discrete(aesthetics, "discreterainbow", reverse = reverse, ...)
}

# Diverging ====================================================================
#' Paul Tol's Diverging Color Schemes for \pkg{ggplot2} and \pkg{ggraph}
#'
Expand Down Expand Up @@ -1094,61 +1149,6 @@ scale_edge_fill_incandescent <- function(..., reverse = FALSE, range = c(0, 1),
}
}

## Discrete Rainbow ------------------------------------------------------------
#' Paul Tol's *discrete rainbow* Sequential Color Scheme
#'
#' @inheritParams scale_tol_sequential
#' @inheritSection scale_tol_sequential Interpolation
#' @inheritSection colour Rainbow Color Scheme
#' @return A [continuous][ggplot2::continuous_scale] scale.
#' @references
#' Tol, P. (2018). *Colour Schemes*. SRON. Technical Note No.
#' SRON/EPS/TN/09-002, issue 3.1.
#' URL: \url{https://personal.sron.nl/~pault/data/colourschemes.pdf}
#' @example inst/examples/ex-tol-sequential.R
#' @author N. Frerebeau
#' @family sequential color schemes
#' @family Paul Tol's color schemes
#' @name scale_tol_discreterainbow
#' @rdname scale_tol_discreterainbow
NULL

#' @export
#' @rdname scale_tol_discreterainbow
scale_colour_discreterainbow <- function(..., reverse = FALSE,
aesthetics = "colour") {
scale_discrete(aesthetics, "discreterainbow", reverse = reverse, ...)
}

#' @export
#' @rdname scale_tol_discreterainbow
scale_color_discreterainbow <- scale_colour_discreterainbow

#' @export
#' @rdname scale_tol_discreterainbow
scale_fill_discreterainbow <- function(..., reverse = FALSE,
aesthetics = "fill") {
scale_discrete(aesthetics, "discreterainbow", reverse = reverse, ...)
}

#' @export
#' @rdname scale_tol_discreterainbow
scale_edge_colour_discreterainbow <- function(..., reverse = FALSE,
aesthetics = "edge_colour") {
scale_discrete(aesthetics, "discreterainbow", reverse = reverse, ...)
}

#' @export
#' @rdname scale_tol_discreterainbow
scale_edge_color_discreterainbow <- scale_edge_colour_discreterainbow

#' @export
#' @rdname scale_tol_discreterainbow
scale_edge_fill_discreterainbow <- function(..., reverse = FALSE,
aesthetics = "edge_fill") {
scale_discrete(aesthetics, "discreterainbow", reverse = reverse, ...)
}

## Smooth Rainbow --------------------------------------------------------------
#' Paul Tol's *smooth rainbow* Sequential Color Scheme
#'
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
90 changes: 45 additions & 45 deletions data-raw/schemes_PaulTol.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,51 @@ schemes_tol2018 <- list(
),
scheme = NULL
),
discreterainbow = list(
type = "qualitative",
interpolate = FALSE,
missing = "#777777",
max = 23,
colours = c("#E8ECFB", "#D9CCE3", "#D1BBD7", "#CAACCB", "#BA8DB4",
"#AE76A3", "#AA6F9E", "#994F88", "#882E72", "#1965B0",
"#437DBF", "#5289C7", "#6195CF", "#7BAFDE", "#4EB265",
"#90C987", "#CAE0AB", "#F7F056", "#F7CB45", "#F6C141",
"#F4A736", "#F1932D", "#EE8026", "#E8601C", "#E65518",
"#DC050C", "#A5170E", "#72190E", "#42150A"),
names = list(
en = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29"),
fr = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29")
),
scheme = list(
c(10),
c(10, 26),
c(10, 18, 26),
c(10, 15, 18, 26),
c(10, 14, 15, 18, 26),
c(10, 14, 15, 17, 18, 26),
c(9, 10, 14, 15, 17, 18, 26),
c(9, 10, 14, 15, 17, 18, 23, 26),
c(9, 10, 14, 15, 17, 18, 23, 26, 28),
c(9, 10, 14, 15, 17, 18, 21, 24, 26, 28),
c(9, 10, 12, 14, 15, 17, 18, 21,24, 26, 28),
c(3, 6, 9, 10, 12, 14, 15, 17, 18, 21, 24, 26),
c(3, 6, 9, 10, 12, 14, 15, 16, 17, 18, 21, 24, 26),
c(3, 6, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26),
c(3, 6, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28),
c(3, 5, 7, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28),
c(3, 5, 7, 8, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28),
c(3, 5, 7, 8, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 27, 28),
c(2, 4, 5, 7, 8, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 27, 28),
c(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 27, 28),
c(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 23, 25, 26, 27, 28),
c(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 23, 25, 26, 27, 28, 29),
c(1, 2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 23, 25, 26, 27, 28, 29)
)
),
# Diverging colour types ---------------------------------------------------
# The colours can be used as given or linearly interpolated
sunset = list(
Expand Down Expand Up @@ -192,51 +237,6 @@ schemes_tol2018 <- list(
names = NULL,
scheme = NULL
),
discreterainbow = list(
type = "sequential",
interpolate = FALSE,
missing = "#777777",
max = 23,
colours = c("#E8ECFB", "#D9CCE3", "#D1BBD7", "#CAACCB", "#BA8DB4",
"#AE76A3", "#AA6F9E", "#994F88", "#882E72", "#1965B0",
"#437DBF", "#5289C7", "#6195CF", "#7BAFDE", "#4EB265",
"#90C987", "#CAE0AB", "#F7F056", "#F7CB45", "#F6C141",
"#F4A736", "#F1932D", "#EE8026", "#E8601C", "#E65518",
"#DC050C", "#A5170E", "#72190E", "#42150A"),
names = list(
en = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29"),
fr = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29")
),
scheme = list(
c(10),
c(10, 26),
c(10, 18, 26),
c(10, 15, 18, 26),
c(10, 14, 15, 18, 26),
c(10, 14, 15, 17, 18, 26),
c(9, 10, 14, 15, 17, 18, 26),
c(9, 10, 14, 15, 17, 18, 23, 26),
c(9, 10, 14, 15, 17, 18, 23, 26, 28),
c(9, 10, 14, 15, 17, 18, 21, 24, 26, 28),
c(9, 10, 12, 14, 15, 17, 18, 21,24, 26, 28),
c(3, 6, 9, 10, 12, 14, 15, 17, 18, 21, 24, 26),
c(3, 6, 9, 10, 12, 14, 15, 16, 17, 18, 21, 24, 26),
c(3, 6, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26),
c(3, 6, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28),
c(3, 5, 7, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28),
c(3, 5, 7, 8, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28),
c(3, 5, 7, 8, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 27, 28),
c(2, 4, 5, 7, 8, 9, 10, 12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 27, 28),
c(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 27, 28),
c(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 23, 25, 26, 27, 28),
c(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 23, 25, 26, 27, 28, 29),
c(1, 2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 23, 25, 26, 27, 28, 29)
)
),
smoothrainbow = list(
type = "sequential",
interpolate = TRUE,
Expand Down
4 changes: 3 additions & 1 deletion inst/examples/ex-tol-discrete.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library(ggplot2)
data(mpg, package = "ggplot2")

ggplot2::ggplot(mpg, ggplot2::aes(displ, hwy, colour = class)) +
ggplot2::geom_point() +
Expand All @@ -8,6 +8,8 @@ ggplot2::ggplot(mpg, ggplot2::aes(displ, hwy, colour = class)) +
ggplot2::geom_point() +
scale_colour_vibrant()

data(diamonds, package = "ggplot2")

ggplot2::ggplot(diamonds, ggplot2::aes(clarity, fill = cut)) +
ggplot2::geom_bar() +
scale_fill_muted()
Binary file modified inst/tinytest/_snaps/info.rds
Binary file not shown.
3 changes: 2 additions & 1 deletion inst/tinytest/test_colour.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ for (i in seq_len(nrow(pal_qual))) {
expect_error(color(i_pal)(500))
expect_equivalent(
unclass(color(i_pal, reverse = TRUE)(i_max)),
rev(color(i_pal, reverse = FALSE)(i_max))
rev(color(i_pal, reverse = FALSE)(i_max)),
info = i_pal
)
}

Expand Down
1 change: 0 additions & 1 deletion man/scale_crameri_acton.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_bamako.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_batlow.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_batlowK.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_batlowW.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_bilbao.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_buda.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_davos.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_devon.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_grayC.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_hawaii.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_imola.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_lajolla.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/scale_crameri_lapaz.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ab55459

Please sign in to comment.