@@ -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(
0 commit comments