diff --git a/DESCRIPTION b/DESCRIPTION index b4cd9ec950..1bbe9c79af 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -150,7 +150,6 @@ Collate: 'geom-density2d.R' 'geom-dotplot.R' 'geom-errorbar.R' - 'geom-errorbarh.R' 'geom-freqpoly.R' 'geom-function.R' 'geom-hex.R' diff --git a/R/geom-errorbar.R b/R/geom-errorbar.R index 3e40b20318..7551f0be59 100644 --- a/R/geom-errorbar.R +++ b/R/geom-errorbar.R @@ -23,6 +23,35 @@ geom_errorbar <- function(mapping = NULL, data = NULL, ) } +#' @export +#' @rdname geom_linerange +#' @note +#' `geom_errorbarh()` is `r lifecycle::badge("deprecated")`. Use +#' `geom_errorbar(orientation = "y")` instead. +geom_errorbarh <- function(mapping = NULL, data = NULL, + stat = "identity", position = "identity", + ..., + orientation = "y", + na.rm = FALSE, + show.legend = NA, + inherit.aes = TRUE) { + deprecate_soft0( + "3.5.2", "geom_errobarh()", "geom_errorbar(orientation = \"y\")", + id = "no-more-errorbarh" + ) + geom_errorbar( + mapping = mapping, + data = data, + stat = stat, + position = position, + ..., + orientation = orientation, + na.rm = na.rm, + show.legend = show.legend, + inherit.aes = inherit.aes + ) +} + #' @rdname ggplot2-ggproto #' @format NULL #' @usage NULL @@ -80,3 +109,18 @@ GeomErrorbar <- ggproto("GeomErrorbar", Geom, rename_size = TRUE ) + +#' @rdname ggplot2-ggproto +#' @format NULL +#' @usage NULL +#' @export +GeomErrorbarh <- ggproto( + "GeomErrorbarh", GeomErrorbar, + setup_params = function(data, params) { + deprecate_soft0( + "3.5.2", "geom_errobarh()", "geom_errorbar(orientation = \"y\")", + id = "no-more-errorbarh" + ) + GeomLinerange$setup_params(data, params) + } +) diff --git a/R/geom-errorbarh.R b/R/geom-errorbarh.R deleted file mode 100644 index c38b9b7cd6..0000000000 --- a/R/geom-errorbarh.R +++ /dev/null @@ -1,91 +0,0 @@ -#' Horizontal error bars -#' -#' A rotated version of [geom_errorbar()]. -#' -#' @eval rd_aesthetics("geom", "errorbarh") -#' @inheritParams layer -#' @inheritParams geom_point -#' @export -#' @examples -#' df <- data.frame( -#' trt = factor(c(1, 1, 2, 2)), -#' resp = c(1, 5, 3, 4), -#' group = factor(c(1, 2, 1, 2)), -#' se = c(0.1, 0.3, 0.3, 0.2) -#' ) -#' -#' # Define the top and bottom of the errorbars -#' -#' p <- ggplot(df, aes(resp, trt, colour = group)) -#' p + -#' geom_point() + -#' geom_errorbarh(aes(xmax = resp + se, xmin = resp - se)) -#' -#' p + -#' geom_point() + -#' geom_errorbarh(aes(xmax = resp + se, xmin = resp - se, height = .2)) -geom_errorbarh <- function(mapping = NULL, data = NULL, - stat = "identity", position = "identity", - ..., - na.rm = FALSE, - show.legend = NA, - inherit.aes = TRUE) { - layer( - data = data, - mapping = mapping, - stat = stat, - geom = GeomErrorbarh, - position = position, - show.legend = show.legend, - inherit.aes = inherit.aes, - params = list2( - na.rm = na.rm, - ... - ) - ) -} - - -#' @rdname ggplot2-ggproto -#' @format NULL -#' @usage NULL -#' @export -GeomErrorbarh <- ggproto("GeomErrorbarh", Geom, - - default_aes = aes( - colour = from_theme(ink), - linewidth = from_theme(linewidth), - linetype = from_theme(linetype), - height = 0.5, - alpha = NA - ), - - draw_key = draw_key_path, - - required_aes = c("xmin", "xmax", "y"), - - setup_data = function(data, params) { - data$height <- data$height %||% - params$height %||% (resolution(data$y, FALSE, TRUE) * 0.9) - - transform(data, - ymin = y - height / 2, ymax = y + height / 2, height = NULL - ) - }, - - draw_panel = function(self, data, panel_params, coord, height = NULL, lineend = "butt") { - data <- check_linewidth(data, snake_class(self)) - GeomPath$draw_panel(data_frame0( - x = vec_interleave(data$xmax, data$xmax, NA, data$xmax, data$xmin, NA, data$xmin, data$xmin), - y = vec_interleave(data$ymin, data$ymax, NA, data$y, data$y, NA, data$ymin, data$ymax), - colour = rep(data$colour, each = 8), - alpha = rep(data$alpha, each = 8), - linewidth = rep(data$linewidth, each = 8), - linetype = rep(data$linetype, each = 8), - group = rep(1:(nrow(data)), each = 8), - .size = nrow(data) * 8 - ), panel_params, coord, lineend = lineend) - }, - - rename_size = TRUE -) diff --git a/R/geom-linerange.R b/R/geom-linerange.R index 83360800e2..085d8f98a9 100644 --- a/R/geom-linerange.R +++ b/R/geom-linerange.R @@ -11,8 +11,7 @@ #' `geom_pointrange()`. #' @seealso #' [stat_summary()] for examples of these guys in use, -#' [geom_smooth()] for continuous analogue, -#' [geom_errorbarh()] for a horizontal error bar. +#' [geom_smooth()] for continuous analogue #' @export #' @inheritParams layer #' @inheritParams geom_bar diff --git a/man/geom_errorbarh.Rd b/man/geom_errorbarh.Rd deleted file mode 100644 index 4e6fb3aae9..0000000000 --- a/man/geom_errorbarh.Rd +++ /dev/null @@ -1,147 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/geom-errorbarh.R -\name{geom_errorbarh} -\alias{geom_errorbarh} -\title{Horizontal error bars} -\usage{ -geom_errorbarh( - mapping = NULL, - data = NULL, - stat = "identity", - position = "identity", - ..., - na.rm = FALSE, - show.legend = NA, - inherit.aes = TRUE -) -} -\arguments{ -\item{mapping}{Set of aesthetic mappings created by \code{\link[=aes]{aes()}}. If specified and -\code{inherit.aes = TRUE} (the default), it is combined with the default mapping -at the top level of the plot. You must supply \code{mapping} if there is no plot -mapping.} - -\item{data}{The data to be displayed in this layer. There are three -options: - -If \code{NULL}, the default, the data is inherited from the plot -data as specified in the call to \code{\link[=ggplot]{ggplot()}}. - -A \code{data.frame}, or other object, will override the plot -data. All objects will be fortified to produce a data frame. See -\code{\link[=fortify]{fortify()}} for which variables will be created. - -A \code{function} will be called with a single argument, -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{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: -\itemize{ -\item The result of calling a position function, such as \code{position_jitter()}. -This method allows for passing extra arguments to the position. -\item A string naming the position adjustment. To give the position as a -string, strip the function name of the \code{position_} prefix. For example, -to use \code{position_jitter()}, give the position as \code{"jitter"}. -\item For more information and other ways to specify the position, see the -\link[=layer_positions]{layer position} 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 -can \emph{not} be passed through \code{...}. Unknown arguments that are not part -of the 4 categories below are ignored. -\itemize{ -\item Static aesthetics that are not mapped to a scale, but are at a fixed -value and apply to the layer as a whole. For example, \code{colour = "red"} -or \code{linewidth = 3}. The geom's documentation has an \strong{Aesthetics} -section that lists the available options. The 'required' aesthetics -cannot be passed on to the \code{params}. Please note that while passing -unmapped aesthetics as vectors is technically possible, the order and -required length is not guaranteed to be parallel to the input data. -\item When constructing a layer using -a \verb{stat_*()} function, the \code{...} argument can be used to pass on -parameters to the \code{geom} part of the layer. An example of this is -\code{stat_density(geom = "area", outline.type = "both")}. The geom's -documentation lists which parameters it can accept. -\item Inversely, when constructing a layer using a -\verb{geom_*()} function, the \code{...} argument can be used to pass on parameters -to the \code{stat} part of the layer. An example of this is -\code{geom_area(stat = "density", adjust = 0.5)}. The stat's documentation -lists which parameters it can accept. -\item The \code{key_glyph} argument of \code{\link[=layer]{layer()}} may also be passed on through -\code{...}. This can be one of the functions described as -\link[=draw_key]{key glyphs}, to change the display of the layer in the legend. -}} - -\item{na.rm}{If \code{FALSE}, the default, missing values are removed with -a warning. If \code{TRUE}, missing values are silently removed.} - -\item{show.legend}{logical. Should this layer be included in the legends? -\code{NA}, the default, includes if any aesthetics are mapped. -\code{FALSE} never includes, and \code{TRUE} always includes. -It can also be a named logical vector to finely select the aesthetics to -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[=borders]{borders()}}.} -} -\description{ -A rotated version of \code{\link[=geom_errorbar]{geom_errorbar()}}. -} -\section{Aesthetics}{ - -\code{geom_errorbarh()} understands the following aesthetics (required aesthetics are in bold): -\itemize{ -\item \strong{\code{\link[=aes_position]{xmin}}} -\item \strong{\code{\link[=aes_position]{xmax}}} -\item \strong{\code{\link[=aes_position]{y}}} -\item \code{\link[=aes_colour_fill_alpha]{alpha}} -\item \code{\link[=aes_colour_fill_alpha]{colour}} -\item \code{\link[=aes_group_order]{group}} -\item \code{height} -\item \code{\link[=aes_linetype_size_shape]{linetype}} -\item \code{\link[=aes_linetype_size_shape]{linewidth}} -} -Learn more about setting these aesthetics in \code{vignette("ggplot2-specs")}. -} - -\examples{ -df <- data.frame( - trt = factor(c(1, 1, 2, 2)), - resp = c(1, 5, 3, 4), - group = factor(c(1, 2, 1, 2)), - se = c(0.1, 0.3, 0.3, 0.2) -) - -# Define the top and bottom of the errorbars - -p <- ggplot(df, aes(resp, trt, colour = group)) -p + - geom_point() + - geom_errorbarh(aes(xmax = resp + se, xmin = resp - se)) - -p + - geom_point() + - geom_errorbarh(aes(xmax = resp + se, xmin = resp - se, height = .2)) -} diff --git a/man/geom_linerange.Rd b/man/geom_linerange.Rd index 87bc5c8e75..60d10575b0 100644 --- a/man/geom_linerange.Rd +++ b/man/geom_linerange.Rd @@ -4,6 +4,7 @@ \name{geom_crossbar} \alias{geom_crossbar} \alias{geom_errorbar} +\alias{geom_errorbarh} \alias{geom_linerange} \alias{geom_pointrange} \title{Vertical intervals: lines, crossbars & errorbars} @@ -33,6 +34,18 @@ geom_errorbar( inherit.aes = TRUE ) +geom_errorbarh( + mapping = NULL, + data = NULL, + stat = "identity", + position = "identity", + ..., + orientation = "y", + na.rm = FALSE, + show.legend = NA, + inherit.aes = TRUE +) + geom_linerange( mapping = NULL, data = NULL, @@ -162,6 +175,10 @@ the default plot specification, e.g. \code{\link[=borders]{borders()}}.} Various ways of representing a vertical interval defined by \code{x}, \code{ymin} and \code{ymax}. Each case draws a single graphical object. } +\note{ +\code{geom_errorbarh()} is \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}}. Use +\code{geom_errorbar(orientation = "y")} instead. +} \section{Orientation}{ This geom treats each axis differently and, thus, can thus have two orientations. Often the orientation is easy to deduce from a combination of the given mappings and the types of positional scales in use. Thus, ggplot2 will by default try to guess which orientation the layer should have. Under rare circumstances, the orientation is ambiguous and guessing may fail. In that case the orientation can be specified directly using the \code{orientation} parameter, which can be either \code{"x"} or \code{"y"}. The value gives the axis that the geom should run along, \code{"x"} being the default orientation you would expect for the geom. @@ -235,6 +252,5 @@ geom_errorbar( } \seealso{ \code{\link[=stat_summary]{stat_summary()}} for examples of these guys in use, -\code{\link[=geom_smooth]{geom_smooth()}} for continuous analogue, -\code{\link[=geom_errorbarh]{geom_errorbarh()}} for a horizontal error bar. +\code{\link[=geom_smooth]{geom_smooth()}} for continuous analogue } diff --git a/man/ggplot2-ggproto.Rd b/man/ggplot2-ggproto.Rd index c3384f1e45..963df0278c 100644 --- a/man/ggplot2-ggproto.Rd +++ b/man/ggplot2-ggproto.Rd @@ -9,24 +9,23 @@ % R/geom-boxplot.R, R/geom-col.R, R/geom-path.R, R/geom-contour.R, % R/geom-crossbar.R, R/geom-segment.R, R/geom-curve.R, R/geom-ribbon.R, % R/geom-density.R, R/geom-density2d.R, R/geom-dotplot.R, R/geom-errorbar.R, -% R/geom-errorbarh.R, R/geom-function.R, R/geom-hex.R, R/geom-hline.R, -% R/geom-label.R, R/geom-linerange.R, R/geom-point.R, R/geom-pointrange.R, -% R/geom-quantile.R, R/geom-rug.R, R/geom-smooth.R, R/geom-spoke.R, -% R/geom-text.R, R/geom-tile.R, R/geom-violin.R, R/geom-vline.R, -% R/guide-.R, R/guide-axis.R, R/guide-axis-logticks.R, R/guide-axis-stack.R, -% R/guide-axis-theta.R, R/guide-legend.R, R/guide-bins.R, R/guide-colorbar.R, -% R/guide-colorsteps.R, R/guide-custom.R, R/guide-none.R, R/guide-old.R, -% R/layout.R, R/position-.R, R/position-dodge.R, R/position-dodge2.R, -% R/position-identity.R, R/position-jitter.R, R/position-jitterdodge.R, -% R/position-nudge.R, R/position-stack.R, R/scale-.R, R/scale-binned.R, -% R/scale-continuous.R, R/scale-date.R, R/scale-discrete-.R, -% R/scale-identity.R, R/stat-align.R, R/stat-bin.R, R/stat-bin2d.R, -% R/stat-bindot.R, R/stat-binhex.R, R/stat-boxplot.R, R/stat-contour.R, -% R/stat-count.R, R/stat-density-2d.R, R/stat-density.R, R/stat-ecdf.R, -% R/stat-ellipse.R, R/stat-function.R, R/stat-identity.R, R/stat-qq-line.R, -% R/stat-qq.R, R/stat-quantilemethods.R, R/stat-smooth.R, R/stat-sum.R, -% R/stat-summary-2d.R, R/stat-summary-bin.R, R/stat-summary-hex.R, -% R/stat-summary.R, R/stat-unique.R, R/stat-ydensity.R +% R/geom-function.R, R/geom-hex.R, R/geom-hline.R, R/geom-label.R, +% R/geom-linerange.R, R/geom-point.R, R/geom-pointrange.R, R/geom-quantile.R, +% R/geom-rug.R, R/geom-smooth.R, R/geom-spoke.R, R/geom-text.R, +% R/geom-tile.R, R/geom-violin.R, R/geom-vline.R, R/guide-.R, R/guide-axis.R, +% R/guide-axis-logticks.R, R/guide-axis-stack.R, R/guide-axis-theta.R, +% R/guide-legend.R, R/guide-bins.R, R/guide-colorbar.R, R/guide-colorsteps.R, +% R/guide-custom.R, R/guide-none.R, R/guide-old.R, R/layout.R, R/position-.R, +% R/position-dodge.R, R/position-dodge2.R, R/position-identity.R, +% R/position-jitter.R, R/position-jitterdodge.R, R/position-nudge.R, +% R/position-stack.R, R/scale-.R, R/scale-binned.R, R/scale-continuous.R, +% R/scale-date.R, R/scale-discrete-.R, R/scale-identity.R, R/stat-align.R, +% R/stat-bin.R, R/stat-bin2d.R, R/stat-bindot.R, R/stat-binhex.R, +% R/stat-boxplot.R, R/stat-contour.R, R/stat-count.R, R/stat-density-2d.R, +% R/stat-density.R, R/stat-ecdf.R, R/stat-ellipse.R, R/stat-function.R, +% R/stat-identity.R, R/stat-qq-line.R, R/stat-qq.R, R/stat-quantilemethods.R, +% R/stat-smooth.R, R/stat-sum.R, R/stat-summary-2d.R, R/stat-summary-bin.R, +% R/stat-summary-hex.R, R/stat-summary.R, R/stat-unique.R, R/stat-ydensity.R \docType{data} \name{ggplot2-ggproto} \alias{ggplot2-ggproto} diff --git a/tests/testthat/test-function-args.R b/tests/testthat/test-function-args.R index 2a78bf9f50..f7be015fa7 100644 --- a/tests/testthat/test-function-args.R +++ b/tests/testthat/test-function-args.R @@ -13,7 +13,7 @@ test_that("geom_xxx and GeomXxx$draw arg defaults match", { geom_fun_names, c("geom_map", "geom_sf", "geom_smooth", "geom_column", "geom_area", "geom_density", "annotation_custom", "annotation_map", "annotation_raster", - "annotation_id") + "annotation_id", "geom_errorbarh") ) # For each geom_xxx function and the corresponding GeomXxx$draw and diff --git a/tests/testthat/test-geom-errorbar.R b/tests/testthat/test-geom-errorbar.R new file mode 100644 index 0000000000..bdfdf3f88d --- /dev/null +++ b/tests/testthat/test-geom-errorbar.R @@ -0,0 +1,16 @@ +test_that("geom_errorbarh throws deprecation messages", { + + lifecycle::expect_deprecated(geom_errorbarh()) + + p <- ggplot( + data.frame(y = "A", min = 0, max = 10), + aes(y = y, xmin = min, xmax = max) + ) + + layer( + geom = "errorbarh", + stat = "identity", + position = "identity" + ) + + lifecycle::expect_deprecated(ggplot_build(p)) +})