Skip to content

Commit

Permalink
Viridis color palette from ggplot2 v. 3.0.0 cloned here
Browse files Browse the repository at this point in the history
  • Loading branch information
phgrosjean committed Sep 8, 2018
1 parent 44ef1f3 commit 26696d4
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 8 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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.
Authors@R: c(person("Philippe", "Grosjean", role = c("aut", "cre"),
email = "phgrosjean@sciviews.org"))
Maintainer: Philippe Grosjean <phgrosjean@sciviews.org>
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
Expand Down
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
8 changes: 7 additions & 1 deletion R/chart-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!
Expand Down
126 changes: 126 additions & 0 deletions R/chart_theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions man/chart_theme.Rd

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

2 changes: 1 addition & 1 deletion man/combine_charts.Rd

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

2 changes: 1 addition & 1 deletion man/f_aes.Rd

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

120 changes: 120 additions & 0 deletions man/viridis.Rd

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

0 comments on commit 26696d4

Please sign in to comment.