stat_debug_group in ggpmisc
library(ggplot2) library(ggpmisc) library(tibble) set.seed(4321) # generate artificial data my.data <- data.frame(x=1:100, y=(x+x^2+x^3)+rnorm(length(x),mean=0,sd=mean(x^3)/4) class=c("A","B"), y2=y*c(0.5, 2), block=c("a","a","b","b")) old_theme <- theme_set(theme_bw()) p1 <- my.data %>% ggplot(aes(x, y)) p1 + stat_debug_group() p1 + geom_point() + stat_debug_group() p1 + geom_point() + stat_debug_group() p1 + geom_point() + stat_debug_group(summary.fun=as_tibble) p1 + geom_point() + stat_debug_group(summary.fun=function(x){x}) p1 + geom_point() + stat_debug_group(summary.fun=summary) p1 + geom_point() + stat_debug_group(summary.fun=nrow) p1 + geom_point() + stat_debug_group(summary.fun=head) p1 + geom_point() + stat_debug_group(summary.fun=head, summary.fun.args=list(n=3)) p2 <- my.data %>% ggplot(aes(x, y, color=class)) p2 + geom_point() + stat_debug_group() p2 + geom_point() + stat_debug_panel() p2 + geom_point() + stat_debug_group(summary.fun=head, summary.fun.args=list(n=3)) p2 + geom_point() + stat_debug_group(summary.fun=nrow) + facet_wrap( ~block) p2 + geom_point() + stat_debug_panel(summary.fun=nrow) + facet_wrap( ~block) p2 + geom_point() + geom_debug(summary.fun=head) p2 + geom_point() + stat_debug_group(geom="label", vjust=c(-0.5,1.5)) tidy.assign <- function(value, name, pos = .GlobalEnv, ...) { assign(x = name, value = value, inherits = FALSE, pos = pos, ...) } p2 + geom_point() + geom_debug(summary.fun=tidy.assign, summary.fun.args=list(name="debug_data"), print.fun = NULL) debug_data p2 + geom_point() + geom_debug(summary.fun=NULL) p2 + geom_point() + stat_smooth(method="lm", geom="debug") p2 + geom_point() + stat_peaks(span = NULL, geom = "debug") formula <- y ~ poly(x, 3, raw = TRUE) p1 + stat_fit_residuals(formula=formula, geom="debug", summary.fun=head, summary.fun.args=list(n=12)) formula <- y ~ x + I(x^2) + I(x^3) p1 + geom_point() + stat_fit_augment(method="lm", method.args=list(formula=formula), geom="debug") + stat_fit_augment(method="lm", method.args=list(formula=formula), geom="smooth") p2 + geom_point() + stat_fit_augment(method="lm", method.args=list(formula=formula), geom="debug") + stat_fit_augment(method="lm", method.args=list(formula=formula), geom="smooth")