Skip to content

allow fewer elements in named values vector in manual scales #4471

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 9 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ggplot2 (development version)

* Manual scales now allow named vectors passed to `values` to contain fewer
elements than existing in the data. Elements not present in values will be set
to `NA` (@thomasp85, #3451)

* Add support for the BrailleR package for creating descriptions of the plot
when rendered (@thomasp85, #4459)

Expand Down
8 changes: 6 additions & 2 deletions R/scale-manual.r
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ scale_discrete_manual <- function(aesthetics, ..., values, breaks = waiver()) {
}


manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ...) {
manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), limits = NULL, ...) {
# check for missing `values` parameter, in lieu of providing
# a default to all the different scale_*_manual() functions
if (is_missing(values)) {
Expand All @@ -130,6 +130,10 @@ manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ...) {
force(values)
}

if (is.null(limits)) {
limits <- names(values)
}

# order values according to breaks
if (is.vector(values) && is.null(names(values)) && !is.waive(breaks) &&
!is.null(breaks) && !is.function(breaks)) {
Expand All @@ -146,5 +150,5 @@ manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ...) {
}
values
}
discrete_scale(aesthetic, "manual", pal, breaks = breaks, ...)
discrete_scale(aesthetic, "manual", pal, breaks = breaks, limits = limits, ...)
}