2014-01-23 40 views
1

我一直在爭取一段時間,雖然它看起來應該很簡單。我一直在這個問題上以下建議:使用ggplot2添加「LD50」線來邏輯迴歸圖

Predicted values for logistic regression from glm and stat_smooth in ggplot2 are different

我有什麼是Logistic迴歸曲線,我想補充一個「LD50」行,如在密度,其中試驗的0.5是成功(Y = 1)。我已經使用dose.p函數計算了密度值,並正在使用這些值來指導我的geom_line。但由於某種原因,線條不會走到我希望他們去的地方,我無法弄清楚爲什麼!我想讓它們以90度的角度運行,就像上面的例子一樣,但由於某種原因,我得到了一條對角線,我真的不知道爲什麼!

Treatment=c(rep("Live", 50), rep("Degraded", 50)) 
Density=c(rep(141.9015767,10), rep(165.4903068, 10), rep(186.4801865,10), rep(325.9259259,10), rep(662.8471167, 10), rep(141.9015767,10), rep(165.4903068, 10), rep(186.4801865,10), rep(325.9259259,10), rep(662.8471167, 10)) 
Code=c(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1,1,1,1,1,1) 
Choice=data.frame(Treatment, Density, Code) 

##plot 
Choice_plot=ggplot(data=Choice, aes(x=Density, y=Code, linetype=Treatment)) + geom_point(size=2.5, colour="black", alpha=1/6)+ 
#LD50 line Live 
geom_segment(aes(y = 0.5, yend = 0.5, xend = 316.8520),x = -Inf,linetype = "dashed") + 
geom_segment(aes(x=316.8520, xend=316.8520, y=0, yend=0.5),x = -Inf,linetype = "dotted") +  

#LD50 line Dead 
geom_segment(aes(y = 0.5, yend = 0.5, xend = 411.8904),x = -Inf,linetype = "dashed") + 
geom_segment(aes(x=411.8904, xend=411.8904, y=0, yend=0.5),x = -Inf,linetype = "dotted") + 
stat_smooth(method="glm", family="binomial", se=FALSE, colour="black", size=0.3)+ 
labs(y="Colony Choice", size=1.5)+ 
labs(x="P. mol density on established colony (ind/m2)", size=1.5)+ 

## Make it pretty 
theme_bw()+ 
theme(panel.background = element_rect(colour = NA))+ 
theme(panel.grid.minor = element_line(colour = NA))+ 
theme(panel.grid.major = element_line(colour = NA)) 
Choice_plot 

水平線工作正常,但假想垂直線結束了從y中對角線運行中,x = 0到y = 0.5,X = 316.8520或411.8904代替垂直!我還無法發佈圖片,我是新手,對不起!

對於記錄,最初我開始使用一個類似geom_segment代碼兩者的水平和垂直段:

geom_segment(aes(y = 0.5, yend = 0.5, xend = 411.8904),x = -Inf,linetype = "dashed") + 
    geom_segment(aes(x=411.8904, xend=411.8904, yend=0.5),x = -Inf,linetype = "dotted") 

在這種情況下我結束了從水平行的末尾運行兩個對角線到y = 1 & 0,x = 0

有沒有人有任何想法我要去哪裏錯了?

+1

在你的第二個'geom_segment()'已映射'x'兩次('X = 411.8904'和'X = -Inf'),刪除第二個,你應該是正確的 - 投票關閉爲這似乎是一個不太可能幫助他人的簡單錯誤。 – alexwhan

回答

1

將虛線x = -Inf更改爲y = -Inf。 請注意,我也將所有x和y移動到aes()。

ggplot(data=Choice, aes(x=Density, y=Code, linetype=Treatment)) + geom_point(size=2.5, colour="black", alpha=1/6)+ 
     #LD50 line Live 
     geom_segment(aes(y = 0.5, yend = 0.5, xend = 316.8520, x = -Inf),linetype = "dashed") + 
     geom_segment(aes(x=316.8520, xend=316.8520, yend=0.5,y = -Inf),linetype = "dotted") +  

     #LD50 line Dead 
     geom_segment(aes(y = 0.5, yend = 0.5, xend = 411.8904,x = -Inf),linetype = "dashed") + 
     geom_segment(aes(x=411.8904, xend=411.8904, y=-Inf, yend=0.5),linetype = "dotted") + 
     stat_smooth(method="glm", family="binomial", se=FALSE, colour="black", size=0.3)+ 
     labs(y="Colony Choice", size=1.5)+ 
     labs(x="P. mol density on established colony (ind/m2)", size=1.5)+ 

     ## Make it pretty 
     theme_bw()+ 
     theme(panel.background = element_rect(colour = NA))+ 
     theme(panel.grid.minor = element_line(colour = NA))+ 

    theme(panel.grid.major = element_line(colour = NA))