-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Smarter keys #5302
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
Smarter keys #5302
Conversation
@@ -89,6 +89,7 @@ ggplot_build.ggplot <- function(plot) { | |||
if (npscales$n() > 0) { | |||
lapply(data, npscales$train_df) | |||
data <- lapply(data, npscales$map_df) | |||
plot$key_data <- npscales$key_data(data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking the data has to happen before the defaults are filled in and after_scale()
modifications are applied. Otherwise the information might be lost. I gave the scales list a method to check the data. The downside is that we have to check all the data regardless of whether the scale has a legend guide, as that isn't resolved at this point.
} | ||
present | ||
}, logical(length(pal))) | ||
list(aesthetics = aes, data = data_frame0(pal = pal, member = out)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point, member
is a length(pal)
* length(data)
logical matrix tracking whether a palette value was seen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also aes
can be a vector, e.g. c("colour", "fill")
. The inclusion/exclusion rule in the guide only needs to match one of the aesthetics, not all, to be included.
Closing this PR in favour of #5502 |
This PR aims to fix #3648. This PR is mutually incompatible with #3649.
Briefly, it automatically checks whether the key value is actually in the data and doesn't draw keys when it isn't. This contrasts with the approach taken in #3649, where the user is given more control.
Less briefly: it performs this check when
show.legend = NA
, which is the default. Whenshow.legend = TRUE
, is draws the keys regardless of presence in the layer data. To get back the old behaviour and you have multiple legends, you'd have to be specific to include a layer in the keys of a particular legend, for exampleshow.legend = c(colour = TRUE)
.A small demo from #3648 (comment). Note how there is no point displayed for the 'regression line' key, and no line for the 'points' key.
Created on 2023-05-09 with reprex v2.0.2