-
Notifications
You must be signed in to change notification settings - Fork 2k
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
add function scale_label_identity
function
#5389
Conversation
@@ -236,7 +236,6 @@ draw_key_smooth <- function(data, params, size) { | |||
#' @export | |||
#' @rdname draw_key | |||
draw_key_text <- function(data, params, size) { | |||
if(is.null(data$label)) data$label <- "a" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little bit worried with this line.
I fear that the following code wouldn't draw labels in the legend:
ggplot(mtcars, aes(mpg, disp)) +
geom_text(aes(
label = rownames(mtcars),
colour = factor(cyl)
))
Could you confirm and amend if necessary?
Overall I'm a little bit cautious about the need for such function, which might have been fruitful to discuss in an issue before posting a PR. Labels typically aren't mapped to a scale and introducing |
Thanks for your comment, I wanted bring it into ggplot2 package for these issues: #2004, https://stackoverflow.com/questions/49965758/change-geom-texts-default-a-legend-to-label-string-itself?noredirect=1&lq=1, https://stackoverflow.com/questions/18337653/remove-a-from-legend-when-using-aesthetics-and-geom-text?noredirect=1&lq=1, https://stackoverflow.com/questions/59091627/add-new-legend-for-geom-text-with-text-labels-as-legend-key, without scale_label function, the data in |
I have noticed that the existence of the |
Perhaps it might be advisable not to alter the |
It has come to my attention that the current Line 10 in c76b9ae
|
I'll admit that the current way of dealing with this is not ideal. I'd say the recommended route without this PR would be to use the library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.3.1
data <- data.frame(
x = c(1, 2, 3, 4, 5),
y = c(1, 2, 3, 4, 5),
label = c("A", "B", "C", "D", "E")
)
ggplot(data, aes(x, y)) +
geom_text(aes(label = label, color = label)) +
guides(colour = guide_legend(override.aes = list(label = LETTERS[1:5]))) You could still have an identity scale, but you'd need to set the guide for it to work as expected. scale_label_identity <- function(..., guide = "none") {
sc <- discrete_scale("label", "identity", force, ...,
guide = guide,
super = ScaleDiscreteIdentity
)
sc
}
ggplot(data, aes(x, y)) +
geom_text(aes(label = label, color = label)) +
scale_label_identity(guide = "legend") Created on 2023-08-17 with reprex v2.0.2
There is a subtle nuance in what is 'mapped' and what is 'scaled'. The |
Thank you for your explanations. I now realize the differences between "map" and "scale". Given this understanding, it might not be advisable to include such a function in this context. For me, It'll be okay to close the pull request. |
As it seems closing this pull request is the conclusion here, I'm closing now. |
Through these commits, we are enabled to effortlessly exhibit legend labeling using the
guide
parameter within thescale_label_identity
function, aligning it with analogous aesthetic settings for other geoms. Furthermore, the assignment of the label key can be readily accomplished through the intrinsic data within thedraw_key_
function (#2004). This bringscale_label_identity
function and catch following concers:draw_key_text
function. As we can see with following codeshow.legend
shoule have include the mapping aesthetic:draw_key_text
function appears superfluous.