Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接R中绘图函数中的点的线_R_Plot - Fatal编程技术网

连接R中绘图函数中的点的线

连接R中绘图函数中的点的线,r,plot,R,Plot,我有一个关于R编程语言的绘图函数的简单问题。我想在这些点(和)之间划一条线,然而,我得到了一些奇怪的东西。我只希望一个点与另一个点相连,这样我就可以连续地看到函数,然而,在我的绘图中,点随机地与其他一些点相连。请看第二幅图 代码如下: x <- runif(100, -1,1) # inputs: uniformly distributed [-1,1] noise <- rnorm(length(x), 0, 0.2) # normally distributed noise (m

我有一个关于R编程语言的绘图函数的简单问题。我想在这些点(和)之间划一条线,然而,我得到了一些奇怪的东西。我只希望一个点与另一个点相连,这样我就可以连续地看到函数,然而,在我的绘图中,点随机地与其他一些点相连。请看第二幅图

代码如下:

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")

x您必须对x值进行排序:

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)

你好,非常感谢。您能解释一下为什么需要对“x”和“f_x”的值进行排序吗?因为绘制图形需要单调增加x值。如果您修改第一行
x Yes,这是有意义的。但是,我认为x应该已经包含单调递增的值,或者应该由plot函数来处理。很高兴知道。我对R很陌生。谢谢。
plot
可以按任何顺序放置点-如果它自动排序,就不可能画出像你这样的图形。而且
x
显然还没有排序-这是一次随机抽签!如果100个随机点排序出来,你应该担心随机数生成器!另见