Skip to content

Commit a8e9668

Browse files
authored
Sort ribbon/area in setup_data (#3023)
* Make GeomRibbon/GeomArea sort the data in setup_data in the same manner as GeomLine
1 parent 23a23cd commit a8e9668

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
* ggplot2 now works in Turkish locale (@yutannihilation, #3011).
2323

24+
* `GeomRibbon` and `GeomArea` now sort the data along the x-axis in the
25+
`setup_data()` method rather than as part of `draw_group()` (@thomasp85, #3023)
26+
2427
* `coord_sf()`, `coord_map()`, and `coord_polar()` now squash `-Inf` and `Inf`
2528
into the min and max of the plot (@yutannihilation, #2972).
2629

R/geom-ribbon.r

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
6262

6363
required_aes = c("x", "ymin", "ymax"),
6464

65+
setup_data = function(data, params) {
66+
transform(data[order(data$PANEL, data$group, data$x), ], y = ymin)
67+
},
68+
6569
draw_key = draw_key_polygon,
6670

6771
handle_na = function(data, params) {
@@ -70,7 +74,7 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
7074

7175
draw_group = function(data, panel_params, coord, na.rm = FALSE) {
7276
if (na.rm) data <- data[stats::complete.cases(data[c("x", "ymin", "ymax")]), ]
73-
data <- data[order(data$group, data$x), ]
77+
data <- data[order(data$group), ]
7478

7579
# Check that aesthetics are constant
7680
aes <- unique(data[c("colour", "fill", "size", "linetype", "alpha")])
@@ -137,6 +141,6 @@ GeomArea <- ggproto("GeomArea", GeomRibbon,
137141
required_aes = c("x", "y"),
138142

139143
setup_data = function(data, params) {
140-
transform(data, ymin = 0, ymax = y)
144+
transform(data[order(data$PANEL, data$group, data$x), ], ymin = 0, ymax = y)
141145
}
142146
)

0 commit comments

Comments
 (0)