diff --git a/NEWS.md b/NEWS.md index 9ae326659c..8a3a1f8e9d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 2.2.1.9000 +* `geom_segment` now also takes a `linejoin` parameter. This allows more control over the appearance of the segments, which is especially useful for plotting thick arrows (@Ax3man, #774). + * Theme elements can now be subclassed. Add a `merge_element` method to control how properties are inherited from parent element. Add `element_grob` method to define how elements are rendered into grobs (@thomasp85, #1981). diff --git a/R/geom-segment.r b/R/geom-segment.r index 90af43ee0a..a252c9eb33 100644 --- a/R/geom-segment.r +++ b/R/geom-segment.r @@ -15,6 +15,7 @@ #' @inheritParams geom_point #' @param arrow specification for arrow heads, as created by arrow(). #' @param lineend Line end style (round, butt, square). +#' @param linejoin Line join style (round, mitre, bevel). #' @seealso [geom_path()] and [geom_line()] for multi- #' segment lines and paths. #' @seealso [geom_spoke()] for a segment parameterised by a location @@ -42,6 +43,21 @@ #' arrow = arrow(length = unit(0.1,"cm"))) + #' borders("state") #' +#' # Use lineend and linejoin to change the style of the segments +#' df2 <- expand.grid( +#' lineend = c('round', 'butt', 'square'), +#' linejoin = c('round', 'mitre', 'bevel'), +#' stringsAsFactors = FALSE +#' ) +#' df2 <- data.frame(df2, y = 1:9) +#' ggplot(df2, aes(x = 1, y = y, xend = 2, yend = y, label = paste(lineend, linejoin))) + +#' geom_segment( +#' lineend = df2$lineend, linejoin = df2$linejoin, +#' size = 3, arrow = arrow(length = unit(0.3, "inches")) +#' ) + +#' geom_text(hjust = 'outside', nudge_x = -0.2) + +#' xlim(0.5, 2) +#' #' # You can also use geom_segment to recreate plot(type = "h") : #' counts <- as.data.frame(table(x = rpois(100,5))) #' counts$x <- as.numeric(as.character(counts$x)) @@ -54,6 +70,7 @@ geom_segment <- function(mapping = NULL, data = NULL, ..., arrow = NULL, lineend = "butt", + linejoin = "round", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) { @@ -68,6 +85,7 @@ geom_segment <- function(mapping = NULL, data = NULL, params = list( arrow = arrow, lineend = lineend, + linejoin = linejoin, na.rm = na.rm, ... ) @@ -84,7 +102,7 @@ GeomSegment <- ggproto("GeomSegment", Geom, default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA), draw_panel = function(data, panel_params, coord, arrow = NULL, - lineend = "butt", na.rm = FALSE) { + lineend = "butt", linejoin = "round", na.rm = FALSE) { data <- remove_missing(data, na.rm = na.rm, c("x", "y", "xend", "yend", "linetype", "size", "shape"), @@ -100,7 +118,8 @@ GeomSegment <- ggproto("GeomSegment", Geom, fill = alpha(coord$colour, coord$alpha), lwd = coord$size * .pt, lty = coord$linetype, - lineend = lineend + lineend = lineend, + linejoin = linejoin ), arrow = arrow )) diff --git a/man/geom_segment.Rd b/man/geom_segment.Rd index 3c8424bd9b..f3b9b4f572 100644 --- a/man/geom_segment.Rd +++ b/man/geom_segment.Rd @@ -7,7 +7,8 @@ \usage{ geom_segment(mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., arrow = NULL, lineend = "butt", - na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) + linejoin = "round", na.rm = FALSE, show.legend = NA, + inherit.aes = TRUE) geom_curve(mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., curvature = 0.5, angle = 90, ncp = 5, @@ -49,6 +50,8 @@ to the paired geom/stat.} \item{lineend}{Line end style (round, butt, square).} +\item{linejoin}{Line join style (round, mitre, bevel).} + \item{na.rm}{If \code{FALSE}, the default, missing values are removed with a warning. If \code{TRUE}, missing values are silently removed.} @@ -111,6 +114,21 @@ ggplot(seals, aes(long, lat)) + arrow = arrow(length = unit(0.1,"cm"))) + borders("state") +# Use lineend and linejoin to change the style of the segments +df2 <- expand.grid( + lineend = c('round', 'butt', 'square'), + linejoin = c('round', 'mitre', 'bevel'), + stringsAsFactors = FALSE +) +df2 <- data.frame(df2, y = 1:9) +ggplot(df2, aes(x = 1, y = y, xend = 2, yend = y, label = paste(lineend, linejoin))) + + geom_segment( + lineend = df2$lineend, linejoin = df2$linejoin, + size = 3, arrow = arrow(length = unit(0.3, "inches")) + ) + + geom_text(hjust = 'outside', nudge_x = -0.2) + + xlim(0.5, 2) + # You can also use geom_segment to recreate plot(type = "h") : counts <- as.data.frame(table(x = rpois(100,5))) counts$x <- as.numeric(as.character(counts$x))