# NOT RUN {
# 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()
# using major axis regression
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_line(method = "MA") +
stat_ma_eq(aes(label =
paste(after_stat(eq.label),
after_stat(p.value.label),
sep = "*\", \"*")),
method = "MA")
# using standard major axis regression
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_line(method = "SMA") +
stat_ma_eq(aes(label =
paste(after_stat(eq.label),
after_stat(p.value.label),
sep = "*\", \"*")),
method = "SMA")
# 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(aes(label =
paste(after_stat(eq.label),
after_stat(p.value.label),
sep = "*\", \"*")),
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(aes(label =
paste(after_stat(eq.label),
after_stat(p.value.label),
sep = "*\", \"*")),
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,
aes(label = paste(after_stat(eq.label),
after_stat(p.value.label),
sep = "*\", \"*")))
# 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(aes(label = paste(after_stat(grp.label),
after_stat(eq.label),
sep = "*\": \"*"))) +
theme_classic()
# geom = "text"
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_line() +
stat_ma_eq(label.x = "left", label.y = "top")
# Inspecting the returned data using geom_debug()
if (requireNamespace("gginnards", quietly = TRUE)) {
library(gginnards)
# This provides a quick way of finding out the names of the variables that
# are available for mapping to aesthetics.
# default is ouitput.type = "expression"
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_eq(geom = "debug")
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_eq(aes(label = after_stat(eq.label)),
geom = "debug",
output.type = "markdown")
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_ma_eq(geom = "debug", output.type = "text")
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