黄土在 R 中不合适,到处都是线条?

Loess fit wrong in R, lines all over the place?

我在使用 R 中的 Loess 时遇到问题。我使用的是每个 R 安装中包含的默认 iris 数据集。然而,当我尝试制作一条黄土线时,它到处都是。 loess(y~x) 的 loess 线是红色的,因为我想试验看看我是否把 x 和 y 排序错了,所以 loess(x~y) 的线是蓝色的。如您所见,这些行显然是错误的。为什么会这样?我似乎无法修复它。这是代码:

#ignore
library(lattice)
#cloud()

#CODE OF CONCERN BELOW
data <- iris
n<-150
x <- data$Petal.Length
y<-data$Petal.Width
plot(y ~ x)
loess_fit <- loess(y~x)
lines(x, predict(loess_fit), col = "blue")

这是我得到的图片:

使用黄土前需要先订购点数

x <- sort(data$Petal.Length)
y<-data$Petal.Width[order(data$Petal.Length)] 
plot(y ~ x)
loess_fit <- loess(y~x)
lines(x, predict(loess_fit), col = "blue")