# generate artificial data
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
y <- y / max(y)
my.data <- data.frame(x = x, y = y,
group = c("A", "B"),
y2 = y * c(1, 2) + max(y) * c(0, 0.1),
w = sqrt(x))
# using defaults
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line() +
stat_quant_eq()
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line() +
stat_quant_eq(mapping = use_label("eq"))
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line() +
stat_quant_eq(mapping = use_label("eq"), decreasing = TRUE)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line() +
stat_quant_eq(mapping = use_label("eq", "method"))
# same formula as default
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = y ~ x) +
stat_quant_eq(formula = y ~ x)
# explicit formula "x explained by y"
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = x ~ y) +
stat_quant_eq(formula = x ~ y)
# using color
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(mapping = aes(color = after_stat(quantile.f))) +
stat_quant_eq(mapping = aes(color = after_stat(quantile.f))) +
labs(color = "Quantiles")
# location and colour
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(mapping = aes(color = after_stat(quantile.f))) +
stat_quant_eq(mapping = aes(color = after_stat(quantile.f)),
label.y = "bottom", label.x = "right") +
labs(color = "Quantiles")
# give a name to a formula
formula <- y ~ poly(x, 3, raw = TRUE)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = formula, linewidth = 0.5) +
stat_quant_eq(formula = formula)
# angle
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = formula, linewidth = 0.5) +
stat_quant_eq(formula = formula, angle = 90, hstep = 0.04, vstep = 0,
label.y = 0.02, hjust = 0) +
expand_limits(x = -15) # make space for equations
# user set quantiles
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = formula, quantiles = 0.5) +
stat_quant_eq(formula = formula, quantiles = 0.5)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_band(formula = formula,
quantiles = c(0.1, 0.5, 0.9)) +
stat_quant_eq(formula = formula, parse = TRUE,
quantiles = c(0.1, 0.5, 0.9))
# grouping
ggplot(my.data, aes(x, y2, color = group)) +
geom_point() +
stat_quant_line(formula = formula, linewidth = 0.5) +
stat_quant_eq(formula = formula)
ggplot(my.data, aes(x, y2, color = group)) +
geom_point() +
stat_quant_band(formula = formula, linewidth = 0.75) +
stat_quant_eq(formula = formula) +
theme_bw()
# labelling equations
ggplot(my.data, aes(x, y2, shape = group, linetype = group,
grp.label = group)) +
geom_point() +
stat_quant_band(formula = formula, color = "black", linewidth = 0.75) +
stat_quant_eq(mapping = use_label("grp", "eq", sep = "*\": \"*"),
formula = formula) +
expand_limits(y = 3) +
theme_classic()
# modifying the explanatory variable within the model formula
# modifying the response variable within aes()
formula.trans <- y ~ I(x^2)
ggplot(my.data, aes(x, y + 1)) +
geom_point() +
stat_quant_line(formula = formula.trans) +
stat_quant_eq(mapping = use_label("eq"),
formula = formula.trans,
eq.x.rhs = "~x^2",
eq.with.lhs = "y + 1~~`=`~~")
# using weights
ggplot(my.data, aes(x, y, weight = w)) +
geom_point() +
stat_quant_line(formula = formula, linewidth = 0.5) +
stat_quant_eq(formula = formula)
# no weights, quantile set to upper boundary
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = formula, quantiles = 0.95) +
stat_quant_eq(formula = formula, quantiles = 0.95)
# manually assemble and map a specific label using paste() and aes()
ggplot(my.data, aes(x, y2, color = group, grp.label = group)) +
geom_point() +
stat_quant_line(method = "rq", formula = formula,
quantiles = c(0.05, 0.5, 0.95),
linewidth = 0.5) +
stat_quant_eq(mapping = aes(label = paste(after_stat(grp.label), "*\": \"*",
after_stat(eq.label), sep = "")),
quantiles = c(0.05, 0.5, 0.95),
formula = formula, size = 3)
# manually assemble and map a specific label using sprintf() and aes()
ggplot(my.data, aes(x, y2, color = group, grp.label = group)) +
geom_point() +
stat_quant_band(method = "rq", formula = formula,
quantiles = c(0.05, 0.5, 0.95)) +
stat_quant_eq(mapping = aes(label = sprintf("%s*\": \"*%s",
after_stat(grp.label),
after_stat(eq.label))),
quantiles = c(0.05, 0.5, 0.95),
formula = formula, size = 3)
# geom = "text"
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = formula, quantiles = 0.5) +
stat_quant_eq(label.x = "left", label.y = "top",
formula = formula,
quantiles = 0.5)
# 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 using after_stat().
gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)
if (gginnards.installed)
library(gginnards)
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_eq(formula = formula, geom = "debug")
if (FALSE) {
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_eq(mapping = aes(label = after_stat(eq.label)),
formula = formula, geom = "debug",
output.type = "markdown")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_eq(formula = formula, geom = "debug", output.type = "text")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_eq(formula = formula, geom = "debug", output.type = "numeric")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_eq(formula = formula, quantiles = c(0.25, 0.5, 0.75),
geom = "debug", output.type = "text")
if (gginnards.installed)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_eq(formula = formula, quantiles = c(0.25, 0.5, 0.75),
geom = "debug", output.type = "numeric")
}
Run the code above in your browser using DataLab