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 2 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).

* `geom_smooth` now orders by the `x` aesthetic, making it easier to pass
pre-computed values without manual ordering (@izahn, #2028).

Expand Down
20 changes: 18 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 \code{\link{geom_path}} and \code{\link{geom_line}} for multi-
#' segment lines and paths.
#' @seealso \code{\link{geom_spoke}} for a segment parameterised by a location
Expand Down Expand Up @@ -42,6 +43,18 @@
#' 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() +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use house indenting rules? (http://style.tidyverse.org/syntax.html#long-lines)

#' geom_segment(aes(x = 1, y = y, xend = 2, yend = y), df2, size = 4,
#' lineend = df2$lineend, linejoin = df2$linejoin,
#' arrow = arrow(length = unit(0.5, "inches"))) +
#' geom_text(aes(x = 2, y = 1:9, label = paste(df2$lineend, df2$linejoin)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can simplify this plot by setting data and aes(y = y) in the ggplot2 call. The geom_text() layer shouldn't use df$

#' hjust = 'outside', nudge_x = 0.2) +
#' xlim(1, 2.25)
#'
#' # 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 +67,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 +82,7 @@ geom_segment <- function(mapping = NULL, data = NULL,
params = list(
arrow = arrow,
lineend = lineend,
linejoin = linejoin,
na.rm = na.rm,
...
)
Expand All @@ -84,7 +99,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 +115,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
17 changes: 16 additions & 1 deletion man/geom_segment.Rd

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