Skip to content

Commit 1f25e57

Browse files
Ax3mankarawoo
authored andcommitted
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
1 parent e5336a4 commit 1f25e57

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 2.2.1.9000
22

3+
* `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).
4+
35
* Theme elements can now be subclassed. Add a `merge_element` method to control
46
how properties are inherited from parent element. Add `element_grob` method
57
to define how elements are rendered into grobs (@thomasp85, #1981).

R/geom-segment.r

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#' @inheritParams geom_point
1616
#' @param arrow specification for arrow heads, as created by arrow().
1717
#' @param lineend Line end style (round, butt, square).
18+
#' @param linejoin Line join style (round, mitre, bevel).
1819
#' @seealso [geom_path()] and [geom_line()] for multi-
1920
#' segment lines and paths.
2021
#' @seealso [geom_spoke()] for a segment parameterised by a location
@@ -42,6 +43,21 @@
4243
#' arrow = arrow(length = unit(0.1,"cm"))) +
4344
#' borders("state")
4445
#'
46+
#' # Use lineend and linejoin to change the style of the segments
47+
#' df2 <- expand.grid(
48+
#' lineend = c('round', 'butt', 'square'),
49+
#' linejoin = c('round', 'mitre', 'bevel'),
50+
#' stringsAsFactors = FALSE
51+
#' )
52+
#' df2 <- data.frame(df2, y = 1:9)
53+
#' ggplot(df2, aes(x = 1, y = y, xend = 2, yend = y, label = paste(lineend, linejoin))) +
54+
#' geom_segment(
55+
#' lineend = df2$lineend, linejoin = df2$linejoin,
56+
#' size = 3, arrow = arrow(length = unit(0.3, "inches"))
57+
#' ) +
58+
#' geom_text(hjust = 'outside', nudge_x = -0.2) +
59+
#' xlim(0.5, 2)
60+
#'
4561
#' # You can also use geom_segment to recreate plot(type = "h") :
4662
#' counts <- as.data.frame(table(x = rpois(100,5)))
4763
#' counts$x <- as.numeric(as.character(counts$x))
@@ -54,6 +70,7 @@ geom_segment <- function(mapping = NULL, data = NULL,
5470
...,
5571
arrow = NULL,
5672
lineend = "butt",
73+
linejoin = "round",
5774
na.rm = FALSE,
5875
show.legend = NA,
5976
inherit.aes = TRUE) {
@@ -68,6 +85,7 @@ geom_segment <- function(mapping = NULL, data = NULL,
6885
params = list(
6986
arrow = arrow,
7087
lineend = lineend,
88+
linejoin = linejoin,
7189
na.rm = na.rm,
7290
...
7391
)
@@ -84,7 +102,7 @@ GeomSegment <- ggproto("GeomSegment", Geom,
84102
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
85103

86104
draw_panel = function(data, panel_params, coord, arrow = NULL,
87-
lineend = "butt", na.rm = FALSE) {
105+
lineend = "butt", linejoin = "round", na.rm = FALSE) {
88106

89107
data <- remove_missing(data, na.rm = na.rm,
90108
c("x", "y", "xend", "yend", "linetype", "size", "shape"),
@@ -100,7 +118,8 @@ GeomSegment <- ggproto("GeomSegment", Geom,
100118
fill = alpha(coord$colour, coord$alpha),
101119
lwd = coord$size * .pt,
102120
lty = coord$linetype,
103-
lineend = lineend
121+
lineend = lineend,
122+
linejoin = linejoin
104123
),
105124
arrow = arrow
106125
))

man/geom_segment.Rd

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)