-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#' Colours for data values | ||
#' | ||
#' Scales input data to the palette, so that colour is mapped linearly to the range of values. | ||
#' | ||
#' Default palette 'pal' is the 'viridis' colours of [grDevices::hcl.colors()], and may be literal colour values | ||
#' or a function. | ||
#' | ||
#' [data_pal()] is an alias of [d_pal()]. | ||
#' | ||
#' @param x data vector, maybe be numeric or character | ||
#' @param pal palette, may be colours or a function | ||
#' | ||
#' @export | ||
#' @importFrom grDevices hcl.colors | ||
#' @examples | ||
#' plot(1:100, col = d_pal(1:100)) | ||
#' plot(1:100, col = d_pal(1:100, chl_pal)) | ||
#' | ||
d_pal <- function(x, pal = hcl.colors(84)) { | ||
if (is.function(pal)) { | ||
pal <- pal(84) | ||
} | ||
if (is.character(x)) { | ||
x <- as.integer(factor(x)) | ||
} | ||
if (is.factor(x)) { | ||
x <- as.integer(x) | ||
} | ||
x <- x[!is.na(x)] | ||
xx <- (x - min(x)) / diff(range(x)) | ||
pal[xx * (length(pal) - 1) + 1] | ||
} | ||
|
||
#' @name d_pal | ||
#' @export | ||
data_pal <- function(x, pal = hcl.colors(84)) { | ||
d_pal(x, pal = pal) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.