将特定线添加到散点图

Adding a specific line to a scatter plot

我有一个频谱与频率的关系图,我正在尝试通过数据添加一条特定的线,我现在拥有的是

plot(freq, spc, log='xy', type='l')
y.loess <- loess(spc ~ freq, span=0.8, data.frame(x=freq, y=spc))
y.predict <- predict(y.loess, data.frame(x=freq))
lines(freq,y.predict)
lines(freq,y.predict, col='red')

这给了我以下信息

图表的黑色部分是正确的,是我需要的,但红线不正确,我需要的应该是这样的

我认为黄土会起作用,但它不是我想要的。如何在我的数据中添加一行,使其看起来像第二张图片?

您没有可重现的示例,因此,我的回答会非常简单。你可以试试现有的函数 scatter.smooth:

require(graphics)

with(cars, scatter.smooth(speed, dist))

## You can elaborate more on the line and dots as your will:
with(cars, scatter.smooth(speed, dist, lpars =
                    list(col = "red", lwd = 3, lty = 3)))

我会预缩放值并尝试使内核平滑:

Ks <- ksmooth(log(freq),log(spc),kernel = "normal",bandwidth=0.3)  
lines(Ks,col="red")

您可以调整带宽或将其基于您的日志(数据)的标准偏差。查看此 Wikipedia article 以了解使用 npreg 的替代方法。