2016-02-29 135 views
2

我試圖在使用ggplot更改圖的圖例中的標籤名稱時出現問題。scale_fill_discrete不會更改標籤名稱

我來讀一些關於Cookbook for R這個主題的教程和關於本論壇同一主題的問題(How do I manually change the key labels in a legend in ggplot2)等等。我寫這個的原因是因爲我無法理解我的ggplot函數使得scale_fill_discrete()在其他示例中不起作用。

我的數據幀是一樣的東西:

  x w t 
1 0.8972208 A 0 
2 0.8312684 A 1 
3 0.7504638 A 2 
4 0.6563472 A 3 
5 0.5764883 A 4 
6 0.5224609 A 5 
7 0.8972208 B 0 
8 0.8456315 B 1 
9 0.7674353 B 2 
10 0.6689723 B 3 
11 0.6114414 B 4 
12 0.5381668 B 5 
13 0.8983253 C 0 
14 0.7878405 C 1 
15 0.6955420 C 2 
16 0.6179749 C 3 
17 0.5524141 C 4 
18 0.4966967 C 5  

據我所知,在傳說中的排序因素是使用ggplot一個非常艱鉅的任務,這就是爲什麼他們在按字母順序排序的原因數據幀(factor w)。然而,真正的名稱應該是不同的東西,如: 「O」, 「R」 和「N

ggplot功能我尤斯是:

ggplot (data = eso3, aes (x=t, y = x, colour = w, linetype = w, shape= w)) 
    +geom_line() 
    +geom_point() 
    +scale_linetype_manual("LEGEND TITLE",values =c("solid","solid","dashed","dashed","dotted","dotted")) 
    +scale_colour_manual("LEGEND TITLE",values=c("black","black","darkgray","darkgray","dimgray","dimgray")) 
    +scale_shape_manual("LEGEND TITLE",values=c(19,15,19,15,19,15)) 
    +scale_y_continuous("Y LABEL",limits=c(0.0,1.0)) 
    +theme(axis.title.y = element_text(angle=0)) 
    +scale_fill_discrete("LEGEND TITLE",breaks=c("A","B","C"),labels=c("O","R","N")) 
    +ggtitle("MAIN TITLE") 
    +theme_bw()  

我知道功能scale_fill_discrete應該改變("A","B","C")("O","R","N")。爲什麼它不工作?

回答

4

由於您使用的colourlinetypeshape但不fill,你必須整合日Ë與scale_xxx_manual()調用新labeles,如:

ggplot (data = eso3, aes (x=t, y = x, colour = w, linetype = w, shape= w))+ 
geom_line()+ 
geom_point()+ 
scale_linetype_manual("LEGEND TITLE",values =c("solid","solid","dashed","dashed","dotted","dotted"),breaks=c("A", "B", "C"), labels=c("O","R","N"))+ 
scale_colour_manual("LEGEND TITLE",values=c("black","black","darkgray","darkgray","dimgray","dimgray"),breaks=c("A", "B", "C"), labels=c("O","R","N"))+ 
scale_shape_manual("LEGEND TITLE",values=c(19,15,19,15,19,15),breaks=c("A", "B", "C"), labels=c("O","R","N"))+ 
scale_y_continuous("Y LABEL",limits=c(0.0,1.0))+ 
theme(axis.title.y = element_text(angle=0))+ 
ggtitle("MAIN TITLE")+ 
theme_bw() 

enter image description here

+0

非常有建設性的回答,非常感謝你!它解決了問題:) – EuGENE