Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 change legend title in this particular example [duplicate]

I have tried many suggestions but none fit this particular simple example. How would I change the legend title to Sample?

df1 <- data.frame(
  Order = c(1,1,2,2),
  Tissue = c("Bladder","Pancreas","Bladder","Pancreas"),
  Sample = c("WT","WT","Mutant","Mutant"),
  Percent = c(94.12,55,5.88,45)
)

ggplot(data = df1, aes(x = Tissue, y = Percent, group = Sample)) + 
    geom_point(aes(shape=reorder(Sample,Order), fill=reorder(Sample,Order)), size = 6) +
    scale_shape_manual(values=c(21,22)) +
    scale_fill_manual(values=c("deepskyblue1","yellow")) 

Thank you.

like image 846
jmu Avatar asked Feb 17 '23 02:02

jmu


1 Answers

the scale_XX accept a name argument that you can adjust:

scale_shape_manual(name = "Sample", values=c(21,22)) +
scale_fill_manual(name = "Sample", values=c("deepskyblue1","yellow")) 

If you don't pass it into both scales, it creates two separate legends by default apparently.

You could alternatively do the reoder() bit before passing into your plotting code.

See here for details.

like image 119
Chase Avatar answered Feb 19 '23 23:02

Chase