Skip to content

Commit a8e1e3a

Browse files
authored
Merge pull request #5 from tidyverse/master
update to tidyverse edition 20191206
2 parents 4313b5e + 16ed4d0 commit a8e1e3a

File tree

7 files changed

+89
-108
lines changed

7 files changed

+89
-108
lines changed

R/guide-bins.R

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -161,38 +161,21 @@ guide_geom.bins <- function(guide, layers, default_mapping) {
161161
guide$geoms <- lapply(layers, function(layer) {
162162
matched <- matched_aes(layer, guide, default_mapping)
163163

164-
if (length(matched) > 0) {
165-
# This layer contributes to the legend
166-
167-
# check if this layer should be included, different behaviour depending on
168-
# if show.legend is a logical or a named logical vector
169-
if (!is.null(names(layer$show.legend))) {
170-
layer$show.legend <- rename_aes(layer$show.legend)
171-
include <- is.na(layer$show.legend[matched]) ||
172-
layer$show.legend[matched]
173-
} else {
174-
include <- is.na(layer$show.legend) || layer$show.legend
175-
}
164+
# check if this layer should be included
165+
include <- include_layer_in_guide(layer, matched)
176166

177-
if (include) {
178-
# Default is to include it
167+
if (!include) {
168+
return(NULL)
169+
}
179170

180-
# Filter out set aesthetics that can't be applied to the legend
181-
n <- vapply(layer$aes_params, length, integer(1))
182-
params <- layer$aes_params[n == 1]
171+
if (length(matched) > 0) {
172+
# Filter out set aesthetics that can't be applied to the legend
173+
n <- vapply(layer$aes_params, length, integer(1))
174+
params <- layer$aes_params[n == 1]
183175

184-
data <- layer$geom$use_defaults(guide$key[matched], params)
185-
} else {
186-
return(NULL)
187-
}
176+
data <- layer$geom$use_defaults(guide$key[matched], params)
188177
} else {
189-
# This layer does not contribute to the legend
190-
if (is.na(layer$show.legend) || !layer$show.legend) {
191-
# Default is to exclude it
192-
return(NULL)
193-
} else {
194-
data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ]
195-
}
178+
data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ]
196179
}
197180

198181
# override.aes in guide_legend manually changes the geom

R/guide-colorbar.r

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,8 @@ guide_geom.colorbar <- function(guide, layers, default_mapping) {
249249
return(NULL)
250250
}
251251

252-
# check if this layer should be included, different behaviour depending on
253-
# if show.legend is a logical or a named logical vector
254-
if (is_named(layer$show.legend)) {
255-
layer$show.legend <- rename_aes(layer$show.legend)
256-
show_legend <- layer$show.legend[matched]
257-
# we cannot use `isTRUE(is.na(show_legend))` here because
258-
# 1. show_legend can be multiple NAs
259-
# 2. isTRUE() was not tolerant for a named TRUE
260-
show_legend <- show_legend[!is.na(show_legend)]
261-
include <- length(show_legend) == 0 || any(show_legend)
262-
} else {
263-
include <- isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend)
264-
}
265-
266-
if (include) {
252+
# check if this layer should be included
253+
if (include_layer_in_guide(layer, matched)) {
267254
layer
268255
} else {
269256
NULL

R/guide-legend.r

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -246,42 +246,21 @@ guide_geom.legend <- function(guide, layers, default_mapping) {
246246
guide$geoms <- lapply(layers, function(layer) {
247247
matched <- matched_aes(layer, guide, default_mapping)
248248

249+
# check if this layer should be included
250+
include <- include_layer_in_guide(layer, matched)
251+
252+
if (!include) {
253+
return(NULL)
254+
}
255+
249256
if (length(matched) > 0) {
250-
# This layer contributes to the legend
251-
252-
# check if this layer should be included, different behaviour depending on
253-
# if show.legend is a logical or a named logical vector
254-
if (is_named(layer$show.legend)) {
255-
layer$show.legend <- rename_aes(layer$show.legend)
256-
show_legend <- layer$show.legend[matched]
257-
# we cannot use `isTRUE(is.na(show_legend))` here because
258-
# 1. show_legend can be multiple NAs
259-
# 2. isTRUE() was not tolerant for a named TRUE
260-
show_legend <- show_legend[!is.na(show_legend)]
261-
include <- length(show_legend) == 0 || any(show_legend)
262-
} else {
263-
include <- isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend)
264-
}
265-
266-
if (include) {
267-
# Default is to include it
268-
269-
# Filter out set aesthetics that can't be applied to the legend
270-
n <- vapply(layer$aes_params, length, integer(1))
271-
params <- layer$aes_params[n == 1]
272-
273-
data <- layer$geom$use_defaults(guide$key[matched], params)
274-
} else {
275-
return(NULL)
276-
}
257+
# Filter out set aesthetics that can't be applied to the legend
258+
n <- vapply(layer$aes_params, length, integer(1))
259+
params <- layer$aes_params[n == 1]
260+
261+
data <- layer$geom$use_defaults(guide$key[matched], params)
277262
} else {
278-
# This layer does not contribute to the legend
279-
if (isTRUE(is.na(layer$show.legend)) || !isTRUE(layer$show.legend)) {
280-
# Default is to exclude it
281-
return(NULL)
282-
} else {
283-
data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ]
284-
}
263+
data <- layer$geom$use_defaults(NULL, layer$aes_params)[rep(1, nrow(guide$key)), ]
285264
}
286265

287266
# override.aes in guide_legend manually changes the geom

R/guides-.r

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,35 @@ matched_aes <- function(layer, guide, defaults) {
363363
matched <- setdiff(matched, names(layer$geom_params))
364364
setdiff(matched, names(layer$aes_params))
365365
}
366+
367+
# This function is used by guides in guide_geom.* to determine whether
368+
# a given layer should be included in the guide
369+
# `matched` is the set of aesthetics that match between the layer and the guide
370+
include_layer_in_guide <- function(layer, matched) {
371+
if (!is.logical(layer$show.legend)) {
372+
warning("`show.legend` must be a logical vector.", call. = FALSE)
373+
layer$show.legend <- FALSE # save back to layer so we don't issue this warning more than once
374+
return(FALSE)
375+
}
376+
377+
if (length(matched) > 0) {
378+
# This layer contributes to the legend
379+
380+
# check if this layer should be included, different behaviour depending on
381+
# if show.legend is a logical or a named logical vector
382+
if (is_named(layer$show.legend)) {
383+
layer$show.legend <- rename_aes(layer$show.legend)
384+
show_legend <- layer$show.legend[matched]
385+
# we cannot use `isTRUE(is.na(show_legend))` here because
386+
# 1. show_legend can be multiple NAs
387+
# 2. isTRUE() was not tolerant for a named TRUE
388+
show_legend <- show_legend[!is.na(show_legend)]
389+
return(length(show_legend) == 0 || any(show_legend))
390+
}
391+
return(all(is.na(layer$show.legend)) || isTRUE(layer$show.legend))
392+
}
393+
394+
# This layer does not contribute to the legend.
395+
# Default is to exclude it, except if it is explicitly turned on
396+
isTRUE(layer$show.legend)
397+
}

README.Rmd

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ knitr::opts_chunk$set(
1717
[![Travis Build Status](https://travis-ci.org/tidyverse/ggplot2.svg?branch=master)](https://travis-ci.org/tidyverse/ggplot2)
1818
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/tidyverse/ggplot2?branch=master&svg=true)](https://ci.appveyor.com/project/tidyverse/ggplot2)
1919
[![Coverage Status](https://img.shields.io/codecov/c/github/tidyverse/ggplot2/master.svg)](https://codecov.io/github/tidyverse/ggplot2?branch=master)
20-
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/ggplot2)](https://cran.r-project.org/package=ggplot2)
20+
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/ggplot2)](https://cran.r-project.org/package=ggplot2)
2121

2222
## Overview
2323

24-
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.
24+
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.
2525

2626
## Installation
2727

@@ -32,7 +32,7 @@ install.packages("tidyverse")
3232
# Alternatively, install just ggplot2:
3333
install.packages("ggplot2")
3434
35-
# Or the the development version from GitHub:
35+
# Or the development version from GitHub:
3636
# install.packages("devtools")
3737
devtools::install_github("tidyverse/ggplot2")
3838
```
@@ -59,17 +59,17 @@ ggplot(mpg, aes(displ, hwy, colour = class)) +
5959

6060
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.
6161

62-
If you are looking for innovation, look to ggplot2's rich ecosystem of extensions. See a community maintained list at <http://www.ggplot2-exts.org/gallery/>.
62+
If you are looking for innovation, look to ggplot2's rich ecosystem of extensions. See a community maintained list at <https://www.ggplot2-exts.org/gallery/>.
6363

6464
## Learning ggplot2
6565

6666
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:
6767

68-
1. The [data visualisation][r4ds-vis] and
69-
[graphics for communication][r4ds-comm] chapters in
70-
[R for data science][r4ds]. R for data science is designed to
68+
1. The [Data Visualisation][r4ds-vis] and
69+
[Graphics for communication][r4ds-comm] chapters in
70+
[R for Data Science][r4ds]. R for Data Science is designed to
7171
give you a comprehensive introduction to the
72-
[tidyverse](http://tidyverse.org), and these two chapters will
72+
[tidyverse](https://tidyverse.org), and these two chapters will
7373
get you up to speed with the essentials of ggplot2 as quickly as
7474
possible.
7575

@@ -95,11 +95,11 @@ There are two main places to get help with ggplot2:
9595
created a reproducible example that illustrates your problem.
9696

9797
[community]: https://community.rstudio.com/
98-
[ggplot2-book]: http://amzn.to/2fncG50
99-
[gg-book]: http://amzn.to/2ef1eWp
100-
[so]: http://stackoverflow.com/questions/tagged/ggplot2?sort=frequent&pageSize=50
98+
[ggplot2-book]: https://amzn.to/2fncG50
99+
[gg-book]: https://amzn.to/2ef1eWp
100+
[so]: https://stackoverflow.com/questions/tagged/ggplot2?sort=frequent&pageSize=50
101101
[cookbook]: https://amzn.to/2TU78ip
102-
[r4ds]: http://r4ds.had.co.nz
103-
[r4ds-vis]: http://r4ds.had.co.nz/data-visualisation.html
104-
[r4ds-comm]: http://r4ds.had.co.nz/graphics-for-communication.html
105-
[oreilly]: http://shop.oreilly.com/product/0636920052807.do
102+
[r4ds]: https://r4ds.had.co.nz
103+
[r4ds-vis]: https://r4ds.had.co.nz/data-visualisation.html
104+
[r4ds-comm]: https://r4ds.had.co.nz/graphics-for-communication.html
105+
[oreilly]: https://shop.oreilly.com/product/0636920052807.do

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ Status](https://travis-ci.org/tidyverse/ggplot2.svg?branch=master)](https://trav
99
Status](https://ci.appveyor.com/api/projects/status/github/tidyverse/ggplot2?branch=master&svg=true)](https://ci.appveyor.com/project/tidyverse/ggplot2)
1010
[![Coverage
1111
Status](https://img.shields.io/codecov/c/github/tidyverse/ggplot2/master.svg)](https://codecov.io/github/tidyverse/ggplot2?branch=master)
12-
[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/ggplot2)](https://cran.r-project.org/package=ggplot2)
12+
[![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/ggplot2)](https://cran.r-project.org/package=ggplot2)
1313

1414
## Overview
1515

1616
ggplot2 is a system for declaratively creating graphics, based on [The
17-
Grammar of Graphics](http://amzn.to/2ef1eWp). You provide the data, tell
18-
ggplot2 how to map variables to aesthetics, what graphical primitives to
19-
use, and it takes care of the details.
17+
Grammar of Graphics](https://amzn.to/2ef1eWp). You provide the data,
18+
tell ggplot2 how to map variables to aesthetics, what graphical
19+
primitives to use, and it takes care of the details.
2020

2121
## Installation
2222

@@ -27,7 +27,7 @@ install.packages("tidyverse")
2727
# Alternatively, install just ggplot2:
2828
install.packages("ggplot2")
2929

30-
# Or the the development version from GitHub:
30+
# Or the development version from GitHub:
3131
# install.packages("devtools")
3232
devtools::install_github("tidyverse/ggplot2")
3333
```
@@ -67,26 +67,26 @@ behaviour we will do them for compelling reasons.
6767

6868
If you are looking for innovation, look to ggplot2’s rich ecosystem of
6969
extensions. See a community maintained list at
70-
<http://www.ggplot2-exts.org/gallery/>.
70+
<https://www.ggplot2-exts.org/gallery/>.
7171

7272
## Learning ggplot2
7373

7474
If you are new to ggplot2 you are better off starting with a systematic
7575
introduction, rather than trying to learn from reading individual
7676
documentation pages. Currently, there are three good places to start:
7777

78-
1. The [data
79-
visualisation](http://r4ds.had.co.nz/data-visualisation.html) and
80-
[graphics for
81-
communication](http://r4ds.had.co.nz/graphics-for-communication.html)
82-
chapters in [R for data science](http://r4ds.had.co.nz). R for data
83-
science is designed to give you a comprehensive introduction to the
84-
[tidyverse](http://tidyverse.org), and these two chapters will get
78+
1. The [Data
79+
Visualisation](https://r4ds.had.co.nz/data-visualisation.html) and
80+
[Graphics for
81+
communication](https://r4ds.had.co.nz/graphics-for-communication.html)
82+
chapters in [R for Data Science](https://r4ds.had.co.nz). R for Data
83+
Science is designed to give you a comprehensive introduction to the
84+
[tidyverse](https://tidyverse.org), and these two chapters will get
8585
you up to speed with the essentials of ggplot2 as quickly as
8686
possible.
8787

8888
2. If you’d like to take an online course, try [Data Visualization in R
89-
With ggplot2](http://shop.oreilly.com/product/0636920052807.do) by
89+
With ggplot2](https://shop.oreilly.com/product/0636920052807.do) by
9090
Kara Woo.
9191

9292
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:
9595
set of recipes to solve common graphics problems.
9696

9797
If you’ve mastered the basics and want to learn more, read [ggplot2:
98-
Elegant Graphics for Data Analysis](http://amzn.to/2fncG50). It
98+
Elegant Graphics for Data Analysis](https://amzn.to/2fncG50). It
9999
describes the theoretical underpinnings of ggplot2 and shows you how all
100100
the pieces fit together. This book helps you understand the theory that
101101
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:
111111
friendly place to ask any questions about ggplot2.
112112

113113
2. [Stack
114-
Overflow](http://stackoverflow.com/questions/tagged/ggplot2?sort=frequent&pageSize=50)
114+
Overflow](https://stackoverflow.com/questions/tagged/ggplot2?sort=frequent&pageSize=50)
115115
is a great source of answers to common ggplot2 questions. It is also
116116
a great place to get help, once you have created a reproducible
117117
example that illustrates your problem.

man/figures/README-example-1.png

-66.7 KB
Loading

0 commit comments

Comments
 (0)