| scatter.gam | R Documentation |
Creates a scatter plot with a GAM (Generalized Additive Model) smooth line.
Supports both scatter.gam(x, y) and scatter.gam(y ~ x).
scatter.gam(
x,
y,
data.dots = TRUE,
three.dots = FALSE,
data = NULL,
k = NULL,
plot.dist = NULL,
dot.pch = 16,
dot.col = adjustcolor("gray", 0.7),
jitter = FALSE,
...
)
x |
A numeric vector of x values, or a formula of the form |
y |
A numeric vector of y values. Not used if |
data.dots |
Logical. If TRUE, displays data on scatterplot |
three.dots |
Logical. If TRUE, divides x into tertiles and puts markers on the average x & y for each |
data |
An optional data frame containing the variables |
k |
Optional integer specifying the basis dimension for the smooth term
in the GAM model (passed to |
plot.dist |
Character string specifying how to plot the distribution of |
dot.pch |
Plotting character for data points when |
dot.col |
Color for data points when |
jitter |
Logical. If TRUE, applies a small amount of jitter to data points to reduce overplotting. Default is FALSE. |
... |
Additional arguments passed to
|
This function fits a GAM model with a smooth term for x and plots the fitted
smooth line. The function uses the mgcv package's gam() function.
When three.dots = TRUE, the x variable is divided into three equal-sized
groups (tertiles), and the mean x and y values for each group are plotted as
points. This provides a simple summary of the relationship across the range of x.
Invisibly returns the fitted GAM model object.
scatter.smooth for a simpler loess-based scatter plot smoother.
# Generate sample data for examples
x <- rnorm(100)
y <- 2*x + rnorm(100)
# Plot GAM smooth line only
scatter.gam(x, y)
# Equivalent call using formula syntax (y ~ x)
scatter.gam(y ~ x)
# Include scatter plot with underlying data points behind the GAM line
scatter.gam(x, y, data.dots = TRUE)
# Include summary points showing mean x and y for each tertile bin
scatter.gam(x, y, three.dots = TRUE)
# Customize the plot with a custom title, line color, and line width
scatter.gam(x, y, data.dots = TRUE, col = "red", lwd = 2, main = "GAM Fit")
# Control smoothness of the GAM line by specifying the basis dimension
scatter.gam(x, y, k = 10)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.