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

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

Closed
acpguedes opened this issue Nov 30, 2020 · 5 comments
Closed

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

acpguedes opened this issue Nov 30, 2020 · 5 comments

Comments

@acpguedes
Copy link

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
Copy link
Owner

eliocamp commented Dec 1, 2020

Thanks! I'll look into it!

@eliocamp
Copy link
Owner

eliocamp commented Dec 1, 2020

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
Copy link
Owner

eliocamp commented Dec 1, 2020

Investigating further, The issue with the disappearing guides is that ggplot2 is merging them because they have the same name and values (see: tidyverse/ggplot2#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
Copy link
Owner

eliocamp commented Dec 2, 2020

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

@eliocamp eliocamp closed this as completed Dec 2, 2020
@acpguedes
Copy link
Author

Thank you, this solution you give me is enough

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

2 participants