@@ -322,14 +322,18 @@ toBasic <- list(
322
322
})
323
323
group2NA(g , " path" )
324
324
},
325
- polygon = function (g ){
326
- if (is.null(g $ params $ fill )){
327
- g
328
- }else if (is.na(g $ params $ fill )){
329
- group2NA(g , " path" )
330
- }else {
331
- g
332
- }
325
+ rect = function (g ){
326
+ g $ data $ group <- 1 : nrow(g $ data )
327
+ used <- c(" xmin" , " ymin" , " xmax" , " ymax" )
328
+ others <- g $ data [! names(g $ data ) %in% used ]
329
+ g $ data <- with(g $ data , {
330
+ rbind(cbind(x = xmin , y = ymin , others ),
331
+ cbind(x = xmin , y = ymax , others ),
332
+ cbind(x = xmax , y = ymax , others ),
333
+ cbind(x = xmax , y = ymin , others ))
334
+ })
335
+ g $ geom <- " polygon"
336
+ g
333
337
},
334
338
path = function (g ) {
335
339
group2NA(g , " path" )
@@ -411,7 +415,6 @@ toBasic <- list(
411
415
}
412
416
)
413
417
414
-
415
418
# ' Drawing ggplot2 geoms with a group aesthetic is most efficient in
416
419
# ' plotly when we convert groups of things that look the same to
417
420
# ' vectors with NA.
@@ -421,16 +424,41 @@ toBasic <- list(
421
424
# ' @return list of geom info.
422
425
# ' @author Toby Dylan Hocking
423
426
group2NA <- function (g , geom ) {
424
- poly.list <- split(g $ data , g $ data $ group )
427
+ poly.list <- split(g $ data , g $ data $ group , drop = TRUE )
425
428
is.group <- names(g $ data ) == " group"
426
- poly.na.df <- data.frame ()
427
- for (i in seq_along(poly.list )) {
429
+ poly.na.list <- list ()
430
+ forward.i <- seq_along(poly.list )
431
+ # # When group2NA is called on geom_polygon (or geom_rect, which is
432
+ # # treated as a basic polygon), we need to retrace the first points
433
+ # # of each group, see https://github.com/ropensci/plotly/pull/178
434
+ retrace.first.points <- g $ geom == " polygon"
435
+ for (i in forward.i ) {
428
436
no.group <- poly.list [[i ]][, ! is.group , drop = FALSE ]
429
437
na.row <- no.group [1 , ]
430
438
na.row [, c(" x" , " y" )] <- NA
431
- poly.na.df <- rbind(poly.na.df , no.group , na.row )
439
+ retrace.first <- if (retrace.first.points ){
440
+ no.group [1 ,]
441
+ }
442
+ poly.na.list [[paste(i , " forward" )]] <-
443
+ rbind(no.group , retrace.first , na.row )
444
+ }
445
+ if (retrace.first.points ){
446
+ backward.i <- rev(forward.i [- 1 ])[- 1 ]
447
+ for (i in backward.i ){
448
+ no.group <- poly.list [[i ]][1 , ! is.group , drop = FALSE ]
449
+ na.row <- no.group [1 , ]
450
+ na.row [, c(" x" , " y" )] <- NA
451
+ poly.na.list [[paste(i , " backward" )]] <- rbind(no.group , na.row )
452
+ }
453
+ if (length(poly.list ) > 1 ){
454
+ first.group <- poly.list [[1 ]][1 , ! is.group , drop = FALSE ]
455
+ poly.na.list [[" last" ]] <- rbind(first.group , first.group )
456
+ }
457
+ }
458
+ g $ data <- do.call(rbind , poly.na.list )
459
+ if (is.na(g $ data $ x [nrow(g $ data )])){
460
+ g $ data <- g $ data [- nrow(g $ data ), ]
432
461
}
433
- g $ data <- poly.na.df
434
462
g $ geom <- geom
435
463
g
436
464
}
@@ -477,10 +505,12 @@ geom2trace <- list(
477
505
line = paramORdefault(params , aes2line , line.defaults ))
478
506
},
479
507
polygon = function (data , params ){
480
- list (x = c(data $ x , data $ x [1 ]),
481
- y = c(data $ y , data $ y [1 ]),
508
+ g <- list (data = data , geom = " polygon" )
509
+ g <- group2NA(g , " polygon" )
510
+ list (x = g $ data $ x ,
511
+ y = g $ data $ y ,
482
512
name = params $ name ,
483
- text = data $ text ,
513
+ text = g $ data $ text ,
484
514
type = " scatter" ,
485
515
mode = " lines" ,
486
516
line = paramORdefault(params , aes2line , polygon.line.defaults ),
0 commit comments