From 26696d4995f7783c9c2a3523808c995b5be5761e Mon Sep 17 00:00:00 2001 From: phgrosjean Date: Sat, 8 Sep 2018 17:09:44 +0200 Subject: [PATCH] Viridis color palette from ggplot2 v. 3.0.0 cloned here --- DESCRIPTION | 6 +- NAMESPACE | 11 ++++ NEWS.md | 6 ++ R/chart-package.R | 8 ++- R/chart_theme.R | 126 ++++++++++++++++++++++++++++++++++++++++++ man/chart_theme.Rd | 4 +- man/combine_charts.Rd | 2 +- man/f_aes.Rd | 2 +- man/viridis.Rd | 120 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 277 insertions(+), 8 deletions(-) create mode 100644 man/viridis.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 029b7a2..3851fb4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: chart Type: Package -Version: 1.1.0 +Version: 1.2.0 Title: General Charting (Plotting) Function Description: Chart generalizes plot generation in R, being with base R plot function, lattice or ggplot2. A formula interface is available for both. @@ -8,8 +8,8 @@ Authors@R: c(person("Philippe", "Grosjean", role = c("aut", "cre"), email = "phgrosjean@sciviews.org")) Maintainer: Philippe Grosjean Depends: R (>= 3.3.0), lattice, ggplot2 -Imports: stats, utils, rlang, cowplot, grDevices, graphics, scales, latticeExtra, pryr, data.io, ggplotify, ggpubr -Suggests: covr, knitr, testthat +Imports: stats, utils, rlang, cowplot, grDevices, graphics, scales, latticeExtra, viridis, pryr, data.io, ggplotify, ggpubr +Suggests: MASS, covr, knitr, testthat Remotes: SciViews/data.io Encoding: UTF-8 ByteCompile: yes diff --git a/NAMESPACE b/NAMESPACE index c4c54ca..2fd3882 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,15 @@ export(chart_theme) export(combine_charts) export(f_aes) export(ggarrange) +export(scale_color_continuous) +export(scale_color_viridis_c) +export(scale_color_viridis_d) +export(scale_colour_continuous) +export(scale_colour_viridis_c) +export(scale_colour_viridis_d) +export(scale_fill_continuous) +export(scale_fill_viridis_c) +export(scale_fill_viridis_d) export(theme_sciviews) export(theme_sciviews_graphics) export(theme_sciviews_lattice) @@ -35,7 +44,9 @@ importFrom(rlang,f_lhs) importFrom(rlang,f_rhs) importFrom(rlang,is_true) importFrom(rlang,warn) +importFrom(scales,gradient_n_pal) importFrom(scales,hue_pal) importFrom(stats,as.formula) importFrom(stats,asOneSidedFormula) importFrom(utils,modifyList) +importFrom(viridis,viridis_pal) diff --git a/NEWS.md b/NEWS.md index 1bd3d09..a32563d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,11 @@ # chart News +## chart version 1.2.0 + +- The functions to use viridis palettes in ggplot2 are cloned here from ggplot2 + v. 3.0.0, in order to use them also with older ggplot2 plots. + + ## chart version 1.1.0 - First implementation of base and lattice versions of `chart()`s. diff --git a/R/chart-package.R b/R/chart-package.R index a96e4ec..304beb1 100644 --- a/R/chart-package.R +++ b/R/chart-package.R @@ -17,7 +17,8 @@ #' @import lattice #' @importFrom latticeExtra custom.theme ggplot2like ggplot2like.opts #' @import ggplot2 -#' @importFrom scales hue_pal +#' @importFrom viridis viridis_pal +#' @importFrom scales hue_pal gradient_n_pal #' @importFrom cowplot theme_cowplot #' @importFrom rlang abort warn f_env f_lhs f_rhs is_true #' @importFrom stats as.formula asOneSidedFormula @@ -36,6 +37,11 @@ NULL #ggplot2::theme_set(theme_sciviews()) #theme_sciviews_lattice() #theme_sciviews_graphics() + # Use viridis continuous palettes by default with ggplot2 charts + if (is.null(getOption("ggplot2.continuous.fill"))) + options(ggplot2.continuous.fill = "viridis") + if (is.null(getOption("ggplot2.continuous.colour"))) + options(ggplot2.continuous.colour = "viridis") } `%is%` <- function(x, what) # This is more expressive! diff --git a/R/chart_theme.R b/R/chart_theme.R index b5518ca..06baff8 100644 --- a/R/chart_theme.R +++ b/R/chart_theme.R @@ -159,6 +159,132 @@ theme_svmap_lattice <- function() { #' @rdname chart_theme theme_svmap_graphics <- function() NULL +#' Viridis color scales +#' +#' ggplot2 version 3 add these function to include viridis color scales. These +#' functions are cloned from ggplot2 v.3.0.0 in order to use the same color +#' scheme with earlier versions of ggplot2. Note, however, that the options +#' `ggplot2.continuous.fill` and `gplot2.continuous.colour` are set to +#' `"viridis"` if not already specified. This changes the default for continuous +#' colors in favor of viridis for both `ggplot()` and `chart()`. +#' +#' @param ... Other arguments passed on to [discrete_scale()] or +#' [continuous_scale()] to control name, limits, breaks, labels and so forth. +#' @param alpha The alpha transparency, a number in `[0,1]`, see argument alpha +#' in hsv. +#' @param begin The (corrected) hue in `[0,1]` at which the viridis colormap +#' begins. +#' @param end The (corrected) hue in `[0,1]` at which the viridis colormap ends. +#' @param direction Sets the order of colors in the scale. If `1`, the default, +#' colors are ordered from darkest to lightest. If `-1`, the order of colors is +#' reversed. +#' @param option A character string indicating the colormap option to use. Four +#' options are available: `"magma"` (or `"A"`), `"inferno"` (or `"B"`), +#' `"plasma"` (or `"C"`), `"viridis"` (or `"D"`, the default option) and +#' `"cividis"` (or `"E"`). +#' @param aesthetics Character string or vector of character strings listing the +#' name(s) of the aesthetic(s) that this scale works with. This can be useful, +#' for example, to apply colour settings to the colour and fill aesthetics at +#' the same time, via `aesthetics = c("colour", "fill")`. +#' @param values If colours should not be evenly positioned along the gradient +#' this vector gives the position (between 0 and 1) for each colour in the +#' colours vector. See rescale() in the scales package for a convience function +#' to map an arbitrary range to between 0 and 1. +#' @param space Colour space in which to calculate gradient. Must be "Lab" - +#' other values are deprecated. +#' @param na.value Missing values will be replaced with this value. +#' @param guide A function used to create a guide or its name. See [guides()] +#' for more info. +#' @param type One of "gradient" or "viridis" indicating the colour scale to +#' use. Can be changed with [options()], and the chart package set it to +#' `"viridis"` in case it is not set yet when the package is attached. +#' +#' @export +#' @seealso [chart()], [chart_theme()] +#' @keywords color +#' @name viridis +#' @concept Viridis colors +#' @examples +#' geyser <- data.io::read("geyser", package = "MASS") +#' chart(data = geyser, waiting ~ duration) + +#' xlim(0.5, 6) + ylim(40, 110) + +#' stat_density2d(aes(fill = ..level..), geom = "polygon") + +#' theme(panel.grid = element_blank()) -> gg +#' +#' combine_charts( +#' list( +#' gg + scale_fill_viridis_c(option = "A") + labs(x = "A = magma", y = NULL), +#' gg + scale_fill_viridis_c(option = "B") + labs(x = "B = inferno", y = NULL), +#' gg + scale_fill_viridis_c(option = "C") + labs(x = "C = plasma", y = NULL), +#' gg + scale_fill_viridis_c(option = "D") + labs(x = "D = viridis", y = NULL), +#' gg + scale_fill_viridis_c(option = "E") + labs(x = "E = cividis", y = NULL) +#' ), ncol = 3, nrow = 2 +#' ) +scale_colour_viridis_c <- function(..., alpha = 1, begin = 0, end = 1, +direction = 1, option = "D", values = NULL, space = "Lab", na.value = "grey50", +guide = "colourbar", aesthetics = "colour") { + continuous_scale(aesthetics, "viridis_c", scales::gradient_n_pal( + viridis::viridis_pal(alpha, begin, end, direction, option)(6), + values, space), na.value = na.value, guide = guide, ...) +} + +#' @export +#' @rdname viridis +scale_color_viridis_c <- scale_colour_viridis_c + +#' @export +#' @rdname viridis +scale_colour_viridis_d <- function(..., alpha = 1, begin = 0, end = 1, +direction = 1, option = "D", aesthetics = "colour") { + discrete_scale(aesthetics, "viridis_d", viridis_pal(alpha, + begin, end, direction, option), ...) +} + +#' @export +#' @rdname viridis +scale_color_viridis_d <- scale_colour_viridis_d + +#' @export +#' @rdname viridis +scale_fill_viridis_c <- function(..., alpha = 1, begin = 0, end = 1, +direction = 1, option = "D", values = NULL, space = "Lab", na.value = "grey50", +guide = "colourbar", aesthetics = "fill") { + continuous_scale(aesthetics, "viridis_c", gradient_n_pal( + viridis_pal(alpha, begin, end, direction, option)(6), + values, space), na.value = na.value, guide = guide, ...) +} + +#' @export +#' @rdname viridis +scale_fill_viridis_d <- function(..., alpha = 1, begin = 0, end = 1, +direction = 1, option = "D", aesthetics = "fill") { + discrete_scale(aesthetics, "viridis_d", viridis_pal(alpha, + begin, end, direction, option), ...) +} + +#' @export +#' @rdname viridis +scale_colour_continuous <- function(..., +type = getOption("ggplot2.continuous.colour", default = "gradient")) { + switch(type, + gradient = scale_colour_gradient(...), + viridis = scale_colour_viridis_c(...), + stop("Unknown scale type", call. = FALSE)) +} + +#' @export +#' @rdname viridis +scale_color_continuous <- scale_colour_continuous + +#' @export +#' @rdname viridis +scale_fill_continuous <- function(..., +type = getOption("ggplot2.continuous.fill", default = "gradient")) { + switch(type, + gradient = scale_fill_gradient(...), + viridis = scale_fill_viridis_c(...), + stop("Unknown scale type", call. = FALSE)) +} .sciviewslike_opts <- function() { list(default.args = list(axis = .axis_grid, diff --git a/man/chart_theme.Rd b/man/chart_theme.Rd index 740568c..83a2916 100644 --- a/man/chart_theme.Rd +++ b/man/chart_theme.Rd @@ -43,12 +43,12 @@ theme_svmap_graphics() \item{line_size}{The default line size in this theme.} \item{...}{Arguments passed to \code{\link[=ggplot2like]{ggplot2like()}}, the most used being \code{n=} for -the number of colours to generate in the palette.} +the number of colors to generate in the palette.} } \description{ Lattice and ggplot2 provide themes for their plots, while with base R plots, one specifies appearance through \code{\link[=par]{par()}}. \code{chart_theme()} tries to get the -plot appearance as uniform as possible beteen the three plot engines. So, +plot appearance as uniform as possible between the three plot engines. So, setting themes this way change the appearance of all three kinds of plots. } \examples{ diff --git a/man/combine_charts.Rd b/man/combine_charts.Rd index 0fe7d93..32ca455 100644 --- a/man/combine_charts.Rd +++ b/man/combine_charts.Rd @@ -23,7 +23,7 @@ for lowercase labels.} An object of class \code{ggarrange} containing a list of \code{ggplot}s. } \description{ -Assemble multiple charts on the same page. Wrapper arround \code{\link[=ggarrange]{ggarrange()}} with +Assemble multiple charts on the same page. Wrapper around \code{\link[=ggarrange]{ggarrange()}} with different defaults. } \examples{ diff --git a/man/f_aes.Rd b/man/f_aes.Rd index 8e85af3..7098bc0 100644 --- a/man/f_aes.Rd +++ b/man/f_aes.Rd @@ -9,7 +9,7 @@ f_aes(formula, ..., with.facets = FALSE) \arguments{ \item{formula}{A formula.} -\item{...}{Further aesthetics to set (like \code{size}, \code{colour}, et.)} +\item{...}{Further aesthetics to set (like \code{size}, \code{colour}, etc.)} \item{with.facets}{Do we create special (non-ggplot2) aesthetics for facets (no by default)?} diff --git a/man/viridis.Rd b/man/viridis.Rd new file mode 100644 index 0000000..a833368 --- /dev/null +++ b/man/viridis.Rd @@ -0,0 +1,120 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/chart_theme.R +\name{viridis} +\alias{viridis} +\alias{scale_colour_viridis_c} +\alias{scale_color_viridis_c} +\alias{scale_colour_viridis_d} +\alias{scale_color_viridis_d} +\alias{scale_fill_viridis_c} +\alias{scale_fill_viridis_d} +\alias{scale_colour_continuous} +\alias{scale_color_continuous} +\alias{scale_fill_continuous} +\title{Viridis color scales} +\usage{ +scale_colour_viridis_c(..., alpha = 1, begin = 0, end = 1, + direction = 1, option = "D", values = NULL, space = "Lab", + na.value = "grey50", guide = "colourbar", aesthetics = "colour") + +scale_color_viridis_c(..., alpha = 1, begin = 0, end = 1, direction = 1, + option = "D", values = NULL, space = "Lab", na.value = "grey50", + guide = "colourbar", aesthetics = "colour") + +scale_colour_viridis_d(..., alpha = 1, begin = 0, end = 1, + direction = 1, option = "D", aesthetics = "colour") + +scale_color_viridis_d(..., alpha = 1, begin = 0, end = 1, direction = 1, + option = "D", aesthetics = "colour") + +scale_fill_viridis_c(..., alpha = 1, begin = 0, end = 1, direction = 1, + option = "D", values = NULL, space = "Lab", na.value = "grey50", + guide = "colourbar", aesthetics = "fill") + +scale_fill_viridis_d(..., alpha = 1, begin = 0, end = 1, direction = 1, + option = "D", aesthetics = "fill") + +scale_colour_continuous(..., type = getOption("ggplot2.continuous.colour", + default = "gradient")) + +scale_color_continuous(..., type = getOption("ggplot2.continuous.colour", + default = "gradient")) + +scale_fill_continuous(..., type = getOption("ggplot2.continuous.fill", default + = "gradient")) +} +\arguments{ +\item{...}{Other arguments passed on to \code{\link[=discrete_scale]{discrete_scale()}} or +\code{\link[=continuous_scale]{continuous_scale()}} to control name, limits, breaks, labels and so forth.} + +\item{alpha}{The alpha transparency, a number in \code{[0,1]}, see argument alpha +in hsv.} + +\item{begin}{The (corrected) hue in \code{[0,1]} at which the viridis colormap +begins.} + +\item{end}{The (corrected) hue in \code{[0,1]} at which the viridis colormap ends.} + +\item{direction}{Sets the order of colors in the scale. If \code{1}, the default, +colors are ordered from darkest to lightest. If \code{-1}, the order of colors is +reversed.} + +\item{option}{A character string indicating the colormap option to use. Four +options are available: \code{"magma"} (or \code{"A"}), \code{"inferno"} (or \code{"B"}), +\code{"plasma"} (or \code{"C"}), \code{"viridis"} (or \code{"D"}, the default option) and +\code{"cividis"} (or \code{"E"}).} + +\item{values}{If colours should not be evenly positioned along the gradient +this vector gives the position (between 0 and 1) for each colour in the +colours vector. See rescale() in the scales package for a convience function +to map an arbitrary range to between 0 and 1.} + +\item{space}{Colour space in which to calculate gradient. Must be "Lab" - +other values are deprecated.} + +\item{na.value}{Missing values will be replaced with this value.} + +\item{guide}{A function used to create a guide or its name. See \code{\link[=guides]{guides()}} +for more info.} + +\item{aesthetics}{Character string or vector of character strings listing the +name(s) of the aesthetic(s) that this scale works with. This can be useful, +for example, to apply colour settings to the colour and fill aesthetics at +the same time, via \code{aesthetics = c("colour", "fill")}.} + +\item{type}{One of "gradient" or "viridis" indicating the colour scale to +use. Can be changed with \code{\link[=options]{options()}}, and the chart package set it to +\code{"viridis"} in case it is not set yet when the package is attached.} +} +\description{ +ggplot2 version 3 add these function to include viridis color scales. These +functions are cloned from ggplot2 v.3.0.0 in order to use the same color +scheme with earlier versions of ggplot2. Note, however, that the options +\code{ggplot2.continuous.fill} and \code{gplot2.continuous.colour} are set to +\code{"viridis"} if not already specified. This changes the default for continuous +colors in favor of viridis for both \code{ggplot()} and \code{chart()}. +} +\examples{ +geyser <- data.io::read("geyser", package = "MASS") +chart(data = geyser, waiting ~ duration) + + xlim(0.5, 6) + ylim(40, 110) + + stat_density2d(aes(fill = ..level..), geom = "polygon") + + theme(panel.grid = element_blank()) -> gg + +combine_charts( + list( + gg + scale_fill_viridis_c(option = "A") + labs(x = "A = magma", y = NULL), + gg + scale_fill_viridis_c(option = "B") + labs(x = "B = inferno", y = NULL), + gg + scale_fill_viridis_c(option = "C") + labs(x = "C = plasma", y = NULL), + gg + scale_fill_viridis_c(option = "D") + labs(x = "D = viridis", y = NULL), + gg + scale_fill_viridis_c(option = "E") + labs(x = "E = cividis", y = NULL) + ), ncol = 3, nrow = 2 +) +} +\seealso{ +\code{\link[=chart]{chart()}}, \code{\link[=chart_theme]{chart_theme()}} +} +\concept{ +Viridis colors +} +\keyword{color}