# generate artificial data
set.seed(98723)
my.data <- data.frame(x = rnorm(100) + (0:99) / 10 - 5,
y = rnorm(100) + (0:99) / 10 - 5,
group = c("A", "B"))
# using defaults (major axis regression)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_line() +
stat_ma_eq()
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_line() +
stat_ma_eq(mapping = use_label("eq"))
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_line() +
stat_ma_eq(mapping = use_label("eq"), decreasing = TRUE)
# use_label() can assemble and map a combined label
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_line(method = "MA") +
stat_ma_eq(mapping = use_label("eq", "R2", "P"))
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_line(method = "MA") +
stat_ma_eq(mapping = use_label("R2", "P", "theta", "method"))
# using ranged major axis regression
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_line(method = "RMA",
range.y = "interval",
range.x = "interval") +
stat_ma_eq(mapping = use_label("eq", "R2", "P"),
method = "RMA",
range.y = "interval",
range.x = "interval")
# No permutation-based test
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_line(method = "MA") +
stat_ma_eq(mapping = use_label("eq", "R2"),
method = "MA",
nperm = 0)
# explicit formula "x explained by y"
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_line(formula = x ~ y) +
stat_ma_eq(formula = x ~ y,
mapping = use_label("eq", "R2", "P"))
# modifying both variables within aes()
ggplot(my.data, aes(log(x + 10), log(y + 10))) +
geom_point() +
stat_poly_line() +
stat_poly_eq(mapping = use_label("eq"),
eq.x.rhs = "~~log(x+10)",
eq.with.lhs = "log(y+10)~~`=`~~")
# grouping
ggplot(my.data, aes(x, y, color = group)) +
geom_point() +
stat_ma_line() +
stat_ma_eq()
# labelling equations
ggplot(my.data,
aes(x, y, shape = group, linetype = group, grp.label = group)) +
geom_point() +
stat_ma_line(color = "black") +
stat_ma_eq(mapping = use_label("grp", "eq", "R2")) +
theme_classic()
# Inspecting the returned data using geom_debug()
# This provides a quick way of finding out the names of the variables that
# are available for mapping to aesthetics with after_stat().
gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)
if (gginnards.installed)
library(gginnards)
# default is output.type = "expression"
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_eq(geom = "debug")
if (FALSE) {
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_eq(mapping = aes(label = after_stat(eq.label)),
geom = "debug",
output.type = "markdown")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_eq(geom = "debug", output.type = "text")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_eq(geom = "debug", output.type = "numeric")
}
Run the code above in your browser using DataLab