您需要分别绘制每个面板;您对box(lwd = 3) 的调用仅在最终情节已经制作完成后进行,因此它只会影响最终情节。
要单独绘制各个面板,我将使用 plot.gam() 的 select 参数来选择我想要绘制的平滑。
这很容易通过创建一个快速包装函数来完成,该函数将您对plot.gam() 的调用组合起来和您想要的对box() 的调用。
包装函数可能如下所示
my_plot_fun <- function(smooth) {
plot(m, select = smooth,
ylab="", xlab="", cex.lab = 1.5, cex.axis= 1.5,
cex.main = 3, lwd = 3)
box(lwd = 3)
}
所有选项都是硬编码的,甚至是要绘制的模型,我们作为参数传入的唯一想法是通过smooth 传递给select 中的select 987654329@电话。
然后我会使用一个简单的for 循环来调用包装函数来依次绘制每个平滑。
下面是一个完整的例子:
library('mgcv')
# simulate some data
set.seed(1)
df <- gamSim(1)
# fit the GAM
m <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = df, method = "REML")
# wrapper function for the plot you want
my_plot_fun <- function(smooth) {
plot(m, select = smooth,
ylab="", xlab="", cex.lab = 1.5, cex.axis= 1.5,
cex.main = 3, lwd = 3)
box(lwd = 3)
}
# set up the plot to have 1 row and 4 columns
layout(matrix(1:4, ncol = 4))
# loop over the indices 1, 2, 3, 4 to plot each smooth in turn
for (i in seq_len(4)) {
my_plot_fun(i)
}
# reset the device
layout(1)
您可以使用par(mfrow) 调用替换最后一位
# set up the plot to have 1 row and 4 columns
op <- par(mfrow = c(1,4))
# loop over the indices 1, 2, 3, 4 to plot each smooth in turn
for (i in seq_len(4)) {
my_plot_fun(i)
}
# reset the device
par(op)