Search code examples
rplotmgcv

Change options of GAM plots indvidually?


Consider the code below to fit a GAM:

library(mgcv)
x1=runif(200)
x2=runif(200)
y=sin(x1)+x2^2+rnorm(200)
m = gam(y~s(x1)+s(x2))

Now using plot(m) plots smooth terms plots separately, so to merge plots I've found this code:

par(mfrow=c(1,2))
plot(m)

The plotted graph looks like this:

merged plots of smooth terms

However I can not change the options of each plot individually, e.g. setting main="plot" changes both plots titles and I need to title each plot differently. How can I change set options of each plot separately?


Solution

  • You can use the select argument of plot.gam:

    select: Allows the plot for a single model term to be selected for printing. e.g. if you just want the plot for the second smooth term set select=2.

    For example:

    library(mgcv)
    df <- data.frame(x1 = runif(200), x2 = runif(200))
    df <- transform(df, y = sin(x1) + x2^2 + rnorm(200))
    
    m <- gam(y~s(x1)+s(x2), data = df)
    
    layout(matrix(1:2, ncol = 2))
    plot(m, select = 1, main = "First smooth")
    plot(m, select = 2, main = "Second smooth")
    layout(1)
    

    The resulting plot is shown below

    enter image description here