Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ggplot2 (development version)

* `coord_munch()` can now close polygon shapes (@teunbrand, #3271)
* The `layer_data()`, `layer_scales()` and `layer_grob()` now have the default
`plot = last_plot()` (@teunbrand, #5166).
* To prevent changing the plotting order, `stat_sf()` is now computed per panel
Expand Down
2 changes: 1 addition & 1 deletion R/annotation-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ GeomAnnotationMap <- ggproto("GeomAnnotationMap", GeomMap,
draw_panel = function(data, panel_params, coord, map) {
# Munch, then set up id variable for polygonGrob -
# must be sequential integers
coords <- coord_munch(coord, map, panel_params)
coords <- coord_munch(coord, map, panel_params, make_closed = TRUE)
coords$group <- coords$group %||% coords$id
grob_id <- match(coords$group, unique0(coords$group))

Expand Down
26 changes: 25 additions & 1 deletion R/coord-munch.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
#' All other variables are duplicated as needed.
#' @param range Panel range specification.
#' @param segment_length Target segment length
#' @param make_closed Whether data should be converted to closed polygons before
#' munching.
#' @keywords internal
#' @export
coord_munch <- function(coord, data, range, segment_length = 0.01) {
coord_munch <- function(coord, data, range, segment_length = 0.01, make_closed = FALSE) {
if (coord$is_linear()) return(coord$transform(data, range))

if (make_closed) {
data <- close_poly(data)
}

# range has theta and r values; get corresponding x and y values
ranges <- coord$backtransform_range(range)

Expand Down Expand Up @@ -204,3 +210,21 @@ spiral_arc_length <- function(a, theta1, theta2) {
(theta1 * sqrt(1 + theta1 * theta1) + asinh(theta1)) -
(theta2 * sqrt(1 + theta2 * theta2) + asinh(theta2)))
}

# Closes a polygon type data structure by repeating the first-in-group after
# the last-in-group
close_poly <- function(data) {
n <- nrow(data)
group <- vec_group_id(
data[, intersect(names(data), c("group", "subgroup"))]
)
if (length(group) != n) group <- rep(1L, n)
order <- order(group)
group <- group[order]
first <- which(c(TRUE, group[-1] != group[-n]))
index <- unlist(vec_interleave(
split(seq_len(n), group),
as.list(first)
))
data[order[index], , drop = FALSE]
}
2 changes: 1 addition & 1 deletion R/geom-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ GeomMap <- ggproto("GeomMap", GeomPolygon,

# Munch, then set up id variable for polygonGrob -
# must be sequential integers
coords <- coord_munch(coord, map, panel_params)
coords <- coord_munch(coord, map, panel_params, make_closed = TRUE)
coords$group <- coords$group %||% coords$id
grob_id <- match(coords$group, unique0(coords$group))

Expand Down
2 changes: 1 addition & 1 deletion R/geom-polygon.r
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ GeomPolygon <- ggproto("GeomPolygon", Geom,
n <- nrow(data)
if (n == 1) return(zeroGrob())

munched <- coord_munch(coord, data, panel_params)
munched <- coord_munch(coord, data, panel_params, make_closed = TRUE)

if (is.null(munched$subgroup)) {
# Sort by group to make sure that colors, fill, etc. come in same order
Expand Down
30 changes: 8 additions & 22 deletions R/geom-rect.r
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ GeomRect <- ggproto("GeomRect", Geom,
aesthetics <- setdiff(
names(data), c("x", "y", "xmin", "xmax", "ymin", "ymax")
)
index <- rep(seq_len(nrow(data)), each = 4)

polys <- lapply(split(data, seq_len(nrow(data))), function(row) {
poly <- rect_to_poly(row$xmin, row$xmax, row$ymin, row$ymax)
aes <- row[rep(1,5), aesthetics]
new <- data[index, aesthetics, drop = FALSE]
new$x <- vec_interleave(data$xmin, data$xmax, data$xmax, data$xmin)
new$y <- vec_interleave(data$ymax, data$ymax, data$ymin, data$ymin)
new$group <- index

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

ggname("geom_rect", inject(grobTree(!!!polys)))
ggname("geom_rect", GeomPolygon$draw_panel(
new, panel_params, coord, lineend = lineend, linejoin = linejoin
))
} else {
coords <- coord$transform(data, panel_params)
ggname("geom_rect", rectGrob(
Expand All @@ -72,18 +73,3 @@ GeomRect <- ggproto("GeomRect", Geom,

rename_size = TRUE
)


# Convert rectangle to polygon
# Useful for non-Cartesian coordinate systems where it's easy to work purely in
# terms of locations, rather than locations and dimensions. Note that, though
# `polygonGrob()` expects an open form, closed form is needed for correct
# munching (c.f. https://github.com/tidyverse/ggplot2/issues/3037#issuecomment-458406857).
#
# @keyword internal
rect_to_poly <- function(xmin, xmax, ymin, ymax) {
data_frame0(
y = c(ymax, ymax, ymin, ymin, ymax),
x = c(xmin, xmax, xmax, xmin, xmin)
)
}
5 changes: 4 additions & 1 deletion man/coord_munch.Rd

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 10 additions & 10 deletions tests/testthat/_snaps/coord-map/usa-mercator.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/geom-violin/coord-polar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.