【问题标题】:why the ```plot()``` function cannot change the color of graph?为什么```plot()```函数不能改变图形的颜色?
【发布时间】:2021-06-26 23:22:56
【问题描述】:

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

我想要如下图:

但我的情节是这样的:

我不知道为什么我的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)

【问题讨论】:

    标签: r plot functional-programming smoothing


    【解决方案1】:

    嗯,原因是'...'(你可能已经指定颜色的可选参数,就像你不一样)永远不会到达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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-27
      • 2020-08-09
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 2019-01-27
      • 2020-10-13
      相关资源
      最近更新 更多