eliocamp / ggnewscale

Multiple Fill, Color and Other Scales in `ggplot2`
https://eliocamp.github.io/ggnewscale/
GNU General Public License v3.0
406 stars 18 forks source link

[BUG] Missed legend: Where is Wally? #29

Closed acpguedes closed 3 years ago

acpguedes commented 3 years ago

Hi,

Thanks for this package which makes it easy to deal with scales. I think it might be a BUG.

I was trying to do a geom_tile with categorical values on the x and y-axis where each value along the x-axis has its color scale.
A heatmap where each column has a color scale and legend

So, I manipulated mtcars just to test code, and BANG, after the last tile I missed the legend. Full example is here

Here is the code I did.

library(tidyverse)
library(ggsci)
library(ggnewscale)

mtcars %>%
  rownames_to_column("rnames") %>% 
  as_tibble() %>% 
  mutate_all(as_factor) %>%
  select(rnames, vs, am, gear, carb) %>% 
  gather(key = "key", value = "value", -rnames) -> temp
#> Warning: attributes are not identical across measure variables;
#> they will be dropped

ggplot() + 
  geom_tile(
    data = temp %>% filter(key=="vs") %>% droplevels, 
    aes(key, rnames, fill=value)
  ) + 
  scale_fill_simpsons() + 
  new_scale_fill() + 
  geom_tile(
    data = temp %>% filter(key=="am") %>% droplevels, 
    aes(key, rnames, fill=value)
  ) + 
  scale_fill_rickandmorty() +
  new_scale_fill() + 
  geom_tile(
    data = temp %>% filter(key=="gear") %>% droplevels, 
    aes(key, rnames, fill=value)
  ) + 
  scale_fill_futurama() +
  new_scale_fill() + 
  geom_tile(
    data = temp %>% filter(key=="carb") %>% droplevels, 
    aes(key, rnames, fill=value)
  ) + 
  scale_fill_tron() +
  theme(legend.position="bottom")

Created on 2020-11-30 by the reprex package (v0.3.0)

Thanks in advance.

eliocamp commented 3 years ago

Thanks! I'll look into it!

eliocamp commented 3 years ago

I can't reproduce on the CRAN version of ggnewscale, but can on the dev version. For now, try installing ggnewcale with install.packages("ggnewscale").

eliocamp commented 3 years ago

Investigating further, The issue with the disappearing guides is that ggplot2 is merging them because they have the same name and values (see: https://github.com/tidyverse/ggplot2/issues/4280).

There's nothing I can do from ggnewcale side of things. What you can do is to change the name argument of each scale (which you should do anyway for clarity!)

library(tidyverse)
library(ggsci)
library(ggnewscale)

data(mtcars)
mtcars %>%
  rownames_to_column("rnames") %>% 
  as_tibble() %>% 
  mutate_all(as_factor) %>%
  select(rnames, vs, am, gear, carb) %>% 
  gather(key = "key", value = "value", -rnames) -> temp
#> Warning: attributes are not identical across measure variables;
#> they will be dropped

ggplot() + 
  geom_tile(
    data = temp %>% filter(key=="vs") %>% droplevels, 
    aes(key, rnames, fill=value)
  ) + 
  scale_fill_simpsons(name = "simpsns") + 
  new_scale_fill() + 
  geom_tile(
    data = temp %>% filter(key=="am") %>% droplevels, 
    aes(key, rnames, fill=value)
  ) + 
  scale_fill_rickandmorty(name ="rick") +
  new_scale_fill() + 
  geom_tile(
    data = temp %>% filter(key=="gear") %>% droplevels, 
    aes(key, rnames, fill=value)
  ) + 
  scale_fill_futurama(name ="futurama") +
  new_scale_fill() + 
  geom_tile(
    data = temp %>% filter(key=="carb") %>% droplevels, 
    aes(key, rnames, fill=value)
  ) + 
  scale_fill_tron(name ="tron") +
  theme(legend.position="bottom")

Created on 2020-12-01 by the reprex package (v0.3.0)

eliocamp commented 3 years ago

I'm closing this for now, since it's something on ggplot2 side of things.

acpguedes commented 3 years ago

Thank you, this solution you give me is enough