Skip to content

Default label is ignored when a mapping is overwritten by a parameter #6003

Closed
@yutannihilation

Description

@yutannihilation

Related to #5879

In the code below, the legend title is chosen differently depending on whether the same aes variable is specified by a parameter outside of aes(). In the second one, I think the title should be either

  • <<< G >>>, because it's specified by the label attribute
  • G, because the second layer is what the color scale is actually for
library(ggplot2)

d <- data.frame(x = 1, y = 2, g = 3)
d2 <- d
colnames(d2) <- c("X", "Y", "G")

attr(d[[1L]], "label") <- "<<< X >>>"
attr(d[[2L]], "label") <- "<<< Y >>>"
attr(d[[3L]], "label") <- "<<< G >>>"

ggplot() +
  geom_point(data = d, aes(x, y, colour = g)) +
  geom_point(data = d2, aes(X, Y, colour = G))

ggplot() +
  geom_point(data = d, aes(x, y, colour = g), colour = "grey") + 
  #                                           ^^^^^^^^^^^^^^^
  #                                             the difference
  geom_point(data = d2, aes(X, Y, colour = G))

Created on 2024-07-17 with reprex v2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions