2017-02-23 70 views
1

我在設計圖上的圖例顏色時遇到了麻煩,我只是不能在我的圖例上設置適當的顏色,並且所有三個都顯示爲紅色:Multiple Scatter ggplot。我嘗試使用scale_colour_manual選項和scale_fill_manual,但它不起作用。我不知道數據框和gerenarl中的代碼是否合適,但這是我找到將不同迴歸線放在同一個圖上的唯一方法。 這裏是一個MWE:使用ggplot標籤的問題

`library("ggplot2") 
ConcCurve<-c(0.000,5.809,11.514,21.995,32.349,44.390,53.552) 
ABSHei<-c(0.01299076, 0.44779044, 0.87251242, 1.64435113, 2.41385198, 3.25395864,3.93389333) 
ABSAr3<-c(0.0224455, 0.8303167, 1.6170380, 3.0466451, 4.4496162, 5.9631238, 7.1746112) 
ABSAr5<-c(0.03847996, 1.44915907, 2.81864550, 5.29479463, 7.69466231, 10.27269797, 12.32472597) 
DataR<-data.frame(ConcCurve,ABSHei,ABSAr3,ABSAr5) 
p1<-ggplot(DataR) + 
    geom_point(aes(x=ConcCurve,y=ABSHei,fill="Height"),colour="blue") + 
    geom_smooth(aes(ConcCurve,ABSHei), method="lm", se=T,level = 0.9999,lwd=0.6, col ="blue") + 
    geom_point(aes(x=ConcCurve,y=ABSAr3,fill = "Area 3 pix"),colour="green") + 
    geom_smooth(aes(ConcCurve,ABSAr3), method="lm", se=T,level = 0.9999,lwd=0.6, col ="green") + 
    geom_point(aes(x=ConcCurve,y=ABSAr5,fill = "Area5 pix"),colour="red")+ 
    geom_smooth(aes(ConcCurve,ABSAr5), method="lm", se=T,level = 0.9999,lwd=0.6, col ="red") + 
    labs(x = expression(paste(plain("Hg"^plain("2+"))," Concentration (",mu,"g ",plain("kg"^plain("-1")),")")), y = "Integrated absorbance")+ 
    ggtitle("Calibration curves obtained using R")+ 
    guides(fill = guide_legend(reverse=F,title="Evaluation\nmode"))+ 
    scale_colour_manual(labels=c("Heigth", "Area 3 pix", "Area 5 pix"), 
         breaks=c("Heigth", "Area 3 pix", "Area 5 pix"), 
         values=c("blue","green","red")) 
print(p1) 
` 

如何配置的顏色,使他們出現在解釋的方式?

+0

似乎有在你的代碼(例如「Heigth」)的一些錯字,但他們似乎並不成爲問題的唯一原因,如將它們固定不產生你想要的結果。 – wwl

+0

我也試過在這些問題中給出的解決方案,他們沒有工作:http://stackoverflow.com/questions/19330257/changing-the-color-in-the-legend-with-ggplot2-in-r – wwl

回答

2

ggplot語法更適合於長期數據格式 - 如果你重塑數據幀,它應該解決的問題,使代碼更簡單。像這樣:

library("ggplot2") 
ConcCurve<-c(0.000,5.809,11.514,21.995,32.349,44.390,53.552) 
ABSHei<-c(0.01299076, 0.44779044, 0.87251242, 1.64435113, 2.41385198, 3.25395864,3.93389333) 
ABSAr3<-c(0.0224455, 0.8303167, 1.6170380, 3.0466451, 4.4496162, 5.9631238, 7.1746112) 
ABSAr5<-c(0.03847996, 1.44915907, 2.81864550, 5.29479463, 7.69466231, 10.27269797, 12.32472597) 
DataR<-data.frame(ConcCurve, ABS=c(ABSAr5,ABSAr3,ABSHei), 
     mode=rep(c("Area 3 pix", "Area 5 pix", "Height"), each=7)) 

ggplot(DataR, aes(x=ConcCurve, y=ABS, color=mode)) + geom_point() + 
    geom_smooth(method="lm", level=0.9999, lwd=0.6) + 
    labs(x = expression(paste(plain("Hg"^plain("2+"))," Concentration (",mu,"g ",plain("kg"^plain("-1")),")")), 
     y = "Integrated absorbance") + 
    ggtitle("Calibration curves obtained using R") + 
    guides(guide_legend(title="Evaluation\nmode")) 

所有的美學都被轉移到主ggplot調用。然後pointsmooth都使用相同的分組和顏色(即使用mode列),而不寫入兩次。由於我們使用內置色標,因此不需要爲每個組明確指定任何比例或顏色。
另請注意,我在製作數據幀時重新排序了ABS類型。

enter image description here

+0

這是一個非常優雅的解決方案,我也嘗試了數據幀重組,但當時並不奏效。謝謝 – CristhianParedes

1

這裏是一個解決方案,你不使用填充真的讓我擺脫了那些

ggplot(DataR) + 
    geom_point(aes(x=ConcCurve,y=ABSHei, color="blue")) + 
    geom_smooth(aes(ConcCurve,ABSHei), color="blue", method="lm", se=T,level = 0.9999,lwd=0.6) + 
    geom_point(aes(x=ConcCurve,y=ABSAr3, color="green")) + 
    geom_smooth(aes(ConcCurve,ABSAr3), color ="green", method="lm", se=T,level = 0.9999,lwd=0.6) + 
    geom_point(aes(x=ConcCurve,y=ABSAr5, colour="red"))+ 
    geom_smooth(aes(ConcCurve,ABSAr5), color ="red", method="lm", se=T,level = 0.9999,lwd=0.6) + 
    labs(x = expression(paste(plain("Hg"^plain("2+"))," Concentration (",mu,"g ",plain("kg"^plain("-1")),")")), y = "Integrated absorbance")+ 
    ggtitle("Calibration curves obtained using R")+ 
    guides(color = guide_legend(reverse=F,title="Evaluation\nmode"))+ 
    scale_colour_identity(labels=c("Height", "Area 3 pix", "Area 5 pix"), 
         breaks=c("blue", "green", "red"), guide="legend") 

注意。我將顏色移至aes()以獲得圖例。然後,我還使用scale_color_identity來使用文字顏色值,並通過參數labels=在圖例中對其進行重命名。

enter image description here

+0

非常感謝,這是我在stackoverflow中的第一次體驗,並且您的合適答案解決了我的問題,使我學習了一些關於ggplot2的知識,並以非常快的方式擺脫了這對我造成的壓力。再次感謝:D – CristhianParedes

+0

@juod的答案是一個更好的長期解決方案。如果您的數據格式很整齊,ggplot更容易使用。如果可能的話,應該避免像這樣添加多個圖層。 – MrFlick