Skip to content

Consistent use of lineend and linejoin in geoms and keys #4664

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 3 commits into from
Nov 10, 2021
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* All geoms now have consistent exposure of linejoin and lineend parameters, and
the guide keys will now respect these settings (@thomasp85, #4653)

* `geom_sf()` now respects `arrow` parameter for lines (@jakeruss, #4659)

* Updated documentation for `print.ggplot` to reflect that it returns
Expand Down
4 changes: 2 additions & 2 deletions R/geom-abline.r
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ geom_abline <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomAbline <- ggproto("GeomAbline", Geom,
draw_panel = function(data, panel_params, coord) {
draw_panel = function(data, panel_params, coord, lineend = "butt") {
ranges <- coord$backtransform_range(panel_params)

if (coord$clip == "on" && coord$is_linear()) {
Expand All @@ -138,7 +138,7 @@ GeomAbline <- ggproto("GeomAbline", Geom,
data$y <- ranges$x[1] * data$slope + data$intercept
data$yend <- ranges$x[2] * data$slope + data$intercept

GeomSegment$draw_panel(unique(data), panel_params, coord)
GeomSegment$draw_panel(unique(data), panel_params, coord, lineend = lineend)
},

default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
Expand Down
11 changes: 9 additions & 2 deletions R/geom-bar.r
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,15 @@ GeomBar <- ggproto("GeomBar", GeomRect,
flip_data(data, params$flipped_aes)
},

draw_panel = function(self, data, panel_params, coord, width = NULL, flipped_aes = FALSE) {
draw_panel = function(self, data, panel_params, coord, lineend = "butt",
linejoin = "mitre", width = NULL, flipped_aes = FALSE) {
# Hack to ensure that width is detected as a parameter
ggproto_parent(GeomRect, self)$draw_panel(data, panel_params, coord)
ggproto_parent(GeomRect, self)$draw_panel(
data,
panel_params,
coord,
lineend = lineend,
linejoin = linejoin
)
}
)
22 changes: 15 additions & 7 deletions R/geom-boxplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
flip_data(data, params$flipped_aes)
},

draw_group = function(data, panel_params, coord, fatten = 2,
outlier.colour = NULL, outlier.fill = NULL,
outlier.shape = 19,
draw_group = function(data, panel_params, coord, lineend = "butt",
linejoin = "mitre", fatten = 2, outlier.colour = NULL,
outlier.fill = NULL, outlier.shape = 19,
outlier.size = 1.5, outlier.stroke = 0.5,
outlier.alpha = NULL,
notch = FALSE, notchwidth = 0.5, varwidth = FALSE, flipped_aes = FALSE) {
outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5,
varwidth = FALSE, flipped_aes = FALSE) {
data <- flip_data(data, flipped_aes)
# this may occur when using geom_boxplot(stat = "identity")
if (nrow(data) != 1) {
Expand Down Expand Up @@ -274,8 +274,16 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,

ggname("geom_boxplot", grobTree(
outliers_grob,
GeomSegment$draw_panel(whiskers, panel_params, coord),
GeomCrossbar$draw_panel(box, fatten = fatten, panel_params, coord, flipped_aes = flipped_aes)
GeomSegment$draw_panel(whiskers, panel_params, coord, lineend = lineend),
GeomCrossbar$draw_panel(
box,
fatten = fatten,
panel_params,
coord,
lineend = lineend,
linejoin = linejoin,
flipped_aes = flipped_aes
)
))
},

Expand Down
11 changes: 9 additions & 2 deletions R/geom-col.r
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,15 @@ GeomCol <- ggproto("GeomCol", GeomRect,
flip_data(data, params$flipped_aes)
},

draw_panel = function(self, data, panel_params, coord, width = NULL, flipped_aes = FALSE) {
draw_panel = function(self, data, panel_params, coord, lineend = "butt",
linejoin = "mitre", width = NULL, flipped_aes = FALSE) {
# Hack to ensure that width is detected as a parameter
ggproto_parent(GeomRect, self)$draw_panel(data, panel_params, coord)
ggproto_parent(GeomRect, self)$draw_panel(
data,
panel_params,
coord,
lineend = lineend,
linejoin = linejoin
)
}
)
8 changes: 5 additions & 3 deletions R/geom-crossbar.r
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,

draw_key = draw_key_crossbar,

draw_panel = function(data, panel_params, coord, fatten = 2.5, width = NULL, flipped_aes = FALSE) {
draw_panel = function(data, panel_params, coord, lineend = "butt",
linejoin = "mitre", fatten = 2.5, width = NULL,
flipped_aes = FALSE) {
data <- flip_data(data, flipped_aes)

middle <- transform(data, x = xmin, xend = xmax, yend = y, size = size * fatten, alpha = NA)
Expand Down Expand Up @@ -99,8 +101,8 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
middle <- flip_data(middle, flipped_aes)

ggname("geom_crossbar", gTree(children = gList(
GeomPolygon$draw_panel(box, panel_params, coord),
GeomSegment$draw_panel(middle, panel_params, coord)
GeomPolygon$draw_panel(box, panel_params, coord, lineend = lineend, linejoin = linejoin),
GeomSegment$draw_panel(middle, panel_params, coord, lineend = lineend, linejoin = linejoin)
)))
}
)
5 changes: 3 additions & 2 deletions R/geom-dotplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,
},


draw_group = function(data, panel_params, coord, na.rm = FALSE,
draw_group = function(data, panel_params, coord, lineend = "butt", na.rm = FALSE,
binaxis = "x", stackdir = "up", stackratio = 1,
dotsize = 1, stackgroups = FALSE) {
if (!coord$is_linear()) {
Expand Down Expand Up @@ -292,7 +292,8 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,
default.units = "npc",
gp = gpar(col = alpha(tdata$colour, tdata$alpha),
fill = alpha(tdata$fill, tdata$alpha),
lwd = tdata$stroke, lty = tdata$linetype))
lwd = tdata$stroke, lty = tdata$linetype,
lineend = lineend))
)
},

Expand Down
4 changes: 2 additions & 2 deletions R/geom-errorbar.r
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ GeomErrorbar <- ggproto("GeomErrorbar", Geom,
flip_data(data, params$flipped_aes)
},

draw_panel = function(data, panel_params, coord, width = NULL, flipped_aes = FALSE) {
draw_panel = function(data, panel_params, coord, lineend = "butt", width = NULL, flipped_aes = FALSE) {
data <- flip_data(data, flipped_aes)
x <- as.vector(rbind(data$xmin, data$xmax, NA, data$x, data$x, NA, data$xmin, data$xmax))
y <- as.vector(rbind(data$ymax, data$ymax, NA, data$ymax, data$ymin, NA, data$ymin, data$ymin))
Expand All @@ -67,6 +67,6 @@ GeomErrorbar <- ggproto("GeomErrorbar", Geom,
row.names = 1:(nrow(data) * 8)
))
data <- flip_data(data, flipped_aes)
GeomPath$draw_panel(data, panel_params, coord)
GeomPath$draw_panel(data, panel_params, coord, lineend = lineend)
}
)
4 changes: 2 additions & 2 deletions R/geom-errorbarh.r
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ GeomErrorbarh <- ggproto("GeomErrorbarh", Geom,
)
},

draw_panel = function(data, panel_params, coord, height = NULL) {
draw_panel = function(data, panel_params, coord, height = NULL, lineend = "butt") {
GeomPath$draw_panel(new_data_frame(list(
x = as.vector(rbind(data$xmax, data$xmax, NA, data$xmax, data$xmin, NA, data$xmin, data$xmin)),
y = as.vector(rbind(data$ymin, data$ymax, NA, data$y, data$y, NA, data$ymin, data$ymax)),
Expand All @@ -77,6 +77,6 @@ GeomErrorbarh <- ggproto("GeomErrorbarh", Geom,
linetype = rep(data$linetype, each = 8),
group = rep(1:(nrow(data)), each = 8),
row.names = 1:(nrow(data) * 8)
)), panel_params, coord)
)), panel_params, coord, lineend = lineend)
}
)
8 changes: 6 additions & 2 deletions R/geom-hex.r
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ geom_hex <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomHex <- ggproto("GeomHex", Geom,
draw_group = function(data, panel_params, coord) {
draw_group = function(data, panel_params, coord, lineend = "butt",
linejoin = "mitre", linemitre = 10) {
if (!inherits(coord, "CoordCartesian")) {
abort("geom_hex() only works with Cartesian coordinates")
}
Expand All @@ -66,7 +67,10 @@ GeomHex <- ggproto("GeomHex", Geom,
col = coords$colour,
fill = alpha(coords$fill, coords$alpha),
lwd = coords$size * .pt,
lty = coords$linetype
lty = coords$linetype,
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
)
))
},
Expand Down
4 changes: 2 additions & 2 deletions R/geom-hline.r
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ geom_hline <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomHline <- ggproto("GeomHline", Geom,
draw_panel = function(data, panel_params, coord) {
draw_panel = function(data, panel_params, coord, lineend = "butt") {
ranges <- coord$backtransform_range(panel_params)

data$x <- ranges$x[1]
data$xend <- ranges$x[2]
data$y <- data$yintercept
data$yend <- data$yintercept

GeomSegment$draw_panel(unique(data), panel_params, coord)
GeomSegment$draw_panel(unique(data), panel_params, coord, lineend = lineend)
},

default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
Expand Down
4 changes: 2 additions & 2 deletions R/geom-linerange.r
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ GeomLinerange <- ggproto("GeomLinerange", Geom,
data
},

draw_panel = function(data, panel_params, coord, flipped_aes = FALSE) {
draw_panel = function(data, panel_params, coord, lineend = "butt", flipped_aes = FALSE) {
data <- flip_data(data, flipped_aes)
data <- transform(data, xend = x, y = ymin, yend = ymax)
data <- flip_data(data, flipped_aes)
ggname("geom_linerange", GeomSegment$draw_panel(data, panel_params, coord))
ggname("geom_linerange", GeomSegment$draw_panel(data, panel_params, coord, lineend = lineend))
}
)
11 changes: 8 additions & 3 deletions R/geom-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ geom_map <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomMap <- ggproto("GeomMap", GeomPolygon,
draw_panel = function(data, panel_params, coord, map) {
draw_panel = function(data, panel_params, coord, lineend = "butt",
linejoin = "round", linemitre = 10, map) {
# Only use matching data and map ids
common <- intersect(data$map_id, map$id)
data <- data[data$map_id %in% common, , drop = FALSE]
Expand All @@ -123,8 +124,12 @@ GeomMap <- ggproto("GeomMap", GeomPolygon,

polygonGrob(coords$x, coords$y, default.units = "native", id = grob_id,
gp = gpar(
col = data$colour, fill = alpha(data$fill, data$alpha),
lwd = data$size * .pt
col = data$colour,
fill = alpha(data$fill, data$alpha),
lwd = data$size * .pt,
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
)
)
},
Expand Down
6 changes: 3 additions & 3 deletions R/geom-pointrange.r
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ GeomPointrange <- ggproto("GeomPointrange", Geom,
GeomLinerange$setup_data(data, params)
},

draw_panel = function(data, panel_params, coord, fatten = 4, flipped_aes = FALSE) {
draw_panel = function(data, panel_params, coord, lineend = "butt", fatten = 4, flipped_aes = FALSE) {
if (is.null(data[[flipped_names(flipped_aes)$y]]))
return(GeomLinerange$draw_panel(data, panel_params, coord, flipped_aes = flipped_aes))
return(GeomLinerange$draw_panel(data, panel_params, coord, lineend = lineend, flipped_aes = flipped_aes))

ggname("geom_pointrange",
gTree(children = gList(
GeomLinerange$draw_panel(data, panel_params, coord, flipped_aes = flipped_aes),
GeomLinerange$draw_panel(data, panel_params, coord, lineend = lineend, flipped_aes = flipped_aes),
GeomPoint$draw_panel(transform(data, size = size * fatten), panel_params, coord)
))
)
Expand Down
13 changes: 10 additions & 3 deletions R/geom-polygon.r
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ geom_polygon <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomPolygon <- ggproto("GeomPolygon", Geom,
draw_panel = function(data, panel_params, coord, rule = "evenodd") {
draw_panel = function(data, panel_params, coord, rule = "evenodd", lineend = "butt",
linejoin = "round", linemitre = 10) {
n <- nrow(data)
if (n == 1) return(zeroGrob())

Expand All @@ -131,7 +132,10 @@ GeomPolygon <- ggproto("GeomPolygon", Geom,
col = first_rows$colour,
fill = alpha(first_rows$fill, first_rows$alpha),
lwd = first_rows$size * .pt,
lty = first_rows$linetype
lty = first_rows$linetype,
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
)
)
)
Expand Down Expand Up @@ -159,7 +163,10 @@ GeomPolygon <- ggproto("GeomPolygon", Geom,
col = first_rows$colour,
fill = alpha(first_rows$fill, first_rows$alpha),
lwd = first_rows$size * .pt,
lty = first_rows$linetype
lty = first_rows$linetype,
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
)
)
)
Expand Down
8 changes: 3 additions & 5 deletions R/geom-rect.r
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ GeomRect <- ggproto("GeomRect", Geom,

required_aes = c("xmin", "xmax", "ymin", "ymax"),

draw_panel = function(self, data, panel_params, coord, linejoin = "mitre") {
draw_panel = function(self, data, panel_params, coord, lineend = "butt", linejoin = "mitre") {
if (!coord$is_linear()) {
aesthetics <- setdiff(
names(data), c("x", "y", "xmin", "xmax", "ymin", "ymax")
Expand All @@ -43,7 +43,7 @@ GeomRect <- ggproto("GeomRect", Geom,
poly <- rect_to_poly(row$xmin, row$xmax, row$ymin, row$ymax)
aes <- new_data_frame(row[aesthetics])[rep(1,5), ]

GeomPolygon$draw_panel(cbind(poly, aes), panel_params, coord)
GeomPolygon$draw_panel(cbind(poly, aes), panel_params, coord, lineend = lineend, linejoin = linejoin)
})

ggname("bar", do.call("grobTree", polys))
Expand All @@ -61,9 +61,7 @@ GeomRect <- ggproto("GeomRect", Geom,
lwd = coords$size * .pt,
lty = coords$linetype,
linejoin = linejoin,
# `lineend` is a workaround for Windows and intentionally kept unexposed
# as an argument. (c.f. https://github.com/tidyverse/ggplot2/issues/3037#issuecomment-457504667)
lineend = if (identical(linejoin, "round")) "round" else "square"
lineend = lineend
)
))
}
Expand Down
15 changes: 12 additions & 3 deletions R/geom-ribbon.r
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
data
},

draw_group = function(data, panel_params, coord, na.rm = FALSE, flipped_aes = FALSE, outline.type = "both") {
draw_group = function(data, panel_params, coord, lineend = "butt",
linejoin = "round", linemitre = 10, na.rm = FALSE,
flipped_aes = FALSE, outline.type = "both") {
data <- flip_data(data, flipped_aes)
if (na.rm) data <- data[stats::complete.cases(data[c("x", "ymin", "ymax")]), ]
data <- data[order(data$group), ]
Expand Down Expand Up @@ -158,7 +160,10 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
fill = alpha(aes$fill, aes$alpha),
col = if (is_full_outline) aes$colour else NA,
lwd = if (is_full_outline) aes$size * .pt else 0,
lty = if (is_full_outline) aes$linetype else 1
lty = if (is_full_outline) aes$linetype else 1,
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
)
)

Expand All @@ -181,7 +186,11 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
gp = gpar(
col = aes$colour,
lwd = aes$size * .pt,
lty = aes$linetype)
lty = aes$linetype,
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
)
)

ggname("geom_ribbon", grobTree(g_poly, g_lines))
Expand Down
10 changes: 8 additions & 2 deletions R/geom-rug.r
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ geom_rug <- function(mapping = NULL, data = NULL,
GeomRug <- ggproto("GeomRug", Geom,
optional_aes = c("x", "y"),

draw_panel = function(data, panel_params, coord, sides = "bl", outside = FALSE, length = unit(0.03, "npc")) {
draw_panel = function(data, panel_params, coord, lineend = "butt", sides = "bl",
outside = FALSE, length = unit(0.03, "npc")) {
if (!inherits(length, "unit")) {
abort("'length' must be a 'unit' object.")
}
Expand All @@ -108,7 +109,12 @@ GeomRug <- ggproto("GeomRug", Geom,
list(min = -1 * length, max = unit(1, "npc") + length)
}

gp <- gpar(col = alpha(data$colour, data$alpha), lty = data$linetype, lwd = data$size * .pt)
gp <- gpar(
col = alpha(data$colour, data$alpha),
lty = data$linetype,
lwd = data$size * .pt,
lineend = lineend
)
if (!is.null(data$x)) {
if (grepl("b", sides)) {
rugs$x_b <- segmentsGrob(
Expand Down
Loading