修改文本图例ggplot2

时间:2012-08-26 17:17:40

标签: r ggplot2 legend

我想使用文本geom修改ggplot2中的图例。示例如下所示。我想将a改为点(圆圈),而不是1,2,3使用自定义名称,如低,中,高。任何建议都将不胜感激。

示例数据:

   x  y Freq colors
1 -2 32    2      1
2 -2 36    1      1
3 -2 37    1      1
4 -2 40    2      1
5 -1 32    2      1
6  0 29    2      1

代码:

  fit=ggplot(a1,aes(x,y,color=factor(colors)),col=colors)+
    geom_text(aes(label=Freq),size=5)+
    theme_bw()+
    opts(legend.position='top',
         legend.title=theme_blank(),
         legend.key=theme_rect(fill="white",colour="white"))
  print(fit)

enter image description here

1 个答案:

答案 0 :(得分:2)

感谢Tyler的评论,我找到了一个解决方案(注意需要加载网格库):

  fit=ggplot(a1,aes(x,y,color=factor(colors)),col=colors)+
    geom_text(aes(label=Freq),size=5)+
    theme_bw()+
    scale_color_hue(breaks=c("1", "2", "3"),
                      labels=c("Low", "Medium", "High"))+
    opts(legend.position='top',
         legend.title=theme_blank(),
         legend.key=theme_rect(fill="white",colour="white"))
  print(fit)
  grid.gedit("^key-[-0-9]+$", label = "*")

我希望我可以在图例中使“*”更大,我知道ggplot2的下一个版本会有更多的图例控件。