为什么```plot()``` 函数不能改变图形的颜色?

时间:2021-03-30 22:51:50

标签: r plot functional-programming smoothing

我正在使用标准粗糙度惩罚处理饱和 B 样条基础。

我想要如下图: enter image description here

但我的情节是这样的:

enter image description here

我不知道为什么我的 plot() 函数不能改变线条的颜色。下面是我的代码,你能告诉我解决方案的原因吗?提前致谢!

library('fda')
data(CanadianWeather)
temp = CanadianWeather$dailyAv[,,1]
precip = CanadianWeather$dailyAv[,,2]
daytime = (1:365)-0.5
day5 = seq(0,365,5)
dayrng = c(0,365)
knots = day5
norder = 4
nbasis = length(knots) + norder - 2
plot(create.bspline.basis(dayrng,nbasis=12,norder))
bbasis = create.bspline.basis(dayrng,nbasis,norder, knots)
in.mat = inprod(bbasis,bbasis)
image(in.mat)
bbasis = create.bspline.basis(dayrng,nbasis=21,norder=4)
lambda = 1e6
curv.Lfd = int2Lfd(2)
curv.fdPar = fdPar(bbasis,curv.Lfd,lambda)
tempSmooth1 = smooth.basis(daytime,temp,curv.fdPar)
plot(tempSmooth1$fd)
lambda = 1e1
curv.fdPar$lambda = lambda
tempSmooth = smooth.basis(daytime,temp,curv.fdPar)
lambdas = 10^seq(-4,4,by=0.5)
mean.gcv = rep(0,length(lambdas))
for(ilam in 1:length(lambdas)){
  curv.fdPari = curv.fdPar
  curv.fdPari$lambda = lambdas[ilam]
  tempSmoothi = smooth.basis(daytime,temp,curv.fdPari)
  mean.gcv[ilam] = mean(tempSmoothi$gcv)
}
plot(lambdas,mean.gcv,type='b',log='x')
best = which.min(mean.gcv)
lambdabest = lambdas[best]
curv.fdPar$lambda = lambdabest
tempSmooth = smooth.basis(daytime,temp,curv.fdPar)
plot(tempSmooth)
tempfd = tempSmooth$fd
mtempfd = mean(tempfd)
plot(tempfd, col = 4)
lines(mtempfd,lwd = 2,col = 2)

1 个答案:

答案 0 :(得分:0)

嗯,原因是“...”(您可能已经指定颜色的可选参数,就像您不这样做的)永远不会到达 plot.fd

中的绘图代码

看这里:

https://github.com/cran/fda/blob/master/R/plot.fd.R#L187

这是在您的案例中创建情节的 matplot() 调用。

如何解决:

如果您复制进入 plot.fd 函数的所有代码,然后进行更改:

            matplot(y, fdmat, type = "l", xlim = xlim, ylim = ylim,
                xlab = xlab, ylab = ylab, axes = Axes )

为此:


            matplot(y, fdmat, type = "l", xlim = xlim, ylim = ylim,
                xlab = xlab, ylab = ylab, axes = Axes, ... )

并将该函数另存为其他内容,例如 my.plot.fd,然后它就可以很好地工作了! (见下文)

(只需单击“Raw”即可获取该 github 文件的原始文本内容,将其复制到文本编辑器,进行修改,将其粘贴到 R 中或保存到您提供的文件中)

my.plot.fd(tempfd, col = 4)

enter image description here