Last chance! 50% off unlimited learning
Sale ends in
stat_fit_tb
fits a model and returns a "tidy" version of
the model's summary or ANOVA table, using 'tidy()
methods from
packages 'broom', 'broom.mixed', or other sources. The annotation is added
to the plots in tabular form.
stat_fit_tb(
mapping = NULL,
data = NULL,
geom = "table_npc",
method = "lm",
method.args = list(formula = y ~ x),
tidy.args = list(),
tb.type = "fit.summary",
tb.vars = NULL,
tb.params = NULL,
digits = 3,
p.digits = digits,
label.x = "center",
label.y = "top",
label.x.npc = NULL,
label.y.npc = NULL,
position = "identity",
table.theme = NULL,
table.rownames = FALSE,
table.colnames = TRUE,
table.hjust = 1,
parse = FALSE,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE,
...
)
A layer specific dataset, only needed if you want to override the plot defaults.
The geometric object to use display the data
character.
lists of arguments to pass to method
and to tidy()
.
character One of "fit.summary", "fit.anova" or "fit.coefs".
character or numeric vectors, optionally named, used to select and/or rename the columns or the parameters in the table returned.
integer indicating the number of significant digits to be used for all numeric values in the table.
integer indicating the number of decimal places to round
p-values to, with those rounded to zero displayed as the next larger
possible value preceded by "<". If p.digits
is outside the
range 1..22 no rounding takes place.
numeric
Coordinates (in data units) to be used
for absolute positioning of the output. If too short they will be recycled.
numeric
with range 0..1 or character.
Coordinates to be used for positioning the output, expressed in "normalized
parent coordinates" or character string. If too short they will be
recycled.
The position adjustment to use for overlapping points on this layer
NULL, list or function A gridExtra ttheme defintion, or a constructor for a ttheme or NULL for default.
logical flag to enable or disabling printing of row names and column names.
numeric Horizontal justification for the core and column headings of the table.
If TRUE, the labels will be parsed into expressions and
displayed as described in ?plotmath
.
a logical indicating whether NA values should be stripped before the computation proceeds.
logical. Should this layer be included in the legends?
NA
, the default, includes if any aesthetics are mapped. FALSE
never includes, and TRUE
always includes.
If FALSE
, overrides the default aesthetics, rather
than combining with them. This is most useful for helper functions that
define both data and aesthetics and shouldn't inherit behaviour from the
default plot specification, e.g. borders
.
The output of tidy()
is
returned as a single "cell" in a tibble (i.e. a tibble nested within a
tibble). The returned data
object contains a single, containing the
result from a single model fit to all data in a panel. If grouping is
present, it is ignored.
To explore the values returned by this statistic, which vary depending
on the model fitting function and model formula we suggest the use of
geom_debug
. An example is shown below.
stat_fit_tb
Applies a model fitting function per panel,
using the grouping factors from easthetic mappings in the fitted model.
This is suitable, for example for analysis of variance used to test for
differences among groups.
The argument to method
can be any fit method for which a
suitable tidy()
method is available, including non-linear
regression. Fit methods retain their default arguments unless orverridden.
A ggplot statistic receives as data a data frame that is not the one passed
as argument by the user, but instead a data frame with the variables mapped
to aesthetics. In other words, it respects the grammar of graphics and
consequently within arguments passed through method.args
names of
aesthetics like $x$ and $y$ should be used instead of the original variable
names, while data is automatically passed the data frame. This helps ensure
that the model is fitted to the same data as plotted in other layers.
broom
and
broom.mixed
for details on how the tidying of the
result of model fits is done. See geom_table
for details on
how inset tables respond to mapped aesthetics and table themes. For details
on predefined table themes see ttheme_gtdefault
.
Other Statistics calling generic tidier methods.:
stat_fit_augment()
,
stat_fit_glance()
,
stat_fit_tidy()
# NOT RUN {
library(broom)
# data for examples
x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
covariate <- sqrt(x) + rnorm(9)
group <- factor(c(rep("A", 4), rep("B", 5)))
my.df <- data.frame(x, group, covariate)
# Linear regression fit summary, by default
ggplot(my.df, aes(covariate, x)) +
geom_point() +
stat_fit_tb() +
expand_limits(y = 70)
# Linear regression fit summary, by default
ggplot(my.df, aes(covariate, x)) +
geom_point() +
stat_fit_tb(digits = 2, p.digits = 4) +
expand_limits(y = 70)
# Linear regression fit summary
ggplot(my.df, aes(covariate, x)) +
geom_point() +
stat_fit_tb(tb.type = "fit.summary") +
expand_limits(y = 70)
# Linear regression ANOVA table
ggplot(my.df, aes(covariate, x)) +
geom_point() +
stat_fit_tb(tb.type = "fit.anova") +
expand_limits(y = 70)
# Linear regression fit coeficients
ggplot(my.df, aes(covariate, x)) +
geom_point() +
stat_fit_tb(tb.type = "fit.coefs") +
expand_limits(y = 70)
# Polynomial regression
ggplot(my.df, aes(covariate, x)) +
geom_point() +
stat_fit_tb(method.args = list(formula = y ~ poly(x, 2))) +
expand_limits(y = 70)
# Polynomial regression with renamed parameters
ggplot(my.df, aes(covariate, x)) +
geom_point() +
stat_fit_tb(method.args = list(formula = y ~ poly(x, 2)),
tb.params = c("x^0" = 1, "x^1" = 2, "x^2" = 3),
parse = TRUE) +
expand_limits(y = 70)
# Polynomial regression with renamed parameters and columns
# using numeric indexes
ggplot(my.df, aes(covariate, x)) +
geom_point() +
stat_fit_tb(method.args = list(formula = y ~ poly(x, 2)),
tb.params = c("x^0" = 1, "x^1" = 2, "x^2" = 3),
tb.vars = c("Term" = 1, "Estimate" = 2, "S.E." = 3,
"italic(F)-value" = 4, "italic(P)-value" = 5),
parse = TRUE) +
expand_limits(y = 70)
# ANOVA summary
ggplot(my.df, aes(group, x)) +
geom_point() +
stat_fit_tb() +
expand_limits(y = 70)
# ANOVA table
ggplot(my.df, aes(group, x)) +
geom_point() +
stat_fit_tb(tb.type = "fit.anova") +
expand_limits(y = 70)
# ANOVA table with renamed and selected columns
# using column names
ggplot(my.df, aes(group, x)) +
geom_point() +
stat_fit_tb(tb.type = "fit.anova",
tb.vars = c(Effect = "term", "df", "italic(F)" = "statistic",
"italic(P)" = "p.value"),
parse = TRUE)
# ANOVA table with renamed and selected columns
# using column names with partial matching
ggplot(my.df, aes(group, x)) +
geom_point() +
stat_fit_tb(tb.type = "fit.anova",
tb.vars = c(Effect = "term", "df", "italic(F)" = "stat",
"italic(P)" = "p"),
parse = TRUE)
# ANOVA summary
ggplot(my.df, aes(group, x)) +
geom_point() +
stat_fit_tb() +
expand_limits(y = 70)
# ANCOVA (covariate not plotted)
ggplot(my.df, aes(group, x, z = covariate)) +
geom_point() +
stat_fit_tb(method.args = list(formula = y ~ x + z),
tb.vars = c(Effect = "term", "italic(F)" = "statistic", "italic(P)" = "p.value"),
parse = TRUE)
# t-test
ggplot(my.df, aes(group, x)) +
geom_point() +
stat_fit_tb(method = "t.test",
tb.vars = c("italic(t)" = "statistic", "italic(P)" = "p.value"),
parse = TRUE)
# t-test (equal variances assumed)
ggplot(my.df, aes(group, x)) +
geom_point() +
stat_fit_tb(method = "t.test",
method.args = list(formula = y ~ x, var.equal = TRUE),
tb.vars = c("italic(t)" = "statistic", "italic(P)" = "p.value"),
parse = TRUE)
# Linear regression using a table theme
ggplot(my.df, aes(covariate, x)) +
geom_point() +
stat_fit_tb(table.theme = ttheme_gtlight) +
expand_limits(y = 70)
# }
Run the code above in your browser using DataLab