# NOT RUN {
# 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)
my.data <- data.frame(x = x, y = y,
group = c("A", "B"),
y2 = y * c(0.5,2),
w = sqrt(x))
# using defaults
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line() +
stat_quant_eq()
# 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(aes(color = after_stat(quantile.f))) +
stat_quant_eq(aes(color = after_stat(quantile.f))) +
labs(color = "Quantiles")
# location and colour
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(aes(color = after_stat(quantile.f))) +
stat_quant_eq(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)
# no weights
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = formula) +
stat_quant_eq(formula = formula)
# angle
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = formula) +
stat_quant_eq(formula = formula, angle = 90, hstep = 0.05, vstep = 0,
label.y = 0.98, hjust = 1)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = formula) +
stat_quant_eq(formula = formula, angle = 90,
hstep = 0.05, vstep = 0, hjust = 0,
label.y = 0.25)
# 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)
# grouping
ggplot(my.data, aes(x, y, color = group)) +
geom_point() +
stat_quant_line(formula = formula) +
stat_quant_eq(formula = formula)
ggplot(my.data, aes(x, y, color = group)) +
geom_point() +
stat_quant_line(formula = formula) +
stat_quant_eq(formula = formula, angle = 90,
hstep = 0.05, vstep = 0, hjust = 0,
size = 3, label.y = 0.3)
# labelling equations
ggplot(my.data, aes(x, y, shape = group, linetype = group,
grp.label = group)) +
geom_point() +
stat_quant_line(formula = formula, color = "black") +
stat_quant_eq(aes(label = paste(after_stat(grp.label), after_stat(eq.label), sep = "*\": \"*")),
formula = formula) +
theme_classic()
# setting non-default quantiles
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(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))
# Location of equations
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = formula) +
stat_quant_eq(formula = formula, label.y = "bottom", label.x = "right")
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_line(formula = formula) +
stat_quant_eq(formula = formula, label.y = 0.03, label.x = 0.95, vstep = 0.04)
# using weights
ggplot(my.data, aes(x, y, weight = w)) +
geom_point() +
stat_quant_line(formula = formula) +
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)
# user specified label
ggplot(my.data, aes(x, y, color = group, grp.label = group)) +
geom_point() +
stat_quant_line(method = "rq", formula = formula,
quantiles = c(0.05, 0.5, 0.95)) +
stat_quant_eq(aes(label = paste(after_stat(grp.label), "*\": \"*",
after_stat(eq.label), sep = "")),
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(method = "rq", formula = formula, quantiles = 0.5) +
stat_quant_eq(label.x = "left", label.y = "top",
formula = formula)
# 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.
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_eq(formula = formula, geom = "debug")
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_eq(aes(label = after_stat(eq.label)),
formula = formula, geom = "debug",
output.type = "markdown")
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_eq(formula = formula, geom = "debug", output.type = "text")
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_quant_eq(formula = formula, geom = "debug", output.type = "numeric")
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")
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