简体   繁体   English

如何使用多个绘图增加plot.gam 中的边界线宽度

[英]How to increase the border lines width in plot.gam with multiple plots

Dear StackOverflow users,亲爱的 StackOverflow 用户,

I need to increase the border line width of my multiple GAM plots.我需要增加我的多个 GAM 图的边界线宽度。 I tried to use box(lwd=3) ,but it only worked in the last figure.我尝试使用box(lwd=3) ,但它只适用于最后一个数字。 So my question is how to increase the border line width of every figure.所以我的问题是如何增加每个图形的边框线宽。

Here are my codes这是我的代码

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)

GAM 图

I also tried not to use par(mfrow=c(1,4) , then the figures will out one by one, and I did succeed to increase the border line width of every figure. However, I have over 30 figures need to plot, so this way is not efficiency. Hope you can have suggestions for me. Thank you.我也试过不使用par(mfrow=c(1,4) ,然后数字会一一出来,我确实成功增加了每个数字的边框线宽。但是,我有超过30个数字需要绘制,所以这种方式效率不高,希望大家给点建议,谢谢。

You will need to plot each panel separately;您需要分别绘制每个面板; your call to box(lwd = 3) only comes after the final plot has been produced and as such it can only affect that final plot.您对box(lwd = 3)调用仅生成最终图之后才出现,因此它只能影响最终图。

To draw the individual panels separately I would use the select argument to plot.gam() to chose which smooth I wanted to draw.要单独绘制各个面板,我将使用plot.gam()select参数来选择我想要绘制的平滑。

This is most easily done by creating a quick wrapper function that combines your call to plot.gam() and the call to box() that you want.这最容易通过创建一个快速包装函数来完成,该函数结合了您对plot.gam()的调用box()的调用。

A wrapper function might look like this包装函数可能如下所示

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)
}

where all the options are hard-coded, even the model to plot from, and the only think we pass in as an argument is which smooth to draw via an argument smooth that we pass to select in the plot.gam() call.其中所有选项都是硬编码的,甚至是要绘制的模型,并且我们作为参数传入的唯一想法是通过我们在plot.gam()调用中传递给select的参数来绘制smooth

Then I would use a simple for loop to call the wrapper function to plot each smooth in turn.然后我会使用一个简单的for循环来调用包装函数来依次绘制每个平滑。

Here is a full example of how to do this:以下是如何执行此操作的完整示例:

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)

You could replace the last bit with a par(mfrow) call as in您可以用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM