diff --git a/NEWS.md b/NEWS.md index 10a4869519..f386071137 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,9 @@ * Fixed regression where `draw_key_rect()` stopped using `fill` colours (@mitchelloharawild, #6609). + +* Allow `stat` in `geom_hline`, `geom_vline`, and `geom_abline`. (@sierrajohnson, #6559) + # ggplot2 4.0.0 ## User facing diff --git a/R/geom-abline.R b/R/geom-abline.R index 72f71f490e..a47a9dd86a 100644 --- a/R/geom-abline.R +++ b/R/geom-abline.R @@ -69,11 +69,13 @@ NULL #' geom_hline(aes(yintercept = wt, colour = wt), mean_wt) + #' facet_wrap(~ cyl) geom_abline <- function(mapping = NULL, data = NULL, + stat = "identity", ..., slope, intercept, na.rm = FALSE, - show.legend = NA) { + show.legend = NA, + inherit.aes = FALSE) { # If nothing set, default to y = x if (is.null(mapping) && missing(slope) && missing(intercept)) { @@ -108,11 +110,11 @@ geom_abline <- function(mapping = NULL, data = NULL, layer( data = data, mapping = mapping, - stat = StatIdentity, + stat = stat, geom = GeomAbline, position = PositionIdentity, show.legend = show.legend, - inherit.aes = FALSE, + inherit.aes = inherit.aes, params = list2( na.rm = na.rm, ... diff --git a/R/geom-hline.R b/R/geom-hline.R index 9d59f21b6e..1498077d30 100644 --- a/R/geom-hline.R +++ b/R/geom-hline.R @@ -4,11 +4,12 @@ NULL #' @export #' @rdname geom_abline geom_hline <- function(mapping = NULL, data = NULL, - position = "identity", + stat = "identity", position = "identity", ..., yintercept, na.rm = FALSE, - show.legend = NA) { + show.legend = NA, + inherit.aes = FALSE) { # Act like an annotation if (!missing(yintercept)) { @@ -28,11 +29,11 @@ geom_hline <- function(mapping = NULL, data = NULL, layer( data = data, mapping = mapping, - stat = StatIdentity, + stat = stat, geom = GeomHline, position = position, show.legend = show.legend, - inherit.aes = FALSE, + inherit.aes = inherit.aes, params = list2( na.rm = na.rm, ... diff --git a/R/geom-vline.R b/R/geom-vline.R index 872ac379f7..12302dcf72 100644 --- a/R/geom-vline.R +++ b/R/geom-vline.R @@ -4,11 +4,12 @@ NULL #' @export #' @rdname geom_abline geom_vline <- function(mapping = NULL, data = NULL, - position = "identity", + stat = "identity", position = "identity", ..., xintercept, na.rm = FALSE, - show.legend = NA) { + show.legend = NA, + inherit.aes = FALSE) { # Act like an annotation if (!missing(xintercept)) { @@ -28,11 +29,11 @@ geom_vline <- function(mapping = NULL, data = NULL, layer( data = data, mapping = mapping, - stat = StatIdentity, + stat = stat, geom = GeomVline, position = position, show.legend = show.legend, - inherit.aes = FALSE, + inherit.aes = inherit.aes, params = list2( na.rm = na.rm, ... diff --git a/man/geom_abline.Rd b/man/geom_abline.Rd index 0c3bd12b24..06392312b7 100644 --- a/man/geom_abline.Rd +++ b/man/geom_abline.Rd @@ -9,31 +9,37 @@ geom_abline( mapping = NULL, data = NULL, + stat = "identity", ..., slope, intercept, na.rm = FALSE, - show.legend = NA + show.legend = NA, + inherit.aes = FALSE ) geom_hline( mapping = NULL, data = NULL, + stat = "identity", position = "identity", ..., yintercept, na.rm = FALSE, - show.legend = NA + show.legend = NA, + inherit.aes = FALSE ) geom_vline( mapping = NULL, data = NULL, + stat = "identity", position = "identity", ..., xintercept, na.rm = FALSE, - show.legend = NA + show.legend = NA, + inherit.aes = FALSE ) } \arguments{ @@ -54,6 +60,19 @@ the plot data. The return value must be a \code{data.frame}, and will be used as the layer data. A \code{function} can be created from a \code{formula} (e.g. \code{~ head(.x, 10)}).} +\item{stat}{The statistical transformation to use on the data for this layer. +When using a \verb{geom_*()} function to construct a layer, the \code{stat} +argument can be used to override the default coupling between geoms and +stats. The \code{stat} argument accepts the following: +\itemize{ +\item A \code{Stat} ggproto subclass, for example \code{StatCount}. +\item A string naming the stat. To give the stat as a string, strip the +function name of the \code{stat_} prefix. For example, to use \code{stat_count()}, +give the stat as \code{"count"}. +\item For more information and other ways to specify the stat, see the +\link[=layer_stats]{layer stat} documentation. +}} + \item{...}{Other arguments passed on to \code{\link[=layer]{layer()}}'s \code{params} argument. These arguments broadly fall into one of 4 categories below. Notably, further arguments to the \code{position} argument, or aesthetics that are required @@ -93,6 +112,11 @@ display. To include legend keys for all levels, even when no data exists, use \code{TRUE}. If \code{NA}, all levels are shown in legend, but unobserved levels are omitted.} +\item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, +rather than combining with them. This is most useful for helper functions +that define both data and aesthetics and shouldn't inherit behaviour from +the default plot specification, e.g. \code{\link[=annotation_borders]{annotation_borders()}}.} + \item{position}{A position adjustment to use on the data for this layer. This can be used in various ways, including to prevent overplotting and improving the display. The \code{position} argument accepts the following: