From fa854e6feba27547122237c4ea7397a7220dc9d9 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Thu, 11 Aug 2022 09:06:48 +0900 Subject: [PATCH] Add an example for stat_align() --- R/geom-ribbon.r | 16 ++++++++++++++++ man/geom_ribbon.Rd | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/R/geom-ribbon.r b/R/geom-ribbon.r index 2c8e1ba641..ed6f058452 100644 --- a/R/geom-ribbon.r +++ b/R/geom-ribbon.r @@ -41,6 +41,22 @@ #' h + #' geom_ribbon(aes(ymin = level - 1, ymax = level + 1), fill = "grey70") + #' geom_line(aes(y = level)) +#' +#' # The underlying stat_align() takes care of unaligned data points +#' df <- data.frame( +#' g = c("a", "a", "a", "b", "b", "b"), +#' x = c(1, 3, 5, 2, 4, 6), +#' y = c(2, 5, 1, 3, 6, 7) +#' ) +#' a <- ggplot(df, aes(x, y, fill = g)) + +#' geom_area() +#' +#' # Two groups have points on different X values. +#' a + geom_point(size = 8) + facet_grid(g ~ .) +#' +#' # stat_align() interpolates and aligns the value so that the areas can stack +#' # properly. +#' a + geom_point(stat = "align", position = "stack", size = 8) geom_ribbon <- function(mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., diff --git a/man/geom_ribbon.Rd b/man/geom_ribbon.Rd index 747333c503..85d54971c6 100644 --- a/man/geom_ribbon.Rd +++ b/man/geom_ribbon.Rd @@ -158,6 +158,22 @@ h + geom_area(aes(x = level, y = year), orientation = "y") h + geom_ribbon(aes(ymin = level - 1, ymax = level + 1), fill = "grey70") + geom_line(aes(y = level)) + +# The underlying stat_align() takes care of unaligned data points +df <- data.frame( + g = c("a", "a", "a", "b", "b", "b"), + x = c(1, 3, 5, 2, 4, 6), + y = c(2, 5, 1, 3, 6, 7) +) +a <- ggplot(df, aes(x, y, fill = g)) + + geom_area() + +# Two groups have points on different X values. +a + geom_point(size = 8) + facet_grid(g ~ .) + +# stat_align() interpolates and aligns the value so that the areas can stack +# properly. +a + geom_point(stat = "align", position = "stack", size = 8) } \seealso{ \code{\link[=geom_bar]{geom_bar()}} for discrete intervals (bars),