@@ -26,13 +26,15 @@ layer2traces <- function(l, d, misc) {
26
26
# geom_smooth() means geom_line() + geom_ribbon()
27
27
# Note the line is always drawn, but ribbon is not if se = FALSE.
28
28
if (g $ geom == " smooth" ) {
29
- # If smoothLine has been compiled already, consider smoothRibbon.
29
+ # If smoothLine has been compiled already, consider drawing the ribbon
30
30
if (isTRUE(misc $ smoothLine )) {
31
31
misc $ smoothLine <- FALSE
32
32
if (isTRUE(l $ stat_params $ se == FALSE )) {
33
33
return (NULL )
34
34
} else {
35
35
g $ geom <- " smoothRibbon"
36
+ # disregard colour
37
+ g $ data <- g $ data [! grepl(" ^colour[.name]?" , names(g $ data ))]
36
38
}
37
39
} else {
38
40
misc $ smoothLine <- TRUE
@@ -248,7 +250,6 @@ layer2traces <- function(l, d, misc) {
248
250
if (length(unique(name.list )) < 2 )
249
251
tr $ name <- as.character(name.list [[1 ]])
250
252
}
251
-
252
253
dpd <- data.params $ data
253
254
if (" PANEL" %in% names(dpd ) && nrow(dpd ) > 0 )
254
255
{
@@ -335,6 +336,11 @@ toBasic <- list(
335
336
g $ geom <- " polygon"
336
337
g
337
338
},
339
+ ribbon = function (g ) {
340
+ g $ data <- ribbon_dat(g $ data )
341
+ g $ geom <- " polygon"
342
+ g
343
+ },
338
344
path = function (g ) {
339
345
group2NA(g , " path" )
340
346
},
@@ -406,12 +412,15 @@ toBasic <- list(
406
412
g
407
413
},
408
414
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"
410
417
group2NA(g , " path" )
411
418
},
412
419
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
415
424
}
416
425
)
417
426
@@ -493,6 +502,26 @@ make.errorbar <- function(data, params, xy){
493
502
tr
494
503
}
495
504
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
+
496
525
# Convert basic geoms to traces.
497
526
geom2trace <- list (
498
527
path = function (data , params ) {
@@ -515,7 +544,8 @@ geom2trace <- list(
515
544
mode = " lines" ,
516
545
line = paramORdefault(params , aes2line , polygon.line.defaults ),
517
546
fill = " tozerox" ,
518
- fillcolor = toFill(params $ fill ))
547
+ fillcolor = toFill(params $ fill , ifelse(is.null(params $ alpha ), 1 ,
548
+ params $ alpha )))
519
549
},
520
550
point = function (data , params ){
521
551
L <- list (x = data $ x ,
@@ -667,15 +697,6 @@ geom2trace <- list(
667
697
fillcolor = toFill(params $ fill , ifelse(is.null(params $ alpha ), 1 ,
668
698
params $ alpha )))
669
699
},
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
- },
679
700
abline = function (data , params ) {
680
701
list (x = c(params $ xstart , params $ xend ),
681
702
y = c(params $ intercept + params $ xstart * params $ slope ,
0 commit comments