Add curve fit
Usage
add_curve_fit(
plot,
dodge_width = NULL,
method = "loess",
linewidth = 0.25,
alpha = 0.4,
preserve = "total",
...
)
Arguments
- plot
A
tidyplot
generated with the functiontidyplot()
.- dodge_width
For adjusting the distance between grouped objects. Defaults to
0.8
for plots with at least one discrete axis and0
for plots with two continuous axes.- method
Smoothing method (function) to use, accepts either
NULL
or a character vector, e.g."lm"
,"glm"
,"gam"
,"loess"
or a function, e.g.MASS::rlm
ormgcv::gam
,stats::lm
, orstats::loess
."auto"
is also accepted for backwards compatibility. It is equivalent toNULL
.For
method = NULL
the smoothing method is chosen based on the size of the largest group (across all panels).stats::loess()
is used for less than 1,000 observations; otherwisemgcv::gam()
is used withformula = y ~ s(x, bs = "cs")
withmethod = "REML"
. Somewhat anecdotally,loess
gives a better appearance, but is \(O(N^{2})\) in memory, so does not work for larger datasets.If you have fewer than 1,000 observations but want to use the same
gam()
model thatmethod = NULL
would use, then setmethod = "gam", formula = y ~ s(x, bs = "cs")
.- linewidth
Thickness of the line in points (pt). Typical values range between
0.25
and1
.- alpha
A
number
between0
and1
for the opacity of an object. A value of0
is completely transparent,1
is completely opaque.- preserve
Should dodging preserve the
"total"
width of all elements at a position, or the width of a"single"
element?- ...
Arguments passed on to
ggplot2::geom_smooth()
.
Examples
time_course %>%
tidyplot(x = day, y = score, color = treatment) %>%
add_curve_fit()
#> `geom_smooth()` using formula = 'y ~ x'
#> Warning: Removed 170 rows containing non-finite outside the scale range
#> (`stat_smooth()`).
# Changing arguments
time_course %>%
tidyplot(x = day, y = score, color = treatment) %>%
add_curve_fit(linewidth = 1)
#> `geom_smooth()` using formula = 'y ~ x'
#> Warning: Removed 170 rows containing non-finite outside the scale range
#> (`stat_smooth()`).
time_course %>%
tidyplot(x = day, y = score, color = treatment) %>%
add_curve_fit(alpha = 0.8)
#> `geom_smooth()` using formula = 'y ~ x'
#> Warning: Removed 170 rows containing non-finite outside the scale range
#> (`stat_smooth()`).
# Remove confidence interval
time_course %>%
tidyplot(x = day, y = score, color = treatment) %>%
add_curve_fit(se = FALSE)
#> `geom_smooth()` using formula = 'y ~ x'
#> Warning: Removed 170 rows containing non-finite outside the scale range
#> (`stat_smooth()`).