Skip to content

Commit 1cab6a8

Browse files
Add option outline.type
1 parent 2b7b26c commit 1cab6a8

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

R/geom-ribbon.r

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#' [geom_polygon()] for general polygons
2020
#' @inheritParams layer
2121
#' @inheritParams geom_point
22+
#' @param outline.type Type of the outline of the area; `"both"` draws both the
23+
#' upper and lower lines, `"upper"` draws the upper lines only. `"legacy"`
24+
#' draws a closed polygon around the area.
2225
#' @export
2326
#' @examples
2427
#' # Generate data
@@ -37,7 +40,10 @@ geom_ribbon <- function(mapping = NULL, data = NULL,
3740
...,
3841
na.rm = FALSE,
3942
show.legend = NA,
40-
inherit.aes = TRUE) {
43+
inherit.aes = TRUE,
44+
outline.type = c("both", "upper", "legacy")) {
45+
outline.type <- match.arg(outline.type)
46+
4147
layer(
4248
data = data,
4349
mapping = mapping,
@@ -48,6 +54,7 @@ geom_ribbon <- function(mapping = NULL, data = NULL,
4854
inherit.aes = inherit.aes,
4955
params = list(
5056
na.rm = na.rm,
57+
outline.type = outline.type,
5158
...
5259
)
5360
)
@@ -78,7 +85,7 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
7885
data
7986
},
8087

81-
draw_group = function(data, panel_params, coord, na.rm = FALSE) {
88+
draw_group = function(data, panel_params, coord, na.rm = FALSE, outline.type = "both") {
8289
if (na.rm) data <- data[stats::complete.cases(data[c("x", "ymin", "ymax")]), ]
8390
data <- data[order(data$group), ]
8491

@@ -113,14 +120,21 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
113120
default.units = "native",
114121
gp = gpar(
115122
fill = alpha(aes$fill, aes$alpha),
116-
col = NA
123+
col = if (identical(outline.type, "legacy")) aes$colour else NA
117124
)
118125
)
119126

127+
if (identical(outline.type, "legacy")) {
128+
return(ggname("geom_ribbon", g_poly))
129+
}
130+
120131
munched_lines <- munched
121132
# increment the IDs of the lower line
122-
munched_lines$id <- munched_lines$id + rep(c(0, max(ids, na.rm = TRUE)), each = length(ids))
123-
133+
munched_lines$id <- switch(outline.type,
134+
both = munched_lines$id + rep(c(0, max(ids, na.rm = TRUE)), each = length(ids)),
135+
upper = munched_lines$id + rep(c(0, NA), each = length(ids)),
136+
abort(paste("inlvaid outline.type:", outline.type))
137+
)
124138
g_lines <- polylineGrob(
125139
munched_lines$x, munched_lines$y, id = munched_lines$id,
126140
default.units = "native",
@@ -132,13 +146,17 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
132146

133147
ggname("geom_ribbon", grobTree(g_poly, g_lines))
134148
}
149+
135150
)
136151

137152
#' @rdname geom_ribbon
138153
#' @export
139154
geom_area <- function(mapping = NULL, data = NULL, stat = "identity",
140155
position = "stack", na.rm = FALSE, show.legend = NA,
141-
inherit.aes = TRUE, ...) {
156+
inherit.aes = TRUE, ...,
157+
outline.type = c("upper", "both", "legacy")) {
158+
outline.type <- match.arg(outline.type)
159+
142160
layer(
143161
data = data,
144162
mapping = mapping,
@@ -149,6 +167,7 @@ geom_area <- function(mapping = NULL, data = NULL, stat = "identity",
149167
inherit.aes = inherit.aes,
150168
params = list(
151169
na.rm = na.rm,
170+
outline.type = outline.type,
152171
...
153172
)
154173
)

man/geom_ribbon.Rd

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

0 commit comments

Comments
 (0)