Skip to content

Commit 323af07

Browse files
Fix geom_ribbon() on non-cartesian Coords (#4025)
* Process upper and lower lines separately * Update test expectations
1 parent 4826838 commit 323af07

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

R/geom-ribbon.r

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,31 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
128128
ids[missing_pos] <- NA
129129

130130
data <- unclass(data) #for faster indexing
131-
positions <- new_data_frame(list(
132-
x = c(data$x, rev(data$x)),
133-
y = c(data$ymax, rev(data$ymin)),
134-
id = c(ids, rev(ids))
131+
132+
# The upper line and lower line need to processed separately (#4023)
133+
positions_upper <- new_data_frame(list(
134+
x = data$x,
135+
y = data$ymax,
136+
id = ids
137+
))
138+
139+
positions_lower <- new_data_frame(list(
140+
x = rev(data$x),
141+
y = rev(data$ymin),
142+
id = rev(ids)
135143
))
136144

137-
positions <- flip_data(positions, flipped_aes)
145+
positions_upper <- flip_data(positions_upper, flipped_aes)
146+
positions_lower <- flip_data(positions_lower, flipped_aes)
138147

139-
munched <- coord_munch(coord, positions, panel_params)
148+
munched_upper <- coord_munch(coord, positions_upper, panel_params)
149+
munched_lower <- coord_munch(coord, positions_lower, panel_params)
150+
151+
munched_poly <- rbind(munched_upper, munched_lower)
140152

141153
is_full_outline <- identical(outline.type, "full")
142154
g_poly <- polygonGrob(
143-
munched$x, munched$y, id = munched$id,
155+
munched_poly$x, munched_poly$y, id = munched_poly$id,
144156
default.units = "native",
145157
gp = gpar(
146158
fill = alpha(aes$fill, aes$alpha),
@@ -154,12 +166,13 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
154166
return(ggname("geom_ribbon", g_poly))
155167
}
156168

157-
munched_lines <- munched
158-
# increment the IDs of the lower line
159-
munched_lines$id <- switch(outline.type,
160-
both = munched_lines$id + rep(c(0, max(ids, na.rm = TRUE)), each = length(ids)),
161-
upper = munched_lines$id + rep(c(0, NA), each = length(ids)),
162-
lower = munched_lines$id + rep(c(NA, 0), each = length(ids)),
169+
# Increment the IDs of the lower line so that they will be drawn as separate lines
170+
munched_lower$id <- munched_lower$id + max(ids, na.rm = TRUE)
171+
172+
munched_lines <- switch(outline.type,
173+
both = rbind(munched_upper, munched_lower),
174+
upper = munched_upper,
175+
lower = munched_lower,
163176
abort(glue("invalid outline.type: {outline.type}"))
164177
)
165178
g_lines <- polylineGrob(

tests/testthat/test-geom-ribbon.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ test_that("outline.type option works", {
4646
# upper
4747
expect_s3_class(g_ribbon_upper$children[[1]]$children[[1]], "polygon")
4848
expect_s3_class(g_ribbon_upper$children[[1]]$children[[2]], "polyline")
49-
expect_equal(g_ribbon_upper$children[[1]]$children[[2]]$id, rep(c(1L, NA), each = 4))
49+
expect_equal(g_ribbon_upper$children[[1]]$children[[2]]$id, rep(1L, each = 4))
5050

5151
# lower
5252
expect_s3_class(g_ribbon_lower$children[[1]]$children[[1]], "polygon")
5353
expect_s3_class(g_ribbon_lower$children[[1]]$children[[2]], "polyline")
54-
expect_equal(g_ribbon_lower$children[[1]]$children[[2]]$id, rep(c(NA, 1L), each = 4))
54+
expect_equal(g_ribbon_lower$children[[1]]$children[[2]]$id, rep(2L, each = 4))
5555

5656
# full
5757
expect_s3_class(g_ribbon_full$children[[1]], "polygon")
5858

5959
# geom_area()'s default is upper
6060
expect_s3_class(g_area_default$children[[1]]$children[[1]], "polygon")
6161
expect_s3_class(g_area_default$children[[1]]$children[[2]], "polyline")
62-
expect_equal(g_area_default$children[[1]]$children[[2]]$id, rep(c(1L, NA), each = 4))
62+
expect_equal(g_area_default$children[[1]]$children[[2]]$id, rep(1L, each = 4))
6363
})

0 commit comments

Comments
 (0)