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

values vector in scale_color_manual #4534

Closed
numpde opened this issue Jun 28, 2021 · 5 comments
Closed

values vector in scale_color_manual #4534

numpde opened this issue Jun 28, 2021 · 5 comments

Comments

@numpde
Copy link

numpde commented Jun 28, 2021

The behavior of scale_color_manual(values = ...) seems to have changed recently and broke many of my plots.

Consider this example:

suppressPackageStartupMessages({
  library(ggplot2)
  library(dplyr)
})

color_map <- list(r = "red", g = "green", b = "blue")

randu %>%
  mutate(color_key = as.factor(if_else(x > y, "r", "g"))) %>%
  ggplot(aes(x = x, y = y, fill = color_key)) + 
  scale_fill_manual(values = color_map) +
  geom_point(shape = 21, size = 7)

It now produces a figure with the blue color also in the legend.
I find that the following works (blue does not appear in the legend):

randu %>%
  mutate(color_key = as.factor(if_else(x > y, "r", "g"))) %>%
  {
    ggplot(., aes(x = x, y = y, fill = color_key)) + 
      scale_fill_manual(values = color_map, breaks = .$col) +
      # scale_fill_manual(values = color_map[.$col]) +  # Alternative
      geom_point(shape = 21, size = 7)
  }

But this code looks unhappy. Is there a more robust way, in particular without extra temporary variables?

Is there somethin like breaks = .df$fill? This would be useful in many contexts.

Sidenote. This would make more sense to me:

scale_fill_manual(values = color_map)  # no blue in the legend
scale_fill_manual(values = color_map, breaks = names(color_map))  # with blue in the legend

==

For lols, this works, too:

gglast <- function(what) {
  p <- ggplot2::last_plot()
  q <- p$mapping[[deparse(substitute(what))]]
  rlang::eval_tidy(q, data = p$data)
}

randu %>%
  mutate(color_key = as.factor(if_else(x > y, "r", "g"))) %>%
  ggplot(aes(x = x, y = y, fill = color_key)) + 
  scale_fill_manual(values = color_map, breaks = gglast(fill)) +
  geom_point(shape = 21, size = 7)
@teunbrand
Copy link
Collaborator

Issue #4511 also touches the extra colour key with predefined values.

@clauswilke
Copy link
Member

Could you update to the latest ggplot2 on CRAN (3.3.5) and let us know if you still have this problem?

@numpde
Copy link
Author

numpde commented Jun 28, 2021

@clauswilke sessionInfo() shows ggplot2_3.3.5 in R 4.1.

Adding limits = force as proposed by @teunbrand in the linked issue also works (removes unused legend items).

banfai added a commit to banfai/ggplot2 that referenced this issue Jul 7, 2021
* issue introduced in tidyverse#4471
* potential solution for tidyverse#4534 and tidyverse#4511
* add extra tests
banfai added a commit to banfai/ggplot2 that referenced this issue Jul 7, 2021
* issue introduced in tidyverse#4471
* potential solution for tidyverse#4534 and tidyverse#4511
* add extra tests
@teunbrand
Copy link
Collaborator

teunbrand commented Sep 15, 2021

It also has come to my attention that trying to do the opposite -displaying fewer legend keys- is also working in weird ways. In the code below, I would have expected both plots to do the same thing. Specifically, I would have expected the first plot to not have any grey points since the limits and values match up.

library(ggplot2)

p <- ggplot(mpg, aes(displ, hwy, colour = drv)) +
  geom_point(size = 2)

p +
  scale_colour_manual(
    values = c("dodgerblue", "tomato", "limegreen"),
    limits = c("4", "f", "r"),
    breaks = c("4", "r")
  )

p +
  scale_colour_manual(
    values = c("dodgerblue", "tomato", "limegreen"),
    limits = c("4", "f", "r"),
    breaks = function(x) c("4", "r")
  )

Created on 2021-09-15 by the reprex package (v2.0.1)

@yutannihilation
Copy link
Member

Fixed by #4619

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants