简体   繁体   中英

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

Dear StackOverflow users,

I need to increase the border line width of my multiple GAM plots. I tried to use box(lwd=3) ,but it only worked in the last figure. 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.

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.

To draw the individual panels separately I would use the select argument to plot.gam() to chose which smooth I wanted to draw.

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.

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.

Then I would use a simple for loop to call the wrapper function to plot each smooth in turn.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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