Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims to fix #3648 and supersedes #5302. It is incompatible with #3649.
Briefly, in this PR, legend guides check whether a value is present in layer data and if not, omits that key for that layer.
Less briefly, the default
layer(show.key = NA)
used to mean 'if layer has mapping with aesthetic, include it in legend, otherwise don't'. That remains true, butshow.key = NA
now also checks on a per-value basis whether that value is present in the layer data. To include or omit all values, regardless of whether they are present, one can uselayer(show.key = TRUE/FALSE)
, like before. This means that the previous behaviour can easily be reinstated by users. Note that this check only applies to discrete values, continuous values are always included (if the layer has the relevant mapping).Compared to #5302, this PR makes use of the plumbing introduced in #5483, making this a much less invasive PR.
There is one visual test that changed for the better.
A demonstration that the layers now only draw the relevant keys:
As
show.legend
can be a named logical vector, this can still be used to finetune inclusion or omission of keys. Below, we're omitting points for the alpha legend, but include all line keys for the alpha legend. This makes no sense for data visualisation, but it shows that the behaviour makes sense.Created on 2023-10-30 with reprex v2.0.2