@@ -26,13 +26,15 @@ layer2traces <- function(l, d, misc) {
2626 # geom_smooth() means geom_line() + geom_ribbon()
2727 # Note the line is always drawn, but ribbon is not if se = FALSE.
2828 if (g $ geom == " smooth" ) {
29- # If smoothLine has been compiled already, consider smoothRibbon.
29+ # If smoothLine has been compiled already, consider drawing the ribbon
3030 if (isTRUE(misc $ smoothLine )) {
3131 misc $ smoothLine <- FALSE
3232 if (isTRUE(l $ stat_params $ se == FALSE )) {
3333 return (NULL )
3434 } else {
3535 g $ geom <- " smoothRibbon"
36+ # disregard colour
37+ g $ data <- g $ data [! grepl(" ^colour[.name]?" , names(g $ data ))]
3638 }
3739 } else {
3840 misc $ smoothLine <- TRUE
@@ -248,7 +250,6 @@ layer2traces <- function(l, d, misc) {
248250 if (length(unique(name.list )) < 2 )
249251 tr $ name <- as.character(name.list [[1 ]])
250252 }
251-
252253 dpd <- data.params $ data
253254 if (" PANEL" %in% names(dpd ) && nrow(dpd ) > 0 )
254255 {
@@ -335,6 +336,11 @@ toBasic <- list(
335336 g $ geom <- " polygon"
336337 g
337338 },
339+ ribbon = function (g ) {
340+ g $ data <- ribbon_dat(g $ data )
341+ g $ geom <- " polygon"
342+ g
343+ },
338344 path = function (g ) {
339345 group2NA(g , " path" )
340346 },
@@ -406,12 +412,15 @@ toBasic <- list(
406412 g
407413 },
408414 smoothLine = function (g ) {
409- if (length(unique(g $ data $ group )) == 1 ) g $ params $ colour <- " #3366FF"
415+ if (length(grep(" ^colour$" , names(g $ data ))) == 0 )
416+ g $ params $ colour <- " #3366FF"
410417 group2NA(g , " path" )
411418 },
412419 smoothRibbon = function (g ) {
413- if (is.null(g $ params $ alpha )) g $ params $ alpha <- 0.1
414- group2NA(g , " ribbon" )
420+ if (is.null(g $ params $ alpha )) g $ params $ alpha <- 0.2
421+ g $ data <- ribbon_dat(g $ data )
422+ g $ geom <- " polygon"
423+ g
415424 }
416425)
417426
@@ -493,6 +502,26 @@ make.errorbar <- function(data, params, xy){
493502 tr
494503}
495504
505+ # function to transform geom_ribbon data into format plotly likes
506+ # (note this function is also used for geom_smooth)
507+ ribbon_dat <- function (dat ) {
508+ n <- nrow(dat )
509+ o <- order(dat $ x )
510+ o2 <- order(dat $ x , decreasing = TRUE )
511+ used <- c(" x" , " ymin" , " ymax" )
512+ not_used <- setdiff(names(dat ), used )
513+ # top-half of ribbon
514+ tmp <- dat [o , ]
515+ others <- tmp [not_used ]
516+ dat1 <- cbind(x = tmp $ x , y = tmp $ ymax , others )
517+ dat1 [n + 1 , ] <- cbind(x = tmp $ x [n ], y = tmp $ ymin [n ], others [n , ])
518+ # bottom-half of ribbon
519+ tmp2 <- dat [o2 , ]
520+ others2 <- tmp2 [not_used ]
521+ dat2 <- cbind(x = tmp2 $ x , y = tmp2 $ ymin , others2 )
522+ rbind(dat1 , dat2 )
523+ }
524+
496525# Convert basic geoms to traces.
497526geom2trace <- list (
498527 path = function (data , params ) {
@@ -515,7 +544,8 @@ geom2trace <- list(
515544 mode = " lines" ,
516545 line = paramORdefault(params , aes2line , polygon.line.defaults ),
517546 fill = " tozerox" ,
518- fillcolor = toFill(params $ fill ))
547+ fillcolor = toFill(params $ fill , ifelse(is.null(params $ alpha ), 1 ,
548+ params $ alpha )))
519549 },
520550 point = function (data , params ){
521551 L <- list (x = data $ x ,
@@ -667,15 +697,6 @@ geom2trace <- list(
667697 fillcolor = toFill(params $ fill , ifelse(is.null(params $ alpha ), 1 ,
668698 params $ alpha )))
669699 },
670- ribbon = function (data , params ) {
671- list (x = c(data $ x [1 ], data $ x , rev(data $ x )),
672- y = c(data $ ymin [1 ], data $ ymax , rev(data $ ymin )),
673- type = " scatter" ,
674- line = paramORdefault(params , aes2line , ribbon.line.defaults ),
675- fill = " tonexty" ,
676- fillcolor = toFill(params $ fill , ifelse(is.null(params $ alpha ), 1 ,
677- params $ alpha )))
678- },
679700 abline = function (data , params ) {
680701 list (x = c(params $ xstart , params $ xend ),
681702 y = c(params $ intercept + params $ xstart * params $ slope ,
0 commit comments