2012-08-26 138 views
1

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

示例数据:

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

+0

本页[(R Cookbook)](http://wiki.stdout.org/rcookbook/Graphs/Legends%20(ggplot2)/)对定制legen的东西进行了大量讨论。结合这篇文章:http://stackoverflow.com/questions/10405823/changing-the-symbol-in-the-legend-key-in-ggplot2应该给你的东西。 –

回答

2

由于泰勒的意见,我找到了解决方案(注意格库需要加载):

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的下一个版本将会有更多的图例控件。