ggplot2在此特定示例中更改了图例标题

时间:2013-03-19 20:38:11

标签: r ggplot2 title legend

我尝试了很多建议,但没有一个适合这个特别简单的例子。如何将图例标题更改为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")) 

谢谢。

2 个答案:

答案 0 :(得分:4)

scale_XX接受您可以调整的name参数:

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

如果你没有将它传递到两个音阶,它默认会默认创建两个单独的图例。

您也可以在传入绘图代码之前执行reoder()位。

有关详细信息,请参阅here

答案 1 :(得分:4)

另一种选择是使用guides

bp + guides(fill=guide_legend("Sample"),shape=guide_legend("Sample"))

enter image description here