Skip to content
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

Fix expression labels in guide_coloursteps() and guide_bins() #6007

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions R/guide-bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ GuideBins <- ggproto(
key$.show <- NA

labels <- scale$get_labels(breaks)
if (is.character(scale$labels) || is.numeric(scale$labels)) {
if (is.character(scale$labels) || is.numeric(scale$labels) || is.expression(scale$labels)) {
limit_lab <- c(NA, NA)
} else {
limit_lab <- scale$get_labels(limits)
Expand All @@ -169,6 +169,9 @@ GuideBins <- ggproto(
} else {
key$.show[nrow(key)] <- TRUE
}
if (is.expression(labels)) {
labels <- as.list(labels)
}

key$.label <- labels
key <- vec_slice(key, !is.na(oob_censor_any(key$.value)))
Expand Down Expand Up @@ -258,7 +261,7 @@ GuideBins <- ggproto(

list(labels = flip_element_grob(
elements$text,
label = key$.label,
label = validate_labels(key$.label),
x = unit(key$.value, "npc"),
margin_x = FALSE,
margin_y = TRUE,
Expand Down
6 changes: 5 additions & 1 deletion R/guide-colorsteps.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ GuideColoursteps <- ggproto(
} else {
key$.value <- breaks
}
key$.label <- scale$get_labels(breaks)
labels <- scale$get_labels(breaks)
if (is.expression(labels)) {
labels <- as.list(labels)
}
Comment on lines +118 to +120
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if it wouldn't be better if this was the responsibility of Scale$get_labels().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... Do we have this call in other places so that moving it to Scale$get_labels() will allow us to remove some code elsewhere?

Copy link
Collaborator Author

@teunbrand teunbrand Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some places for sure. The issue is that, at the moment, it is a balanced trade-off. We have 3 guides where this guard is used, but we also have 3 Scale$get_labels() methods that might generate expressions.

key$.label <- labels

if (breaks[1] %in% limits) {
key$.value <- key$.value - 1L
Expand Down
Loading