Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

show.legend now handles named logical vectors #2027

Merged
merged 6 commits into from Jul 14, 2017
View
@@ -1,5 +1,8 @@
# ggplot2 2.2.1.9000
+* The `show.legend` parameter now accepts a named logical vector to hide/show
+ only some aesthetics in the legend (@tutuchan, #1798)
+
* Default colour maps for continuous data are controlled by global options
`ggplot2.continuous.colour` and `ggplot2.continuous.fill`, which can be set to
either `"gradient"` or `"viridis"` (@karawoo).
View
@@ -132,6 +132,13 @@
#'
#' # reversed order legend
#' p + guides(col = guide_legend(reverse = TRUE))
+#'
+#' # hide some aesthetics from the legend
+#' p4 <- ggplot(mtcars, aes(mpg, qsec, colour = factor(vs), shape = factor(am))) +
+#' geom_point()
+#' p4 + geom_line()
+#' p4 + geom_line(show.legend = c(color = FALSE))
+#'
#' }
guide_legend <- function(
@@ -249,7 +256,17 @@ guide_geom.legend <- function(guide, layers, default_mapping) {
if (length(matched) > 0) {
# This layer contributes to the legend
- if (is.na(layer$show.legend) || layer$show.legend) {
+
+ # check if this layer should be included, different behaviour depending on
+ # if show.legend is a logical or a named logical vector
+ if (!is.null(names(layer$show.legend))) {
+ layer$show.legend <- rename_aes(layer$show.legend)
+ include <- is.na(layer$show.legend[matched]) || layer$show.legend[matched]
+ } else {
+ include <- is.na(layer$show.legend) || layer$show.legend
+ }
+
+ if (include) {
# Default is to include it
# Filter out set aesthetics that can't be applied to the legend
View
@@ -31,6 +31,8 @@
#' @param show.legend logical. Should this layer be included in the legends?
#' `NA`, the default, includes if any aesthetics are mapped.
#' `FALSE` never includes, and `TRUE` always includes.
+#' It can also be a named logical vector to finely select the aesthetics to
+#' display.
#' @param inherit.aes If `FALSE`, overrides the default aesthetics,
#' rather than combining with them. This is most useful for helper functions
#' that define both data and aesthetics and shouldn't inherit behaviour from
@@ -76,8 +78,8 @@ layer <- function(geom = NULL, stat = NULL,
show.legend <- params$show_guide
params$show_guide <- NULL
}
- if (!is.logical(show.legend) || length(show.legend) != 1) {
- warning("`show.legend` must be a logical vector of length 1.", call. = FALSE)
+ if (!is.logical(show.legend)) {
+ warning("`show.legend` must be a logical vector.", call. = FALSE)
show.legend <- FALSE
}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.