Home > Blockchain >  How to add a second legend for stat_summary point below prop legend in geom_count ggplot?
How to add a second legend for stat_summary point below prop legend in geom_count ggplot?

Time:03-18

I have a geom_count plot and add the mean values of the Y-axis (importance) as points using stat_summary. I would like the shape of the point (red rectangle in my case) to be part of the legend so that it is clear that these are the means. How can I get the means symbol to show up in the legend below the prop legend?

#example data:
df <- data.frame(x = c("A", "B", "C", "D", "E"),
                   y = c(1, 2, 3, 4, 5, 5, 4, 3, 2, 1))

#plot
ggplot(df, aes(x = x, y =  y))  
 geom_count(aes(size= after_stat(prop), group=x))   
 theme_bw()  
 scale_size_area(max_size = 8)  
 stat_summary(
   geom = "point",
   fun = "mean",
   col = "black",
   size = 6,
   shape = 23,
   fill = "red",
   show.legend = F)  
 xlab("")  
 ylab("Importance")   
 scale_y_continuous(labels=c("1" = "Not at all", "2" = "Little ", "3" = "Moderate", "4" = "Quite"  , "5" =  "Very"))  
 theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust=1),
       axis.text.y = element_text(angle =30))  

Click here for plot

Changing to show.legend = T does not solve the problem

I've tried the solution suggested in enter image description here

  • Related