【发布时间】:2016-04-12 00:36:16
【问题描述】:
有没有办法在 R 中创建连接两点的线? 我知道lines(),函数,但它创建了我正在寻找的线段是一条无限长的线。
【问题讨论】:
-
感谢您的回复,但我不是在寻找 abline。我喜欢在向量空间中进行参数化移动,而不是坐标几何 (y=mx+c) 解决方案。
标签: r graphics visualization lines
有没有办法在 R 中创建连接两点的线? 我知道lines(),函数,但它创建了我正在寻找的线段是一条无限长的线。
【问题讨论】:
标签: r graphics visualization lines
使用segment()函数。
#example
x1 <- stats::runif(5)
x2 <- stats::runif(5)+2
y <- stats::rnorm(10)
plot(c(x1,x2), y)
segments(x1, y[1:5], x2, y[6:10], col= 'blue')
【讨论】:
#define x and y values for the two points
x <- rnorm(2)
y <- rnorm(2)
slope <- diff(y)/diff(x)
intercept <- y[1]-slope*x[1]
plot(x, y)
abline(intercept, slope, col="red")
# repeat the above as many times as you like to satisfy yourself
【讨论】: