Skip to content
Permalink
Browse files
Added linejoin parameter to geom_segment. (#2132)
* Added linejoin parameter to geom_segment (#774).

* Simplify example code.

* Changed indentation and moved data and aes to ggplot
  • Loading branch information
Ax3man authored and karawoo committed Jul 3, 2017
1 parent e5336a4 commit 1f25e57ef84303294b902d4ca4c7e5edb4b9abf6
Showing 3 changed files with 42 additions and 3 deletions.
@@ -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).
@@ -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
))

Some generated files are not rendered by default. Learn more.

0 comments on commit 1f25e57

Please sign in to comment.