2012-08-26 62 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的下一個版本將會有更多的圖例控件。