Skip to content

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

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 11 commits into from
Dec 2, 2024
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)
}
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.

Copy link
Member

Choose a reason for hiding this comment

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

I think that it logically belongs in get_labels() as it ensures the output of that function is usable. It shouldn't be the responsibility of the caller to further process the return value to make it work

key$.label <- labels

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