Legend help

48 views
Skip to first unread message

Casault, Benoit

unread,
Sep 16, 2022, 1:42:34 PM9/16/22
to ggp...@googlegroups.com

Can someone explain why the script below ends up overlaying black dots on top of the bubbles in the legend “leg2” and adds small grey dots to the markers in “leg1”. I am trying to get “leg2” as bubbles only and “leg1” as markers only. I am missing on something here but just can’t figure it out.

 

 

df <- data.frame(x=seq(1,10), y=seq(1,10), z=c(1,0,5,3,1,8,0,12,1,0)) %>%

  mutate(z_fct=factor(z, levels=c(0,1,NA)))

 

p <- ggplot(df, aes(x=x, y=y)) +

  geom_point(aes(size=z),

             colour="grey50",

             alpha=0.75,

             show.legend=T) +

  geom_point(aes(colour=z_fct, shape=z_fct),

             size=5,

             show.legend=T) +

  scale_colour_manual(name="leg1", limits=factor(c(0,1), levels=c(0,1)), labels=c("N=0", "N<=1"), values=c("red", "black")) +

  scale_shape_manual(name="leg1", limits=factor(c(0,1), levels=c(0,1)), labels=c("N=0", "N<=1"), values=c(4,3)) +

  scale_size_area(name="leg2", limits=c(0, 15), breaks=c(3,6,9,12,15), max_size=10)

 

Hani Alsafadi

unread,
Oct 5, 2022, 8:19:39 AM10/5/22
to ggplot2
Hello, 

You just need to remove "show.legend=T" from both of the geom_points. This will fix it as you like, but the leg2 legend will be first, to fix that if you want leg1 to be first add:

+  guides(color=guide_legend(order = 1), shape=guide_legend(order = 1))


Cheers, 
H
Reply all
Reply to author
Forward
0 new messages