diff --git a/NEWS.md b/NEWS.md index 3ac733842e..dc43b0f7e0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ # ggplot2 (development version) +* You can now omit either `xend` or `yend` from `geom_segment()` as only one + of these is now required. If one is missing, it will be filled from the `x` + and `y` aesthetics respectively. This makes drawing horizontal or vertical + segments a little bit more convenient (@teunbrand, #5140). + * New `plot.tag.location` in `theme()` can control placement of the plot tag in the `"margin"`, `"plot"` or the new `"panel"` option (#4297). diff --git a/R/geom-segment.R b/R/geom-segment.R index a80bf84fa5..611ba85e2c 100644 --- a/R/geom-segment.R +++ b/R/geom-segment.R @@ -102,11 +102,13 @@ geom_segment <- function(mapping = NULL, data = NULL, #' @usage NULL #' @export GeomSegment <- ggproto("GeomSegment", Geom, - required_aes = c("x", "y", "xend", "yend"), + required_aes = c("x", "y", "xend|yend"), non_missing_aes = c("linetype", "linewidth", "shape"), default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA), draw_panel = function(self, data, panel_params, coord, arrow = NULL, arrow.fill = NULL, lineend = "butt", linejoin = "round", na.rm = FALSE) { + data$xend <- data$xend %||% data$x + data$yend <- data$yend %||% data$y data <- check_linewidth(data, snake_class(self)) data <- remove_missing(data, na.rm = na.rm, c("x", "y", "xend", "yend", "linetype", "linewidth", "shape"), diff --git a/man/geom_segment.Rd b/man/geom_segment.Rd index fa4781e151..eb3892920b 100644 --- a/man/geom_segment.Rd +++ b/man/geom_segment.Rd @@ -125,8 +125,7 @@ need to connect points across multiple cases. \itemize{ \item \strong{\code{\link[=aes_position]{x}}} \item \strong{\code{\link[=aes_position]{y}}} -\item \strong{\code{\link[=aes_position]{xend}}} -\item \strong{\code{\link[=aes_position]{yend}}} +\item \strong{\code{\link[=aes_position]{xend}} \emph{or} \code{\link[=aes_position]{yend}}} \item \code{\link[=aes_colour_fill_alpha]{alpha}} \item \code{\link[=aes_colour_fill_alpha]{colour}} \item \code{\link[=aes_group_order]{group}}