diff --git a/R/backports.R b/R/backports.R index 271af9d579..c02aca855a 100644 --- a/R/backports.R +++ b/R/backports.R @@ -14,3 +14,8 @@ if (getRversion() < 3.3) { } else { backport_unit_methods <- function() {} } + +# isFALSE() is available on R (>=3.5) +if (getRversion() < 3.5) { + isFALSE <- function(x) is.logical(x) && length(x) == 1L && !is.na(x) && !x +} diff --git a/R/guide-colorbar.r b/R/guide-colorbar.r index e5a17976ca..d0b1592a9a 100644 --- a/R/guide-colorbar.r +++ b/R/guide-colorbar.r @@ -244,7 +244,7 @@ guide_geom.colorbar <- function(guide, layers, default_mapping) { guide_layers <- lapply(layers, function(layer) { matched <- matched_aes(layer, guide, default_mapping) - if (length(matched) && ((is.na(layer$show.legend) || layer$show.legend))) { + if (length(matched) > 0 && (isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend))) { layer } else { # This layer does not use this guide diff --git a/R/guide-legend.r b/R/guide-legend.r index f965db98f8..e0e1802856 100644 --- a/R/guide-legend.r +++ b/R/guide-legend.r @@ -255,7 +255,7 @@ guide_geom.legend <- function(guide, layers, default_mapping) { include <- is.na(layer$show.legend[matched]) || layer$show.legend[matched] } else { - include <- is.na(layer$show.legend) || layer$show.legend + include <- isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend) } if (include) { @@ -271,7 +271,7 @@ guide_geom.legend <- function(guide, layers, default_mapping) { } } else { # This layer does not contribute to the legend - if (is.na(layer$show.legend) || !layer$show.legend) { + if (isTRUE(is.na(layer$show.legend)) || !isTRUE(layer$show.legend)) { # Default is to exclude it return(NULL) } else { diff --git a/R/guides-.r b/R/guides-.r index 06525a1b0b..f3a80c515f 100644 --- a/R/guides-.r +++ b/R/guides-.r @@ -173,14 +173,14 @@ guides_train <- function(scales, theme, guides, labels) { # this should be changed to testing guide == "none" # scale$legend is backward compatibility # if guides(XXX=FALSE), then scale_ZZZ(guides=XXX) is discarded. - if (guide == "none" || (is.logical(guide) && !guide)) next + if (identical(guide, "none") || isFALSE(guide)) next # check the validity of guide. # if guide is character, then find the guide object guide <- validate_guide(guide) # check the consistency of the guide and scale. - if (guide$available_aes != "any" && !scale$aesthetics %in% guide$available_aes) + if (!identical(guide$available_aes, "any") && !any(scale$aesthetics %in% guide$available_aes)) stop("Guide '", guide$name, "' cannot be used for '", scale$aesthetics, "'.") guide$title <- scale$make_title(guide$title %|W|% scale$name %|W|% labels[[output]]) diff --git a/R/position-stack.r b/R/position-stack.r index 225119a958..e54de07451 100644 --- a/R/position-stack.r +++ b/R/position-stack.r @@ -225,7 +225,7 @@ PositionFill <- ggproto("PositionFill", PositionStack, stack_var <- function(data) { if (!is.null(data$ymax)) { - if (any(data$ymin != 0 && data$ymax != 0, na.rm = TRUE)) { + if (any(data$ymin != 0 & data$ymax != 0, na.rm = TRUE)) { warning("Stacking not well defined when not anchored on the axis", call. = FALSE) } "ymax" diff --git a/R/scale-.r b/R/scale-.r index 7755bfbfa6..00ff579618 100644 --- a/R/scale-.r +++ b/R/scale-.r @@ -563,7 +563,8 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(), position <- match.arg(position, c("left", "right", "top", "bottom")) - if (is.null(breaks) && !is_position_aes(aesthetics) && guide != "none") { + # If the scale is non-positional, break = NULL means removing the guide + if (is.null(breaks) && all(!is_position_aes(aesthetics))) { guide <- "none" } @@ -634,7 +635,8 @@ discrete_scale <- function(aesthetics, scale_name, palette, name = waiver(), position <- match.arg(position, c("left", "right", "top", "bottom")) - if (is.null(breaks) && !is_position_aes(aesthetics) && guide != "none") { + # If the scale is non-positional, break = NULL means removing the guide + if (is.null(breaks) && all(!is_position_aes(aesthetics))) { guide <- "none" }