2017-03-02 66 views
-2

我想繪製負二項分佈和泊松分佈以適合我的實際數據,但我不知道如何繪製圖例,誰可以幫助我那非常感謝。我的代碼和圖片如下:如何在R中繪製分佈模型時添加圖例ggplot2

ggplot() + 
    geom_density(aes(a),color="red",lwd=2) + 
    geom_density(aes(x=rpois(50,1.57)),color="purple",lwd=2) + 
    geom_smooth() + 
    geom_density(aes(x=rnbinom(100,size=0.2,mu=1.57)),color="blue",lwd=2) + 
    geom_smooth() + 
    coord_cartesian(xlim=c(0,10)) + labs(x="count") 

enter image description here

我的數據在這裏上傳: https://www.jianguoyun.com/p/DSHXKgMQm5CLBhiKjCc

回答

1

添加圖例的最簡單方法是將變量映射爲顏色。例如

ggplot() + 
    geom_density(aes(a, color="data"),lwd=2) + 
    geom_density(aes(x=rpois(50,1.57), color="poisson"),,lwd=2) + 
    geom_smooth() + 
    geom_density(aes(x=rnbinom(100,size=0.2,mu=1.57),color="binomial"),lwd=2) + 
    geom_smooth() + 
    coord_cartesian(xlim=c(0,10)) + labs(x="count") 
+0

謝謝你的幫助。 「二項式」之後的括號可以在參數「顏色」之前加上。 – user4672728

+0

@ user4672728我修復了錯字 – MrFlick

相關問題