连接 R 中绘图函数中的点的线 [英] Line connecting the points in the plot function in R
本文介绍了连接 R 中绘图函数中的点的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 R 编程语言的绘图函数中有一个简单的问题.我想在点之间画一条线(虽然第二不是我想要的,但我想要一个连续的情节:
解决方案
您必须对 x 值进行排序:
plot(x, f_x, xlim=range(x), ylim=range(f_x), xlab="x", ylab="y",main = "无噪声数据",pch=16)行(x[order(x)],f_x[order(x)],xlim=range(x),ylim=range(f_x),pch=16)
I have a simple problem in the plot function of R programming language. I want to draw a line between the points (see this link and how to plot in R), however, what I am getting something weird. I want only one point is connected with another point, so that I can see the function in a continuous fashion, however, in my plot points are connected randomly some other points. Please see the second plot.
Below is the code:
x <- runif(100, -1,1) # inputs: uniformly distributed [-1,1]
noise <- rnorm(length(x), 0, 0.2) # normally distributed noise (mean=0, sd=0.2)
f_x <- 8*x^4 - 10*x^2 + x - 4 # f(x), signal without noise
y <- f_x + noise # signal with noise
# plots
x11()
# plot of noisy data (y)
plot(x, y, xlim=range(x), ylim=range(y), xlab="x", ylab="y",
main = "observed noisy data", pch=16)
x11()
# plot of noiseless data (f_x)
plot(x, f_x, xlim=range(x), ylim=range(f_x), xlab="x", ylab="y",
main = "noise-less data",pch=16)
lines(x, f_x, xlim=range(x), ylim=range(f_x), pch=16)
# NOTE: I have also tried this (type="l" is supposed to create lines between the points in the right order), but also not working:
plot(x, f_x, xlim=range(x), ylim=range(f_x), xlab="x", ylab="y",
main = "noise-less data", pch=16, type="l")
First plot is correct: While second is not what I want, I want a continuous plot:
解决方案
You have to sort the x values:
plot(x, f_x, xlim=range(x), ylim=range(f_x), xlab="x", ylab="y",
main = "noise-less data",pch=16)
lines(x[order(x)], f_x[order(x)], xlim=range(x), ylim=range(f_x), pch=16)
这篇关于连接 R 中绘图函数中的点的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文