【问题标题】:How to increase the border lines width in plot.gam with multiple plots如何使用多个绘图增加 plot.gam 中的边界线宽度
【发布时间】:2020-11-05 03:43:54
【问题描述】:

尊敬的 StackOverflow 用户,

我需要增加多个 GAM 图的边界线宽度。我尝试使用box(lwd=3),但它只在最后一个图中有效。所以我的问题是如何增加每个图形的边框线宽度。

这是我的代码

Gam_AF_species <- gam(Species.AF ~ s(SST) + s(SBT, k=3) + s(Salinity) + 
                      s(Primary.productivity), data = Data)
par(mfrow = c(1,4))
plot.gam(Gam_AF_species, ylab="", xlab="", cex.lab = 1.5, cex.axis= 1.5, 
         cex.main = 3, lwd = 3, lwd.tick = 3)
box(lwd=3)

我也尝试不使用par(mfrow=c(1,4),然后数字会一个一个地出来,我确实成功增加了每个数字的边框线宽度。但是,我有30多个数字需要绘制,所以这种方式效率不高。希望你能给我一些建议。谢谢。

【问题讨论】:

    标签: r plot width border gam


    【解决方案1】:

    您需要分别绘制每个面板;您对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)
    

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多