Skip to content

update to tidyverse edition 20191206 #5

New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 11 additions & 28 deletions R/guide-bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 2 additions & 15 deletions R/guide-colorbar.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 13 additions & 34 deletions R/guide-legend.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions R/guides-.r
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 0 additions & 4 deletions R/layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
```
Expand All @@ -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 <http://www.ggplot2-exts.org/gallery/>.
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/>.

## 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.

Expand All @@ -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
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
```
Expand Down Expand Up @@ -67,26 +67,26 @@ 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
<http://www.ggplot2-exts.org/gallery/>.
<https://www.ggplot2-exts.org/gallery/>.

## 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](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
Expand All @@ -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
Expand All @@ -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.
Binary file modified man/figures/README-example-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.