Setting Custom X-Axis Limits When Plotting Generalized Additive Models in R

Plotting GAM in R: Setting Custom x-axis Limits?

When working with Generalized Additive Models (GAMs) in R, it’s often desirable to plot the predicted fits for these models. However, one common challenge is setting custom x-axis limits, especially when dealing with categorical or grouped data.

In this article, we’ll explore how to set custom x-axis limits when plotting GAM models in R, using the gratia package and its smooth_estimates() function.

Introduction to GAM Models

Before diving into the details of plotting GAMs, let’s quickly review what a Generalized Additive Model (GAM) is. A GAM is an extension of traditional linear regression that allows for non-linear relationships between the predictor variables and the response variable.

In R, we can fit GAM models using the mgcv package. One common way to specify a GAM model is through the use of s() functions, which represent smooth terms for each predictor variable.

For example, consider the following simple GAM model:

m <- gam(y ~ s(x, by = group), data = d)

This model includes a smooth term for the continuous predictor x, and we want to specify it separately for each level of the categorical predictor group.

The Challenge: Setting Custom x-axis Limits

When plotting this GAM model, you may notice that the x-axis automatically accommodates the different ranges of values for each group. However, if your data has a varying scale (e.g., Time), you might want to plot the model on a fixed range or set custom limits.

Unfortunately, when using plot.gam(), the default behavior is to predict values across a shared x-axis limit. This can be misleading if you want to see how the model fits each group separately.

Solution: Using gratia and smooth_estimates()

If you install the current development version (>= 0.6.0.9111) from GitHub, gratia will provide a new functionality to evaluate smooths at observed data only, allowing for custom x-axis limits.

Here’s an example of how to use this feature:

remotes::install_github("gavinsimpson/gratia")

library('mgcv')
library('gratia')
library('dplyr')
library('ggplot2')

# Load data
d <- data.frame(group = rep(c('A','B','C'), each = 100),
               x = c(seq(0,1,length=100),
                     seq(.2,1,length=100),
                     seq(0,.5,length=100))) %>% 
  mutate(y = sin(2*pi*x) + rnorm(100, sd=0.3),
         group = as.factor(group))

# Fit GAM model
m <- gam(y ~ s(x, by = group), data = d, method = 'REML')

# Evaluate smooths at observed data only
sm <- smooth_estimates(m, data = d) %>% 
  add_confint()

# Recreate plot with custom x-axis limits
ggplot(sm, aes(x = x, y = est, colour = group)) + 
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci, colour = NULL, fill = group),
              alpha = 0.2) + 
  geom_line() + 
  facet_wrap(~ group)

In this example, we use smooth_estimates() to evaluate the smooths at observed data only, and then recreate the plot with custom x-axis limits using ggplot().

Conclusion

Plotting GAM models in R can be challenging, especially when dealing with categorical or grouped data. However, by using the gratia package and its smooth_estimates() function, we can set custom x-axis limits and create informative plots that showcase how the model fits each group separately.

By following this article’s guidelines, you should now have a better understanding of how to plot GAM models with custom x-axis limits in R.


Last modified on 2025-04-10