Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

r How to remove some parameters from the legend in ggplot2

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading