如何使用ggplot添加多行,给定两个系数向量而不使用for循环?

Jac*_*ack 1 visualization r ggplot2

使用for循环显着慢,只是看起来不对.如果有人可以使用geom_abline(拦截,斜率)的其他方法,我将不胜感激.变量Coeff是一个包含所有参数的四个数据帧的列表,每个数据帧有1001行(第一个是无用的).

p <- qplot(x,y,data = data,color = I("blue"))
for (i in 1:1000){
p <- p + geom_abline(intercept = Coeff[[1]]$Intercept[i+1], slope = Coeff[[1]]$X[i+1],alpha = 0.1,size = 0.1, colour = "red")
}
for (i in 1:1000){
p <- p + geom_abline(intercept = Coeff[[3]]$Intercept[i+1], slope = Coeff[[3]]$X[i+1],alpha = 0.1,size = 0.1, colour = "yellow")
}
Run Code Online (Sandbox Code Playgroud)

Jus*_*tin 7

你可以将向量传递给slopeintercept

g <- ggplot(data.frame(x=-10:10, y=-10:10), aes(x, y))+geom_point()
my_coefs <- data.frame(slope=-5:5, intercept=-5:5)

g + geom_abline(data=my_coefs, aes(slope=slope, intercept=intercept))
Run Code Online (Sandbox Code Playgroud)