Skip to content

Commit

Permalink
chart made compatible with ggplot2 v. 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
phgrosjean committed Apr 24, 2019
1 parent 2e929d1 commit 37e9871
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 132 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Package: chart
Type: Package
Version: 1.2.0
Version: 1.3.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
Depends: R (>= 3.3.0), lattice, ggplot2 (>= 3.0)
Imports: stats, utils, rlang, cowplot, grDevices, graphics, scales, latticeExtra, viridis, pryr, data.io, ggplotify, ggpubr
Suggests: MASS, covr, knitr, testthat
Remotes: SciViews/data.io
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# chart News

## chart version 1.2.0

- Two bugs in chart.default() that appeared with ggplot 3.0 are solved.

- ggplot2 3.0 viridis palettes are not needed any more and are thus eliminated.


## chart version 1.2.0

- The functions to use viridis palettes in ggplot2 are cloned here from ggplot2
Expand Down
12 changes: 9 additions & 3 deletions R/chart.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ chart <- structure(function(data, ..., type = NULL, env = parent.frame()) {
#' @method chart default
chart.default <- function(data, specif = NULL, formula = NULL, mapping = NULL,
..., type = NULL, auto.labs = TRUE, env = parent.frame()) {
force(env)
# TODO: add lattice, autoplot and plot methods too!
force(env)
if (!is.null(type) && type != "auto") {
if (type == "base") { # Try using the expression in 'data' for making a plot
if (is.function(data)) {
Expand Down Expand Up @@ -142,6 +142,8 @@ chart.default <- function(data, specif = NULL, formula = NULL, mapping = NULL,
args$specif <- NULL
args$formula <- NULL
args$mapping <- NULL
args$type <- NULL
args$auto.labs <- NULL
args$env <- NULL
mapping <- .rename_aes(.f_to_aes(formula, args, with.facets = TRUE))
# If mapping is provided, use it to append (or replace) formula items
Expand Down Expand Up @@ -215,15 +217,19 @@ chart.default <- function(data, specif = NULL, formula = NULL, mapping = NULL,
mnames <- names(maps)
for (mname in mnames) {
expr <- maps[[mname]]
form <- asOneSidedFormula(as.character(expr))
if (is_quosure(expr)) {
form <- unclass(expr)
} else {
form <- asOneSidedFormula(as.character(expr))
}
vars <- all.vars(form)
vars <- vars[vars %in% dnames]
for (var in vars) {
expr <- pryr::modify_lang(expr, function(x)
if (is.name(x) && identical(x, as.name(var)))
as.name(labels[[var]]) else x)
}
p$labels[[mname]] <- gsub("`", "", deparse(expr))
p$labels[[mname]] <- sub("^~", "", gsub("`", "", deparse(expr)))
}
}
}
Expand Down
127 changes: 0 additions & 127 deletions R/chart_theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,133 +159,6 @@ 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,
xscale.components = .xscale_components_subticks,
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ environment:
global:
R_VERSION: "stable"
R_CHECK_ARGS: "--no-multiarch --no-manual"
USE_RTOOLS: true

# DO NOT CHANGE the "init" and "install" sections below
# Download script file from GitHub
Expand Down

0 comments on commit 37e9871

Please sign in to comment.