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

drop = FALSE does not work in scale_*_manual #164

Closed
cpauvert opened this issue Oct 19, 2018 · 5 comments
Closed

drop = FALSE does not work in scale_*_manual #164

cpauvert opened this issue Oct 19, 2018 · 5 comments

Comments

@cpauvert
Copy link

Hi,
Thank you very much for this package! However, I was puzzled to see that the option drop = FALSE was not passed along when provided through ... in scale_color_manual for instance.

library(igraph)
gr <- make_graph('bull')
#Adding a categorical value
V(gr)$Cat<-c("A","A","A","B","B")
library(ggraph) # v1.0.2     and ggplot2 v3.0.0
layout <- create_layout(gr, layout = 'igraph', algorithm = 'kk')
ggraph(layout)+geom_node_point(aes(color=Cat))+geom_edge_link()+
  coord_fixed()+theme_void()+
  # Trying to map all possible values
  scale_color_manual(values = c("A"="red","B"="blue","C"="black"),drop=FALSE)

Only the red and blue nodes are shown in the plot, which is expected. But the legend should mention the "C" category and the black label.

Do you have any ideas?
Best,
Charlie

@malcolmbarrett
Copy link
Contributor

malcolmbarrett commented Oct 19, 2018

This is just because Cat only has levels "A" and "B". The same behavior occurs in ggplot.

df <- data.frame(x = 1:3, y  = 1:3, z = factor(c("A", "B", "B")))

ggplot(df, aes(x, y, col = z)) + 
  geom_point() +
  scale_color_manual(values = c("A" = "red", "B" = "blue", "C" = "black"), drop=FALSE)

So you just need to tell it the levels.

layout$Cat <- factor(layout$Cat, levels = c("A", "B", "C"))

ggraph(layout) +
  geom_node_point(aes(color = Cat)) + 
  geom_edge_link() +
  coord_fixed()+
  theme_void()+
  scale_color_manual(values = c("A" = "red", "B" = "blue", "C" = "black"), drop = FALSE)

@cpauvert
Copy link
Author

Hi!
Thank you very much @malcombarrett ! I overlooked factors indeed.
Best,
Charlie

@Artur-man
Copy link

Artur-man commented May 21, 2024

I think this doesnt work anymore because the same script above is giving empty point in the legend

Screenshot 2024-05-21 at 17 08 40

@goesjon
Copy link

goesjon commented May 24, 2024

Same problem here but haven't found a solution yet. If anyone has a solution, please let me know.

@sporella
Copy link

sporella commented May 28, 2024

Its a change included in ggplot2 version 3.5.0 more info here

Following the example here, you need to add show.legend = T

 ggraph(layout) +
  geom_node_point(aes(color = Cat), show.legend = T) + 
  geom_edge_link() +
  coord_fixed()+
  theme_void()+
  scale_color_manual(values = c("A" = "red", "B" = "blue", "C" = "black"), drop = FALSE)

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

5 participants