Skip to content

Commit 3b69172

Browse files
yutannihilationthomasp85
authored andcommitted
Add lineend and linejoin params to geom_rect() and geom_tile() (#3050)
1 parent 981215e commit 3b69172

23 files changed

+118
-88
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ core developer team.
161161
* `geom_*()` and `stat_*()` now accepts purrr-style lambda notation
162162
(@yutannihilation, #3138).
163163

164+
* `geom_tile()` and `geom_rect()` now draw rectangles without notches at the
165+
corners. The style of the corner can be controlled by `linejoin` parameters
166+
(@yutannihilation, #3050).
167+
164168
# ggplot2 3.1.0
165169

166170
## Breaking changes

R/geom-rect.r

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
geom_rect <- function(mapping = NULL, data = NULL,
44
stat = "identity", position = "identity",
55
...,
6+
linejoin = "mitre",
67
na.rm = FALSE,
78
show.legend = NA,
89
inherit.aes = TRUE) {
@@ -15,6 +16,7 @@ geom_rect <- function(mapping = NULL, data = NULL,
1516
show.legend = show.legend,
1617
inherit.aes = inherit.aes,
1718
params = list(
19+
linejoin = linejoin,
1820
na.rm = na.rm,
1921
...
2022
)
@@ -31,7 +33,7 @@ GeomRect <- ggproto("GeomRect", Geom,
3133

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

34-
draw_panel = function(self, data, panel_params, coord) {
36+
draw_panel = function(self, data, panel_params, coord, linejoin = "mitre") {
3537
if (!coord$is_linear()) {
3638
aesthetics <- setdiff(
3739
names(data), c("x", "y", "xmin", "xmax", "ymin", "ymax")
@@ -58,7 +60,10 @@ GeomRect <- ggproto("GeomRect", Geom,
5860
fill = alpha(coords$fill, coords$alpha),
5961
lwd = coords$size * .pt,
6062
lty = coords$linetype,
61-
lineend = "butt"
63+
linejoin = linejoin,
64+
# `lineend` is a workaround for Windows and intentionally kept unexposed
65+
# as an argument. (c.f. https://github.com/tidyverse/ggplot2/issues/3037#issuecomment-457504667)
66+
lineend = if (identical(linejoin, "round")) "round" else "square"
6267
)
6368
))
6469
}
@@ -69,7 +74,10 @@ GeomRect <- ggproto("GeomRect", Geom,
6974

7075

7176
# Convert rectangle to polygon
72-
# Useful for non-Cartesian coordinate systems where it's easy to work purely in terms of locations, rather than locations and dimensions.
77+
# Useful for non-Cartesian coordinate systems where it's easy to work purely in
78+
# terms of locations, rather than locations and dimensions. Note that, though
79+
# `polygonGrob()` expects an open form, closed form is needed for correct
80+
# munching (c.f. https://github.com/tidyverse/ggplot2/issues/3037#issuecomment-458406857).
7381
#
7482
# @keyword internal
7583
rect_to_poly <- function(xmin, xmax, ymin, ymax) {

R/geom-tile.r

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#' @eval rd_aesthetics("geom", "tile")
1111
#' @inheritParams layer
1212
#' @inheritParams geom_point
13+
#' @inheritParams geom_segment
1314
#' @export
1415
#' @examples
1516
#' # The most common use for rectangles is to draw a surface. You always want
@@ -57,6 +58,7 @@
5758
geom_tile <- function(mapping = NULL, data = NULL,
5859
stat = "identity", position = "identity",
5960
...,
61+
linejoin = "mitre",
6062
na.rm = FALSE,
6163
show.legend = NA,
6264
inherit.aes = TRUE) {
@@ -69,6 +71,7 @@ geom_tile <- function(mapping = NULL, data = NULL,
6971
show.legend = show.legend,
7072
inherit.aes = inherit.aes,
7173
params = list(
74+
linejoin = linejoin,
7275
na.rm = na.rm,
7376
...
7477
)

R/legend-draw.r

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ draw_key_polygon <- function(data, params, size) {
8080
fill = alpha(data$fill %||% "grey20", data$alpha),
8181
lty = data$linetype %||% 1,
8282
lwd = lwd * .pt,
83-
linejoin = "mitre"
83+
linejoin = params$linejoin %||% "mitre",
84+
# `lineend` is a workaround for Windows and intentionally kept unexposed
85+
# as an argument. (c.f. https://github.com/tidyverse/ggplot2/issues/3037#issuecomment-457504667)
86+
lineend = if (identical(params$linejoin, "round")) "round" else "square"
8487
))
8588
}
8689

man/geom_tile.Rd

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/figs/creating-aesthetic-mappings/stat-count-width-0-5.svg

Lines changed: 3 additions & 3 deletions
Loading

tests/figs/creating-aesthetic-mappings/stat-count.svg

Lines changed: 3 additions & 3 deletions
Loading

tests/figs/creating-aesthetic-mappings/stat-identity-width-0-5.svg

Lines changed: 3 additions & 3 deletions
Loading

tests/figs/creating-aesthetic-mappings/stat-identity.svg

Lines changed: 3 additions & 3 deletions
Loading

tests/figs/geom-hline-vline-abline/cartesian-lines-intersect-mid-bars.svg

Lines changed: 5 additions & 5 deletions
Loading

tests/figs/geom-hline-vline-abline/flipped-lines-intersect-mid-bars.svg

Lines changed: 5 additions & 5 deletions
Loading

tests/figs/geom-violin/dodging-and-coord-flip.svg

Lines changed: 3 additions & 3 deletions
Loading

0 commit comments

Comments
 (0)