Skip to content

Mark labels from default mappings and overwrite them if possible #4414

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

* Make sure that default labels from default mappings doesn't overwrite default
labels from explicit mappings (@thomasp85, #2406)

* Fix a bug in `position_dodge2()` where `NA` values in thee data would cause an
error (@thomasp85, #2905)

Expand Down
12 changes: 10 additions & 2 deletions R/plot-construction.r
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,16 @@ ggplot_add.Layer <- function(object, plot, object_name) {

# Add any new labels
mapping <- make_labels(object$mapping)
default <- make_labels(object$stat$default_aes)
default <- lapply(make_labels(object$stat$default_aes), function(l) {
attr(l, "fallback") <- TRUE
l
})
new_labels <- defaults(mapping, default)
plot$labels <- defaults(plot$labels, new_labels)
current_labels <- plot$labels
current_fallbacks <- vapply(current_labels, function(l) isTRUE(attr(l, "fallback")), logical(1))
plot$labels <- defaults(current_labels[!current_fallbacks], new_labels)
if (any(current_fallbacks)) {
plot$labels <- defaults(plot$labels, current_labels)
}
plot
}