Home > Mobile >  r How to remove some parameters from the legend in ggplot2
r How to remove some parameters from the legend in ggplot2

Time:09-30

Sorry, couldn't find the answer among the existing threads, so creating the new question for that.

I'm wondering how to remove from the legend some particular parameter used for grouping (but without removing the legend completely) in ggplot2.

Let's say, I'm creating this graph:

ggplot(mtcars, aes(x= factor(cyl), y= hp,
                   fill= factor(am), shape = factor(carb)))   
  theme_bw() 
  scale_shape_manual(values= c(20:25)) 
  geom_boxplot(alpha = 0.5) 
  geom_point(position=position_dodge(0.76), size = 4)

Resulting graph

And I want to show the legend only for factor(am), but to remove factor(carb). How would I do that ?

Thanks a lot for the help!

CodePudding user response:

You can specify guides(shape = guide_none())

ggplot(mtcars, aes(x= factor(cyl), y= hp,
                   fill= factor(am), shape = factor(carb)))   
  theme_bw() 
  scale_shape_manual(values= c(20:25)) 
  geom_boxplot(alpha = 0.5) 
  geom_point(position=position_dodge(0.76), size = 4)  
  guides(shape = guide_none())

enter image description here

  • Related