From 4b235fba9def65279826a289d91ff626ea733fb0 Mon Sep 17 00:00:00 2001 From: Pierre Formont Date: Fri, 14 Jul 2017 18:41:25 +0200 Subject: [PATCH] show.legend now handles named logical vectors (#2027) * add support for vectors in show.legend * fix behavior and add tests * fix code after review * update documentation and style * fix NEWS.md --- NEWS.md | 3 +++ R/guide-legend.r | 19 ++++++++++++++++++- R/layer.r | 6 ++++-- man/geom_abline.Rd | 4 +++- man/geom_bar.Rd | 4 +++- man/geom_bin2d.Rd | 4 +++- man/geom_blank.Rd | 4 +++- man/geom_boxplot.Rd | 4 +++- man/geom_contour.Rd | 4 +++- man/geom_count.Rd | 4 +++- man/geom_density.Rd | 4 +++- man/geom_density_2d.Rd | 4 +++- man/geom_dotplot.Rd | 4 +++- man/geom_errorbarh.Rd | 4 +++- man/geom_hex.Rd | 4 +++- man/geom_histogram.Rd | 4 +++- man/geom_jitter.Rd | 4 +++- man/geom_linerange.Rd | 4 +++- man/geom_map.Rd | 4 +++- man/geom_path.Rd | 4 +++- man/geom_point.Rd | 4 +++- man/geom_polygon.Rd | 4 +++- man/geom_qq.Rd | 4 +++- man/geom_quantile.Rd | 4 +++- man/geom_ribbon.Rd | 4 +++- man/geom_rug.Rd | 4 +++- man/geom_segment.Rd | 4 +++- man/geom_smooth.Rd | 4 +++- man/geom_spoke.Rd | 4 +++- man/geom_text.Rd | 4 +++- man/geom_tile.Rd | 4 +++- man/geom_violin.Rd | 4 +++- man/guide_legend.Rd | 7 +++++++ man/layer.Rd | 4 +++- man/stat_ecdf.Rd | 4 +++- man/stat_ellipse.Rd | 4 +++- man/stat_function.Rd | 4 +++- man/stat_identity.Rd | 4 +++- man/stat_summary.Rd | 4 +++- man/stat_summary_2d.Rd | 4 +++- man/stat_unique.Rd | 4 +++- tests/testthat/test-guides.R | 26 ++++++++++++++++++++++++++ 42 files changed, 169 insertions(+), 40 deletions(-) diff --git a/NEWS.md b/NEWS.md index aba915e5d..a1c06a8cd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/guide-legend.r b/R/guide-legend.r index 6431f4daa..fb9e2bcfe 100644 --- a/R/guide-legend.r +++ b/R/guide-legend.r @@ -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 diff --git a/R/layer.r b/R/layer.r index a8ff5da0f..ef1f145ba 100644 --- a/R/layer.r +++ b/R/layer.r @@ -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 } diff --git a/man/geom_abline.Rd b/man/geom_abline.Rd index 76e2d3ec8..866d17b78 100644 --- a/man/geom_abline.Rd +++ b/man/geom_abline.Rd @@ -45,7 +45,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{xintercept, yintercept, slope, intercept}{Parameters that control the position of the line. If these are set, \code{data}, \code{mapping} and diff --git a/man/geom_bar.Rd b/man/geom_bar.Rd index 7102ed367..3a2ac56fd 100644 --- a/man/geom_bar.Rd +++ b/man/geom_bar.Rd @@ -56,7 +56,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_bin2d.Rd b/man/geom_bin2d.Rd index 42ed22420..f2ef1ea0e 100644 --- a/man/geom_bin2d.Rd +++ b/man/geom_bin2d.Rd @@ -47,7 +47,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_blank.Rd b/man/geom_blank.Rd index 1ade056f6..040896e2c 100644 --- a/man/geom_blank.Rd +++ b/man/geom_blank.Rd @@ -40,7 +40,9 @@ to the paired geom/stat.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_boxplot.Rd b/man/geom_boxplot.Rd index 76ec4217e..a6be50bf8 100644 --- a/man/geom_boxplot.Rd +++ b/man/geom_boxplot.Rd @@ -68,7 +68,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_contour.Rd b/man/geom_contour.Rd index 8feee93ae..1172f1131 100644 --- a/man/geom_contour.Rd +++ b/man/geom_contour.Rd @@ -55,7 +55,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_count.Rd b/man/geom_count.Rd index d154382e5..7f2ea5be0 100644 --- a/man/geom_count.Rd +++ b/man/geom_count.Rd @@ -46,7 +46,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_density.Rd b/man/geom_density.Rd index 5e73ee0fc..cebc1f2f8 100644 --- a/man/geom_density.Rd +++ b/man/geom_density.Rd @@ -47,7 +47,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_density_2d.Rd b/man/geom_density_2d.Rd index c6acba63c..effbdaecc 100644 --- a/man/geom_density_2d.Rd +++ b/man/geom_density_2d.Rd @@ -54,7 +54,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_dotplot.Rd b/man/geom_dotplot.Rd index f26ab9594..2e7f48a8c 100644 --- a/man/geom_dotplot.Rd +++ b/man/geom_dotplot.Rd @@ -80,7 +80,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_errorbarh.Rd b/man/geom_errorbarh.Rd index 48b7d5f07..72377ddd7 100644 --- a/man/geom_errorbarh.Rd +++ b/man/geom_errorbarh.Rd @@ -44,7 +44,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_hex.Rd b/man/geom_hex.Rd index 5a71868c5..0f7d44e14 100644 --- a/man/geom_hex.Rd +++ b/man/geom_hex.Rd @@ -47,7 +47,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_histogram.Rd b/man/geom_histogram.Rd index 792fc4ff5..aa0038ce4 100644 --- a/man/geom_histogram.Rd +++ b/man/geom_histogram.Rd @@ -53,7 +53,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_jitter.Rd b/man/geom_jitter.Rd index 452f4a981..7eb56c81b 100644 --- a/man/geom_jitter.Rd +++ b/man/geom_jitter.Rd @@ -62,7 +62,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_linerange.Rd b/man/geom_linerange.Rd index 44e555e2d..8d311f581 100644 --- a/man/geom_linerange.Rd +++ b/man/geom_linerange.Rd @@ -64,7 +64,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_map.Rd b/man/geom_map.Rd index 4b4aef369..0886bbee0 100644 --- a/man/geom_map.Rd +++ b/man/geom_map.Rd @@ -45,7 +45,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_path.Rd b/man/geom_path.Rd index 9ba8da18d..f50b1522f 100644 --- a/man/geom_path.Rd +++ b/man/geom_path.Rd @@ -63,7 +63,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_point.Rd b/man/geom_point.Rd index 9d3c09c3f..b7396927a 100644 --- a/man/geom_point.Rd +++ b/man/geom_point.Rd @@ -44,7 +44,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_polygon.Rd b/man/geom_polygon.Rd index 7c27fb5bb..149676908 100644 --- a/man/geom_polygon.Rd +++ b/man/geom_polygon.Rd @@ -44,7 +44,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_qq.Rd b/man/geom_qq.Rd index 3c3001a18..e544c9368 100644 --- a/man/geom_qq.Rd +++ b/man/geom_qq.Rd @@ -53,7 +53,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_quantile.Rd b/man/geom_quantile.Rd index 7c5a2ac9c..a055df274 100644 --- a/man/geom_quantile.Rd +++ b/man/geom_quantile.Rd @@ -53,7 +53,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_ribbon.Rd b/man/geom_ribbon.Rd index 362be372b..46b4362c0 100644 --- a/man/geom_ribbon.Rd +++ b/man/geom_ribbon.Rd @@ -49,7 +49,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_rug.Rd b/man/geom_rug.Rd index cac1e1df5..78bc0aa24 100644 --- a/man/geom_rug.Rd +++ b/man/geom_rug.Rd @@ -48,7 +48,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_segment.Rd b/man/geom_segment.Rd index f3b9b4f57..dbde91d15 100644 --- a/man/geom_segment.Rd +++ b/man/geom_segment.Rd @@ -57,7 +57,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_smooth.Rd b/man/geom_smooth.Rd index 486d4c6fe..ed13729e9 100644 --- a/man/geom_smooth.Rd +++ b/man/geom_smooth.Rd @@ -64,7 +64,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_spoke.Rd b/man/geom_spoke.Rd index 8d7c077a2..30c66b17a 100644 --- a/man/geom_spoke.Rd +++ b/man/geom_spoke.Rd @@ -45,7 +45,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_text.Rd b/man/geom_text.Rd index 0bdd43b5e..9af7a112b 100644 --- a/man/geom_text.Rd +++ b/man/geom_text.Rd @@ -64,7 +64,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_tile.Rd b/man/geom_tile.Rd index 4e9621a23..80df52085 100644 --- a/man/geom_tile.Rd +++ b/man/geom_tile.Rd @@ -62,7 +62,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/geom_violin.Rd b/man/geom_violin.Rd index 0b07a8737..07fa116cc 100644 --- a/man/geom_violin.Rd +++ b/man/geom_violin.Rd @@ -57,7 +57,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/guide_legend.Rd b/man/guide_legend.Rd index e66f3c13f..68224e2c7 100644 --- a/man/guide_legend.Rd +++ b/man/guide_legend.Rd @@ -168,6 +168,13 @@ p + guides(col = guide_legend(ncol = 8, byrow = TRUE)) # 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)) + } } \seealso{ diff --git a/man/layer.Rd b/man/layer.Rd index 812d9b2df..2599f8990 100644 --- a/man/layer.Rd +++ b/man/layer.Rd @@ -52,7 +52,9 @@ layer.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} } \description{ A layer is a combination of data, stat and geom with a potential position diff --git a/man/stat_ecdf.Rd b/man/stat_ecdf.Rd index 32a90d935..557927ca9 100644 --- a/man/stat_ecdf.Rd +++ b/man/stat_ecdf.Rd @@ -49,7 +49,9 @@ a warning. If \code{TRUE} silently removes missing values.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/stat_ellipse.Rd b/man/stat_ellipse.Rd index cead43a38..caa0b3744 100644 --- a/man/stat_ellipse.Rd +++ b/man/stat_ellipse.Rd @@ -55,7 +55,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/stat_function.Rd b/man/stat_function.Rd index 777bf9ca1..de849b95e 100644 --- a/man/stat_function.Rd +++ b/man/stat_function.Rd @@ -51,7 +51,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/stat_identity.Rd b/man/stat_identity.Rd index 689fc7b2e..e9c76af00 100644 --- a/man/stat_identity.Rd +++ b/man/stat_identity.Rd @@ -39,7 +39,9 @@ to the paired geom/stat.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/stat_summary.Rd b/man/stat_summary.Rd index d273b435c..bac4ca27c 100644 --- a/man/stat_summary.Rd +++ b/man/stat_summary.Rd @@ -60,7 +60,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/stat_summary_2d.Rd b/man/stat_summary_2d.Rd index 7c11c85ee..305624597 100644 --- a/man/stat_summary_2d.Rd +++ b/man/stat_summary_2d.Rd @@ -63,7 +63,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/man/stat_unique.Rd b/man/stat_unique.Rd index 3964a3a0e..c07535415 100644 --- a/man/stat_unique.Rd +++ b/man/stat_unique.Rd @@ -43,7 +43,9 @@ a warning. If \code{TRUE}, missing values are silently removed.} \item{show.legend}{logical. Should this layer be included in the legends? \code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes.} +\code{FALSE} never includes, and \code{TRUE} always includes. +It can also be a named logical vector to finely select the aesthetics to +display.} \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index 81728651a..5f3c2af14 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -18,6 +18,32 @@ test_that("Colorbar respects show.legend in layer", { expect_true("guide-box" %in% ggplotGrob(p)$layout$name) }) +test_that("show.legend handles named vectors", { + n_legends <- function(p) { + g <- ggplotGrob(p) + gb <- which(g$layout$name == "guide-box") + if (length(gb) > 0) { + n <- length(g$grobs[[gb]]) - 1 + } else { + n <- 0 + } + n + } + + df <- data.frame(x = 1:3, y = 20:22) + p <- ggplot(df, aes(x = x, y = y, color = x, shape = factor(y))) + + geom_point(size = 20) + expect_equal(n_legends(p), 2) + + p <- ggplot(df, aes(x = x, y = y, color = x, shape = factor(y))) + + geom_point(size = 20, show.legend = c(color = FALSE)) + expect_equal(n_legends(p), 1) + + p <- ggplot(df, aes(x = x, y = y, color = x, shape = factor(y))) + + geom_point(size = 20, show.legend = c(color = FALSE, shape = FALSE)) + expect_equal(n_legends(p), 0) +}) + # Visual tests ------------------------------------------------------------