Skip to content

Added linejoin parameter to geom_segment. #2132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
23 changes: 21 additions & 2 deletions R/geom-segment.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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) {
Expand All @@ -68,6 +85,7 @@ geom_segment <- function(mapping = NULL, data = NULL,
params = list(
arrow = arrow,
lineend = lineend,
linejoin = linejoin,
na.rm = na.rm,
...
)
Expand All @@ -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"),
Expand All @@ -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
))
Expand Down
20 changes: 19 additions & 1 deletion man/geom_segment.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.