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

Conversation

Projects
None yet
3 participants
Contributor

Tutuchan commented Feb 2, 2017

This is a followup to #1798.

The rename_aes call is used to handle color/colour.

@Tutuchan Tutuchan add support for vectors in show.legend
248da96
Contributor

Tutuchan commented Feb 3, 2017

This is not working as intended, I'll try and fix it.

@Tutuchan Tutuchan fix behavior and add tests
01b6bea
R/guide-legend.r
@@ -247,9 +254,16 @@ guide_geom.legend <- function(guide, layers, default_mapping) {
guide$geoms <- plyr::llply(layers, function(layer) {
matched <- matched_aes(layer, guide, default_mapping)
+ layer$show.legend <- unlist(rename_aes(as.list(layer$show.legend)))
@hadley

hadley Jul 3, 2017

Owner

Shouldn't this be inside the if block?

@Tutuchan

Tutuchan Jul 14, 2017

Contributor

You're right, I moved it.

R/guide-legend.r
if (length(matched) > 0) {
# This layer contributes to the legend
- if (is.na(layer$show.legend) || layer$show.legend) {
+ include <- ifelse(
@hadley

hadley Jul 3, 2017

Owner

I think this should be an if statement with separate then and else blocks. Can you please rewrite?

@Tutuchan

Tutuchan Jul 14, 2017

Contributor

Done.

tests/testthat/test-guides.R
@@ -16,6 +16,25 @@ test_that("Colorbar respects show.legend in layer", {
p <- ggplot(df, aes(x = x, y = y, color = x)) +
geom_point(size = 20, shape = 21, show.legend = TRUE)
expect_true("guide-box" %in% ggplotGrob(p)$layout$name)
+
+
+ df <- data.frame(x = 1:3, y = 20:22)
@hadley

hadley Jul 3, 2017

Owner

Can you please put this in a new test?

@Tutuchan

Tutuchan Jul 14, 2017

Contributor

Done.

tests/testthat/test-guides.R
+ geom_point(size = 20)
+ expect_length({
+ g <- ggplotGrob(p)
+ g$grobs[[which(g$layout$name == "guide-box")]]
@hadley

hadley Jul 3, 2017

Owner

I think you should pull this out into a helper function. Something like:

n_legends <- function(g) {
  sum(g$layout$name == "guide-box")
}

Then you can use expect_equal() consistently.

@Tutuchan

Tutuchan Jul 14, 2017

Contributor

Indeed, it looks better. I changed the code as suggested.

Owner

hadley commented Jul 13, 2017

@Tutuchan are you interested in finishing off this PR?

Contributor

Tutuchan commented Jul 14, 2017

Yes, sorry, I have been busy these past couple of weeks. I'll get to it today.

@Tutuchan Tutuchan fix code after review
b27e04f
@hadley

Looking good. Just a few more minor changes.

Can you please also add a bullet describing the change to NEWS.md? And update the documentation for the show.legend parameter?

R/guide-legend.r
- if (is.na(layer$show.legend) || layer$show.legend) {
+
+ # rename color to colour
+ layer$show.legend <- unlist(rename_aes(as.list(layer$show.legend)))
@hadley

hadley Jul 14, 2017

Owner

I think this rename would be better off inside the branch where names are found.

Also why are you converting to a list and back?

@Tutuchan

Tutuchan Jul 14, 2017

Contributor

When I first tested this, I thought the rename_aes() call was not working as intended because layer$show.legend is a named vector and not a list. But it does actually work so I must have been mistaken. I'm fixing this.

R/guide-legend.r
+
+ # check if this layer should be included, different behaviour depending on
+ # if show.legend is a logical or a named logical vector
+ include <- if (!is.null(names(layer$show.legend))) {
@hadley

hadley Jul 14, 2017

Owner

Can you please move include <- inside the individual blocks.

tests/testthat/test-guides.R
+ g <- ggplotGrob(p)
+ length(g$grobs[[which(g$layout$name == "guide-box")]]) - 1
+ }
+ has_legends <- function(p){
@hadley

hadley Jul 14, 2017

Owner

I'd rather just use n_legends() so that the tests are more symmetric.

@Tutuchan

Tutuchan Jul 14, 2017

Contributor

You mean removing the has_legends() test but keeping the n_legends() function the same ?

@hadley

hadley Jul 14, 2017

Owner

Correct

@hadley

Nearly there. Just a few tiny things left.

NEWS.md
@@ -25,6 +25,9 @@
* `geom_smooth`'s message for `method="auto"` now reports the formula used,
in addition to the name of the smoothing function (@davharris #1951).
+
+* The `show.legend` parameter now accepts a named logical vector to hide/show only some aesthetics in the
+legend (@tutuchan)
@hadley

hadley Jul 14, 2017

Owner

Can you please mention the related issue number and wrap the lines?

@Tutuchan

Tutuchan Jul 14, 2017

Contributor

Done.

R/guide-legend.r
- if (is.na(layer$show.legend) || layer$show.legend) {
+
+ # rename color to colour
+ layer$show.legend <- rename_aes(layer$show.legend)
@hadley

hadley Jul 14, 2017

Owner

This can go inside the if block. I don't think it needs a comment

@Tutuchan

Tutuchan Jul 14, 2017

Contributor

Indeed, I moved it.

@hadley

hadley approved these changes Jul 14, 2017

Perfect, thanks!

@karawoo would you mind handling the merge conflict in the NEWS?

@karawoo karawoo Merge branch 'master' into feature/show-legend-vector
022b09e

@karawoo karawoo merged commit 4b235fb into tidyverse:master Jul 14, 2017

4 checks passed

codecov/patch 88.88% of diff hit (target 74.85%)
Details
codecov/project 74.89% (+0.04%) compared to 77fc5c7
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Contributor

Tutuchan commented Jul 14, 2017

Great, thanks for the guidance @hadley !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment