2017-10-15 59 views
-2

我編寫這樣的:添加回歸線傳奇geom_abline

ggplot() + 
    geom_point(mapping = aes(x = X, y = y)) + 
    geom_abline(intercept = -0.9930872, slope = 0.4866284, colour = "red") + 
    geom_abline(intercept = -1, slope = 0.5, colour = "blue") 

,但似乎無法得到一個工作傳說爲我的最小二乘populuation迴歸線。我試過各種堆棧溢出的答案,但似乎沒有給我我需要的東西。

Add a legend to a ggplot2 scatter plot including additional lines

這看起來像最好的答案,但我不能得到它的工作!

有什麼建議嗎?

回答

0

經過稍微修改你的代碼工作得很好:萬一,如果你想改變的是藏漢

ggplot() + 
geom_point(mapping = aes(x = X, y = y)) + 
    geom_abline(aes(colour = "line_1", intercept = -0.9930872, slope = 0.4866284)) + 
    geom_abline(aes(colour = "line_2", intercept = -1, slope = 0.5)) + 
    scale_colour_manual(name = "lines", values = c("red", "blue")) + 
    theme(legend.position = "bottom") 

增加了傳奇地位。

2
set.seed(1234) 
X <- rnorm(20,sd=2.5) 
y <- -1+0.5*X+rnorm(20, sd=0.4) 

library(ggplot2) 
ggplot() + 
geom_point(mapping = aes(x = X, y = y)) + 
geom_abline(aes(intercept = -0.9930872, slope = 0.4866284, colour = "line1"), lwd=1) + 
geom_abline(aes(intercept = -1, slope = 0.5, colour = "line2"), lwd=1) + 
scale_colour_manual(values=c("line1"="red","line2"="blue")) 

enter image description here

+0

這看起來很完美!我明天會試試...謝謝! – PS94