set.seed(123456)
my.df <- data.frame(X = rep(1:20,2),
Y = runif(40),
category = rep(c("A","B"), each = 20))
# make sure rows are ordered for X as we will use functions that rely on this
my.df <- my.df[order(my.df[["X"]]), ]
# Centroid
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
stat_centroid(shape = "cross", size = 6) +
geom_point()
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
stat_centroid(geom = "rug", linewidth = 1.5, .fun = median) +
geom_point()
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
stat_centroid(geom = "text", aes(label = category)) +
geom_point()
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
stat_summary_xy(geom = "pointrange",
.fun.x = mean, .fun.y = mean_se) +
geom_point()
# quantiles
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
geom_point() +
stat_apply_group(geom = "rug", .fun.y = quantile, .fun.x = quantile)
ggplot(my.df, aes(x = X, y = Y)) +
geom_point() +
stat_apply_group(geom = "rug", sides = "lr", color = "darkred",
.fun.y = quantile) +
stat_apply_group(geom = "text", hjust = "right", color = "darkred",
.fun.y = quantile,
.fun.x = function(x) {rep(22, 5)}, # set x to 22
mapping = aes(label = after_stat(y.names))) +
expand_limits(x = 21)
my.probs <- c(0.25, 0.5, 0.75)
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
geom_point() +
stat_apply_group(geom = "hline",
aes(yintercept = after_stat(y)),
.fun.y = quantile,
.fun.y.args = list(probs = my.probs))
# cummulative summaries
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
stat_apply_group(.fun.x = function(x) {x},
.fun.y = cummax)
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
stat_apply_group(.fun.x = cumsum, .fun.y = cumsum)
# diff returns a shorter vector by 1 for each group
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
stat_apply_group(.fun.x = function(x) {x[-1L]},
.fun.y = diff, na.rm = TRUE)
# Running summaries
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
geom_point() +
stat_apply_group(.fun.x = function(x) {x},
.fun.y = runmed, .fun.y.args = list(k = 5))
# Rescaling per group
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
stat_apply_group(.fun.x = function(x) {x},
.fun.y = function(x) {(x - min(x)) / (max(x) - min(x))})
# inspecting the returned data
if (requireNamespace("gginnards", quietly = TRUE)) {
library(gginnards)
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
stat_centroid(.fun = mean_se, geom = "debug")
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
stat_summary_xy(.fun.y = mean_se, geom = "debug")
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
stat_apply_group(.fun.y = cumsum, geom = "debug")
ggplot(my.df, aes(x = X, y = Y, colour = category)) +
geom_point() +
stat_apply_group(geom = "debug",
.fun.x = quantile,
.fun.x.args = list(probs = my.probs),
.fun.y = quantile,
.fun.y.args = list(probs = my.probs))
}
Run the code above in your browser using DataLab