diff --git a/R/guide-bins.R b/R/guide-bins.R index e6ad111ffa..2e5b25cf79 100644 --- a/R/guide-bins.R +++ b/R/guide-bins.R @@ -161,38 +161,21 @@ guide_geom.bins <- function(guide, layers, default_mapping) { guide$geoms <- lapply(layers, function(layer) { matched <- matched_aes(layer, guide, default_mapping) - if (length(matched) > 0) { - # This layer contributes to the 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 - } + # check if this layer should be included + include <- include_layer_in_guide(layer, matched) - if (include) { - # Default is to include it + if (!include) { + return(NULL) + } - # Filter out set aesthetics that can't be applied to the legend - n <- vapply(layer$aes_params, length, integer(1)) - params <- layer$aes_params[n == 1] + if (length(matched) > 0) { + # Filter out set aesthetics that can't be applied to the legend + n <- vapply(layer$aes_params, length, integer(1)) + params <- layer$aes_params[n == 1] - data <- layer$geom$use_defaults(guide$key[matched], params) - } else { - return(NULL) - } + data <- layer$geom$use_defaults(guide$key[matched], params) } else { - # This layer does not contribute to the legend - if (is.na(layer$show.legend) || !layer$show.legend) { - # Default is to exclude it - return(NULL) - } else { - data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ] - } + data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ] } # override.aes in guide_legend manually changes the geom diff --git a/R/guide-colorbar.r b/R/guide-colorbar.r index 95e62297e4..b75f3dc187 100644 --- a/R/guide-colorbar.r +++ b/R/guide-colorbar.r @@ -249,21 +249,8 @@ guide_geom.colorbar <- function(guide, layers, default_mapping) { return(NULL) } - # check if this layer should be included, different behaviour depending on - # if show.legend is a logical or a named logical vector - if (is_named(layer$show.legend)) { - layer$show.legend <- rename_aes(layer$show.legend) - show_legend <- layer$show.legend[matched] - # we cannot use `isTRUE(is.na(show_legend))` here because - # 1. show_legend can be multiple NAs - # 2. isTRUE() was not tolerant for a named TRUE - show_legend <- show_legend[!is.na(show_legend)] - include <- length(show_legend) == 0 || any(show_legend) - } else { - include <- isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend) - } - - if (include) { + # check if this layer should be included + if (include_layer_in_guide(layer, matched)) { layer } else { NULL diff --git a/R/guide-legend.r b/R/guide-legend.r index 77ba2f67c0..a7a6de653d 100644 --- a/R/guide-legend.r +++ b/R/guide-legend.r @@ -246,42 +246,21 @@ guide_geom.legend <- function(guide, layers, default_mapping) { guide$geoms <- lapply(layers, function(layer) { matched <- matched_aes(layer, guide, default_mapping) + # check if this layer should be included + include <- include_layer_in_guide(layer, matched) + + if (!include) { + return(NULL) + } + if (length(matched) > 0) { - # This layer contributes to the 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_named(layer$show.legend)) { - layer$show.legend <- rename_aes(layer$show.legend) - show_legend <- layer$show.legend[matched] - # we cannot use `isTRUE(is.na(show_legend))` here because - # 1. show_legend can be multiple NAs - # 2. isTRUE() was not tolerant for a named TRUE - show_legend <- show_legend[!is.na(show_legend)] - include <- length(show_legend) == 0 || any(show_legend) - } else { - include <- isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend) - } - - if (include) { - # Default is to include it - - # Filter out set aesthetics that can't be applied to the legend - n <- vapply(layer$aes_params, length, integer(1)) - params <- layer$aes_params[n == 1] - - data <- layer$geom$use_defaults(guide$key[matched], params) - } else { - return(NULL) - } + # Filter out set aesthetics that can't be applied to the legend + n <- vapply(layer$aes_params, length, integer(1)) + params <- layer$aes_params[n == 1] + + data <- layer$geom$use_defaults(guide$key[matched], params) } else { - # This layer does not contribute to the legend - if (isTRUE(is.na(layer$show.legend)) || !isTRUE(layer$show.legend)) { - # Default is to exclude it - return(NULL) - } else { - data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ] - } + data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ] } # override.aes in guide_legend manually changes the geom diff --git a/R/guides-.r b/R/guides-.r index 40284afa00..061317227b 100644 --- a/R/guides-.r +++ b/R/guides-.r @@ -363,3 +363,35 @@ matched_aes <- function(layer, guide, defaults) { matched <- setdiff(matched, names(layer$geom_params)) setdiff(matched, names(layer$aes_params)) } + +# This function is used by guides in guide_geom.* to determine whether +# a given layer should be included in the guide +# `matched` is the set of aesthetics that match between the layer and the guide +include_layer_in_guide <- function(layer, matched) { + if (!is.logical(layer$show.legend)) { + warning("`show.legend` must be a logical vector.", call. = FALSE) + layer$show.legend <- FALSE # save back to layer so we don't issue this warning more than once + return(FALSE) + } + + if (length(matched) > 0) { + # This layer contributes to the 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_named(layer$show.legend)) { + layer$show.legend <- rename_aes(layer$show.legend) + show_legend <- layer$show.legend[matched] + # we cannot use `isTRUE(is.na(show_legend))` here because + # 1. show_legend can be multiple NAs + # 2. isTRUE() was not tolerant for a named TRUE + show_legend <- show_legend[!is.na(show_legend)] + return(length(show_legend) == 0 || any(show_legend)) + } + return(all(is.na(layer$show.legend)) || isTRUE(layer$show.legend)) + } + + # This layer does not contribute to the legend. + # Default is to exclude it, except if it is explicitly turned on + isTRUE(layer$show.legend) +} diff --git a/R/layer.r b/R/layer.r index 633fe195a4..858c41b111 100644 --- a/R/layer.r +++ b/R/layer.r @@ -80,10 +80,6 @@ layer <- function(geom = NULL, stat = NULL, show.legend <- params$show_guide params$show_guide <- NULL } - if (!is.logical(show.legend)) { - warning("`show.legend` must be a logical vector.", call. = FALSE) - show.legend <- FALSE - } # we validate mapping before data because in geoms and stats # the mapping is listed before the data argument; this causes diff --git a/README.Rmd b/README.Rmd index aa049376f9..963398e592 100644 --- a/README.Rmd +++ b/README.Rmd @@ -17,11 +17,11 @@ knitr::opts_chunk$set( [![Travis Build Status](https://travis-ci.org/tidyverse/ggplot2.svg?branch=master)](https://travis-ci.org/tidyverse/ggplot2) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/tidyverse/ggplot2?branch=master&svg=true)](https://ci.appveyor.com/project/tidyverse/ggplot2) [![Coverage Status](https://img.shields.io/codecov/c/github/tidyverse/ggplot2/master.svg)](https://codecov.io/github/tidyverse/ggplot2?branch=master) -[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/ggplot2)](https://cran.r-project.org/package=ggplot2) +[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/ggplot2)](https://cran.r-project.org/package=ggplot2) ## Overview -ggplot2 is a system for declaratively creating graphics, based on [The Grammar of Graphics](http://amzn.to/2ef1eWp). You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. +ggplot2 is a system for declaratively creating graphics, based on [The Grammar of Graphics](https://amzn.to/2ef1eWp). You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. ## Installation @@ -32,7 +32,7 @@ install.packages("tidyverse") # Alternatively, install just ggplot2: install.packages("ggplot2") -# Or the the development version from GitHub: +# Or the development version from GitHub: # install.packages("devtools") devtools::install_github("tidyverse/ggplot2") ``` @@ -59,17 +59,17 @@ ggplot(mpg, aes(displ, hwy, colour = class)) + ggplot2 is now over 10 years old and is used by hundreds of thousands of people to make millions of plots. That means, by-and-large, ggplot2 itself changes relatively little. When we do make changes, they will be generally to add new functions or arguments rather than changing the behaviour of existing functions, and if we do make changes to existing behaviour we will do them for compelling reasons. -If you are looking for innovation, look to ggplot2's rich ecosystem of extensions. See a community maintained list at . +If you are looking for innovation, look to ggplot2's rich ecosystem of extensions. See a community maintained list at . ## Learning ggplot2 If you are new to ggplot2 you are better off starting with a systematic introduction, rather than trying to learn from reading individual documentation pages. Currently, there are three good places to start: -1. The [data visualisation][r4ds-vis] and - [graphics for communication][r4ds-comm] chapters in - [R for data science][r4ds]. R for data science is designed to +1. The [Data Visualisation][r4ds-vis] and + [Graphics for communication][r4ds-comm] chapters in + [R for Data Science][r4ds]. R for Data Science is designed to give you a comprehensive introduction to the - [tidyverse](http://tidyverse.org), and these two chapters will + [tidyverse](https://tidyverse.org), and these two chapters will get you up to speed with the essentials of ggplot2 as quickly as possible. @@ -95,11 +95,11 @@ There are two main places to get help with ggplot2: created a reproducible example that illustrates your problem. [community]: https://community.rstudio.com/ -[ggplot2-book]: http://amzn.to/2fncG50 -[gg-book]: http://amzn.to/2ef1eWp -[so]: http://stackoverflow.com/questions/tagged/ggplot2?sort=frequent&pageSize=50 +[ggplot2-book]: https://amzn.to/2fncG50 +[gg-book]: https://amzn.to/2ef1eWp +[so]: https://stackoverflow.com/questions/tagged/ggplot2?sort=frequent&pageSize=50 [cookbook]: https://amzn.to/2TU78ip -[r4ds]: http://r4ds.had.co.nz -[r4ds-vis]: http://r4ds.had.co.nz/data-visualisation.html -[r4ds-comm]: http://r4ds.had.co.nz/graphics-for-communication.html -[oreilly]: http://shop.oreilly.com/product/0636920052807.do +[r4ds]: https://r4ds.had.co.nz +[r4ds-vis]: https://r4ds.had.co.nz/data-visualisation.html +[r4ds-comm]: https://r4ds.had.co.nz/graphics-for-communication.html +[oreilly]: https://shop.oreilly.com/product/0636920052807.do diff --git a/README.md b/README.md index 9363a3ddcb..0585a8f900 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,14 @@ Status](https://travis-ci.org/tidyverse/ggplot2.svg?branch=master)](https://trav Status](https://ci.appveyor.com/api/projects/status/github/tidyverse/ggplot2?branch=master&svg=true)](https://ci.appveyor.com/project/tidyverse/ggplot2) [![Coverage Status](https://img.shields.io/codecov/c/github/tidyverse/ggplot2/master.svg)](https://codecov.io/github/tidyverse/ggplot2?branch=master) -[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/ggplot2)](https://cran.r-project.org/package=ggplot2) +[![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/ggplot2)](https://cran.r-project.org/package=ggplot2) ## Overview ggplot2 is a system for declaratively creating graphics, based on [The -Grammar of Graphics](http://amzn.to/2ef1eWp). You provide the data, tell -ggplot2 how to map variables to aesthetics, what graphical primitives to -use, and it takes care of the details. +Grammar of Graphics](https://amzn.to/2ef1eWp). You provide the data, +tell ggplot2 how to map variables to aesthetics, what graphical +primitives to use, and it takes care of the details. ## Installation @@ -27,7 +27,7 @@ install.packages("tidyverse") # Alternatively, install just ggplot2: install.packages("ggplot2") -# Or the the development version from GitHub: +# Or the development version from GitHub: # install.packages("devtools") devtools::install_github("tidyverse/ggplot2") ``` @@ -67,7 +67,7 @@ behaviour we will do them for compelling reasons. If you are looking for innovation, look to ggplot2’s rich ecosystem of extensions. See a community maintained list at -. +. ## Learning ggplot2 @@ -75,18 +75,18 @@ If you are new to ggplot2 you are better off starting with a systematic introduction, rather than trying to learn from reading individual documentation pages. Currently, there are three good places to start: -1. The [data - visualisation](http://r4ds.had.co.nz/data-visualisation.html) and - [graphics for - communication](http://r4ds.had.co.nz/graphics-for-communication.html) - chapters in [R for data science](http://r4ds.had.co.nz). R for data - science is designed to give you a comprehensive introduction to the - [tidyverse](http://tidyverse.org), and these two chapters will get +1. The [Data + Visualisation](https://r4ds.had.co.nz/data-visualisation.html) and + [Graphics for + communication](https://r4ds.had.co.nz/graphics-for-communication.html) + chapters in [R for Data Science](https://r4ds.had.co.nz). R for Data + Science is designed to give you a comprehensive introduction to the + [tidyverse](https://tidyverse.org), and these two chapters will get you up to speed with the essentials of ggplot2 as quickly as possible. 2. If you’d like to take an online course, try [Data Visualization in R - With ggplot2](http://shop.oreilly.com/product/0636920052807.do) by + With ggplot2](https://shop.oreilly.com/product/0636920052807.do) by Kara Woo. 3. If you want to dive into making common graphics as quickly as @@ -95,7 +95,7 @@ documentation pages. Currently, there are three good places to start: set of recipes to solve common graphics problems. If you’ve mastered the basics and want to learn more, read [ggplot2: -Elegant Graphics for Data Analysis](http://amzn.to/2fncG50). It +Elegant Graphics for Data Analysis](https://amzn.to/2fncG50). It describes the theoretical underpinnings of ggplot2 and shows you how all the pieces fit together. This book helps you understand the theory that underpins ggplot2, and will help you create new types of graphics @@ -111,7 +111,7 @@ There are two main places to get help with ggplot2: friendly place to ask any questions about ggplot2. 2. [Stack - Overflow](http://stackoverflow.com/questions/tagged/ggplot2?sort=frequent&pageSize=50) + Overflow](https://stackoverflow.com/questions/tagged/ggplot2?sort=frequent&pageSize=50) is a great source of answers to common ggplot2 questions. It is also a great place to get help, once you have created a reproducible example that illustrates your problem. diff --git a/man/figures/README-example-1.png b/man/figures/README-example-1.png index dd2059d806..9317eed5ba 100644 Binary files a/man/figures/README-example-1.png and b/man/figures/README-example-1.png differ