Skip to contents

Plotting methods for tidygam objects.

Usage

# S3 method for tidygam
plot(x, series = NULL, comparison = NULL, raster_interp = FALSE, ...)

Arguments

x

A tidygam object (see predict_gam()).

series

A string specifying the variable that corresponds to the series to be plotted on the $x$-axis. If a string is given, the other numeric variables in the model are set to their mean value, unless specific values are given in values. If a character vector of two strings is given, the two variables will be taken as the elements of a tensor product smooth. This allows the user to plot 2D raster plots.

comparison

Name of a categorical predictor to compare as a string.

raster_interp

Whether to linearly interpolate when plotting a tensor product smooth/interaction. It makes sense only when series has two variables. The default is FALSE.

...

Arguments passed to plot().

Value

A ggplot object.

Examples

library(mgcv)
set.seed(10)
sim_data <- gamSim(4)
#> Factor `by' variable example

model_1 <- gam(y ~ s(x2, by = fac) + s(x0), data = sim_data)

preds_1 <- predict_gam(model_1, length_out = 50, exclude_terms = "s(x0)")
#> Warning: Could not recover model data from environment. Please make sure your
#>   data is available in your workspace.
#>   Trying to retrieve data from the model frame now.
#> Warning: There was 1 warning in `dplyr::mutate()`.
#>  In argument: `fit = rowSums(dplyr::across())`.
#> Caused by warning:
#> ! Using `across()` without supplying `.cols` was deprecated in dplyr 1.1.0.
#>  Please supply `.cols` instead.
plot(preds_1, "x2")


preds_2 <- predict_gam(model_1, length_out = 100, values = list(x0 = 0))
#> Warning: Could not recover model data from environment. Please make sure your
#>   data is available in your workspace.
#>   Trying to retrieve data from the model frame now.
plot(preds_2, "x2", "fac")

library(ggplot2)
plot(preds_2, "x2", "fac") +
  scale_fill_brewer(type = "qual") +
  scale_color_brewer(type = "qual")


# Plotting tensor product smooths/interactions
model_2 <- gam(y ~ te(x0, x2, by = fac), data = sim_data)
preds_3 <- predict_gam(model_2)
#> Warning: Could not recover model data from environment. Please make sure your
#>   data is available in your workspace.
#>   Trying to retrieve data from the model frame now.
preds_3 %>% plot(series = c("x0", "x2"), comparison = "fac")