-
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
1 parent
fe459f8
commit 18b51c0
Showing
24 changed files
with
536 additions
and
27 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,88 @@ | ||
#' Output a character vector containing code for a palette | ||
#' | ||
#' Call this function to get the code for a character vector containing a | ||
#' palette. If using RStudio, the code will be loaded at the console prompt; | ||
#' otherwise, it will be printed at the terminal. | ||
#' | ||
#' @inheritParams wa_pal | ||
#' | ||
#' @examples | ||
#' pal_vector("rainier", 4) | ||
#' | ||
#' @export | ||
pal_vector = function(palette, n, which=NULL, | ||
type=c("discrete", "continuous"), reverse=FALSE) { | ||
type = match.arg(type) | ||
pal = wa_pal(palette, n, which, type, reverse) | ||
varname = paste0("PAL_", toupper(attr(pal, "name"))) | ||
code = paste0(varname, " = c(", paste0('"', pal, '"', collapse=", "), ")") | ||
code = paste0(strwrap(code, 76, indent=0, exdent=nchar(varname) + 5), | ||
collapse="\n") | ||
code_output(code) | ||
} | ||
|
||
#' Output a character vector containing code for a `ggplot2` scale | ||
#' | ||
#' Call this function to get the code for the `scale_*` functions for a palette. | ||
#' If using RStudio, the code will be loaded at the console prompt; | ||
#' otherwise, it will be printed at the terminal. Assumes that `ggplot2` has | ||
#' been loaded into the namespace, or will be by the time the scales are used. | ||
#' | ||
#' @param palette a `[wacolors]` palette or palette name. | ||
#' @param which if not `NULL`, the indices or names of a subset of colors to use. | ||
#' @param ... Other arguments passed on to [ggplot2::discrete_scale()], | ||
#' [ggplot2::continuous_scale()], or [ggplot2::binned_scale()] to control | ||
#' name, limits, breaks, labels and so forth. | ||
#' @param reverse `TRUE` if the colors should be reversed. | ||
#' | ||
#' @examples | ||
#' pal_functions("rainier") | ||
#' | ||
#' @export | ||
pal_functions = function(palette, which=NULL, type=c("discrete", "continuous"), | ||
reverse=FALSE) { | ||
pal = match_pal(palette) | ||
if (!is.null(which)) pal$pal = pal$pal[which] | ||
if (reverse) pal$pal = rev(pal$pal) | ||
names(pal$pal) = NULL | ||
|
||
make_discr = function(aesthetic) { | ||
pal_col_code = paste0(" pal_cols = c(", paste0('"', pal$pal, '"', collapse=", "), ")") | ||
pal_col_code = paste0(strwrap(pal_col_code, 76, indent=2, exdent=15), collapse="\n") | ||
pal_col_code = paste0(pal_col_code, "\n", " n_col = length(pal_cols)\n") | ||
pal_fun_code = " ramp = grDevices::colorRampPalette(pal_cols)\n" | ||
if (!(pal$name %in% cont_pal)) { | ||
pal_fun_code = paste0(pal_fun_code, " pal_fun = function(n) ", | ||
"if (n <= n_col) pal_cols[1:n] else ramp(n)\n") | ||
} else { | ||
pal_fun_code = paste0(pal_fun_code, " pal_fun = ramp\n") | ||
} | ||
pal_gen_code = paste0(' discrete_scale("', aesthetic, '", "', | ||
pal$name, '", palette=pal_fun, ...)\n') | ||
|
||
fname = paste0("scale_", aesthetic, "_", pal$name, "_d") | ||
paste0(fname, " = function(...) {\n", pal_col_code, | ||
pal_fun_code, pal_gen_code, "}\n") | ||
} | ||
|
||
make_cont = function(aesthetic) { | ||
pal_cols = paste0("c(", paste0('"', pal$pal, '"', collapse=", "), ")") | ||
pal_gen_code = paste0("scale_", aesthetic, "_gradientn(..., colours=", | ||
pal_cols, ")") | ||
pal_gen_code = paste0(strwrap(pal_gen_code, 76, indent=2, | ||
exdent=23 + (aesthetic=="color")), collapse="\n") | ||
fname = paste0("scale_", aesthetic, "_", pal$name, "_c") | ||
paste0(fname, " = function(...) {\n", pal_gen_code, "\n}\n") | ||
} | ||
|
||
code = "" | ||
if ("discrete" %in% type) { | ||
code = paste0(code, make_discr("color"), make_discr("fill"), "\n") | ||
} | ||
if ("continuous" %in% type) { | ||
code = paste0(code, make_cont("color"), make_cont("fill"), "\n") | ||
} | ||
|
||
code_output(code) | ||
} | ||
|
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,117 @@ | ||
#' Helper function-palette for discrete scales | ||
#' @importFrom grDevices colorRampPalette rgb | ||
discr_pal = function(pal, reverse=FALSE) { | ||
n_col = length(pal) | ||
names(pal) = NULL | ||
ramp = colorRampPalette(pal) | ||
function(n) { | ||
if (n <= n_col) | ||
pal[1:n] | ||
else | ||
ramp(n) | ||
} | ||
} | ||
|
||
#' Color palettes for `ggplot2` | ||
#' | ||
#' @rdname scale_wa | ||
#' | ||
#' @param palette a `[wacolors]` palette or palette name. | ||
#' @param which if not `NULL`, the indices or names of a subset of colors to use. | ||
#' @param ... Other arguments passed on to [ggplot2::discrete_scale()], | ||
#' [ggplot2::continuous_scale()], or [ggplot2::binned_scale()] to control | ||
#' name, limits, breaks, labels and so forth. | ||
#' @param reverse `TRUE` if the colors should be reversed. | ||
#' | ||
#' @examples | ||
#' ggplot(mtcars, aes(mpg, wt)) + | ||
#' geom_point(aes(color = factor(cyl), size=hp)) + | ||
#' scale_color_wa_d() | ||
#' | ||
#' ggplot(mtcars, aes(mpg, wt)) + | ||
#' geom_point(aes(color = hp)) + | ||
#' scale_color_wa_c("seattle_night", which=c("seafirst", "pike_clock")) | ||
#' | ||
#' ggplot(diamonds) + | ||
#' geom_bar(aes(x = cut, fill = clarity)) + | ||
#' scale_fill_wa_d(wacolors$sound_sunset, reverse=TRUE) | ||
#' | ||
#' @importFrom ggplot2 discrete_scale scale_color_gradientn | ||
#' @export | ||
scale_color_wa_d = function(palette="rainier", which=NULL, ..., reverse=FALSE) { | ||
pal = match_pal(palette) | ||
if (!is.null(which)) pal$pal = pal$pal[which] | ||
if (reverse) pal$pal = rev(pal$pal) | ||
|
||
if (pal$name %in% cont_pal) | ||
pal_fun = colorRampPalette(pal$pal) | ||
else | ||
pal_fun = discr_pal(pal$pal) | ||
|
||
discrete_scale("colour", pal$name, palette=pal_fun, ...) | ||
} | ||
|
||
#' @rdname scale_wa | ||
#' @export | ||
scale_fill_wa_d = function(palette="rainier", which=NULL, ..., reverse=FALSE) { | ||
pal = match_pal(palette) | ||
if (!is.null(which)) pal$pal = pal$pal[which] | ||
if (reverse) pal$pal = rev(pal$pal) | ||
|
||
if (pal$name %in% cont_pal) | ||
pal_fun = colorRampPalette(pal$pal) | ||
else | ||
pal_fun = discr_pal(pal$pal) | ||
|
||
discrete_scale("fill", pal$name, palette=pal_fun, ...) | ||
} | ||
|
||
#' @rdname scale_wa | ||
#' @export | ||
scale_color_wa_c = function(palette="ferries", which=NULL, ..., reverse=FALSE) { | ||
pal = match_pal(palette) | ||
if (!is.null(which)) pal$pal = pal$pal[which] | ||
if (reverse) pal$pal = rev(pal$pal) | ||
|
||
scale_color_gradientn(..., colours=pal$pal) | ||
} | ||
|
||
#' @rdname scale_wa | ||
#' @export | ||
scale_fill_wa_c = function(palette="ferries", which=NULL, ..., reverse=FALSE) { | ||
pal = match_pal(palette) | ||
if (!is.null(which)) pal$pal = pal$pal[which] | ||
if (reverse) pal$pal = rev(pal$pal) | ||
|
||
scale_fill_gradientn(..., colours=pal$pal) | ||
} | ||
|
||
#' @rdname scale_wa | ||
#' @export | ||
scale_colour_wa_d = scale_color_wa_d | ||
#' @rdname scale_wa | ||
#' @export | ||
scale_colour_wa_c = scale_color_wa_c | ||
#' @rdname scale_wa | ||
#' @export | ||
scale_color_wa_b = scale_color_wa_d | ||
#' @rdname scale_wa | ||
#' @export | ||
scale_colour_wa_b = scale_color_wa_d | ||
#' @rdname scale_wa | ||
#' @export | ||
scale_fill_wa_b = scale_fill_wa_d | ||
|
||
if (F) { | ||
ggplot(mtcars, aes(mpg, wt)) + | ||
geom_point(aes(color = factor(cyl), size=hp)) + | ||
scale_color_wa_d() | ||
|
||
ggplot(mtcars, aes(mpg, wt)) + | ||
geom_point(aes(colour = hp)) + | ||
scale_color_wa_c("seattle_night", which=c("seafirst", "pike_clock")) | ||
|
||
ggplot(diamonds) + | ||
geom_bar(aes(x = cut, fill = clarity)) + | ||
scale_fill_wa_d(wacolors$sound_sunset, reverse=TRUE) | ||
} |
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
Oops, something went wrong.