19
19
# ' [geom_polygon()] for general polygons
20
20
# ' @inheritParams layer
21
21
# ' @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.
22
25
# ' @export
23
26
# ' @examples
24
27
# ' # Generate data
@@ -37,7 +40,10 @@ geom_ribbon <- function(mapping = NULL, data = NULL,
37
40
... ,
38
41
na.rm = FALSE ,
39
42
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
+
41
47
layer(
42
48
data = data ,
43
49
mapping = mapping ,
@@ -48,6 +54,7 @@ geom_ribbon <- function(mapping = NULL, data = NULL,
48
54
inherit.aes = inherit.aes ,
49
55
params = list (
50
56
na.rm = na.rm ,
57
+ outline.type = outline.type ,
51
58
...
52
59
)
53
60
)
@@ -78,7 +85,7 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
78
85
data
79
86
},
80
87
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 " ) {
82
89
if (na.rm ) data <- data [stats :: complete.cases(data [c(" x" , " ymin" , " ymax" )]), ]
83
90
data <- data [order(data $ group ), ]
84
91
@@ -113,14 +120,21 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
113
120
default.units = " native" ,
114
121
gp = gpar(
115
122
fill = alpha(aes $ fill , aes $ alpha ),
116
- col = NA
123
+ col = if (identical( outline.type , " legacy " )) aes $ colour else NA
117
124
)
118
125
)
119
126
127
+ if (identical(outline.type , " legacy" )) {
128
+ return (ggname(" geom_ribbon" , g_poly ))
129
+ }
130
+
120
131
munched_lines <- munched
121
132
# 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
+ )
124
138
g_lines <- polylineGrob(
125
139
munched_lines $ x , munched_lines $ y , id = munched_lines $ id ,
126
140
default.units = " native" ,
@@ -132,13 +146,17 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
132
146
133
147
ggname(" geom_ribbon" , grobTree(g_poly , g_lines ))
134
148
}
149
+
135
150
)
136
151
137
152
# ' @rdname geom_ribbon
138
153
# ' @export
139
154
geom_area <- function (mapping = NULL , data = NULL , stat = " identity" ,
140
155
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
+
142
160
layer(
143
161
data = data ,
144
162
mapping = mapping ,
@@ -149,6 +167,7 @@ geom_area <- function(mapping = NULL, data = NULL, stat = "identity",
149
167
inherit.aes = inherit.aes ,
150
168
params = list (
151
169
na.rm = na.rm ,
170
+ outline.type = outline.type ,
152
171
...
153
172
)
154
173
)
0 commit comments