13
13
# ' \code{tooltip = c("y", "x", "colour")} if you want y first, x second, and
14
14
# ' colour last.
15
15
# ' @param source Only relevant for \link{event_data}.
16
+ # ' @param ... arguments passed onto methods.
16
17
# ' @seealso \link{signup}, \link{plot_ly}
17
18
# ' @return a plotly object
18
19
# ' @export
31
32
# ' }
32
33
# '
33
34
ggplotly <- function (p = ggplot2 :: last_plot(), width = NULL , height = NULL ,
34
- tooltip = " all" , source = " A" ) {
35
+ tooltip = " all" , source = " A" , ... ) {
36
+ UseMethod(" ggplotly" , p )
37
+ }
38
+
39
+ # ' @export
40
+ ggplotly.ggmatrix <- function (p = ggplot2 :: last_plot(), width = NULL ,
41
+ height = NULL , tooltip = " all" , source = " A" , ... ) {
42
+ subplotList <- list ()
43
+ for (i in seq_len(p $ ncol )) {
44
+ columnList <- list ()
45
+ for (j in seq_len(p $ nrow )) {
46
+ thisPlot <- p [j , i ]
47
+ if (i == 1 ) {
48
+ if (p $ showYAxisPlotLabels ) thisPlot <- thisPlot + ylab(p $ yAxisLabels [j ])
49
+ } else {
50
+ # y-axes are never drawn on the interior, and diagonal plots are densities,
51
+ # so it doesn't make sense to synch zoom actions on y
52
+ thisPlot <- thisPlot +
53
+ theme(
54
+ axis.ticks.y = element_blank(),
55
+ axis.text.y = element_blank()
56
+ )
57
+ }
58
+ columnList <- c(columnList , list (ggplotly(thisPlot , tooltip = tooltip )))
59
+ }
60
+ # conditioned on a column in a ggmatrix, the x-axis should be on the
61
+ # same scale.
62
+ s <- subplot(columnList , nrows = p $ nrow , margin = 0.01 , shareX = TRUE , titleY = TRUE )
63
+ subplotList <- c(subplotList , list (s ))
64
+ }
65
+ s <- layout(subplot(subplotList , nrows = 1 ), width = width , height = height )
66
+ if (nchar(p $ title ) > 0 ) {
67
+ s <- layout(s , title = p $ title )
68
+ }
69
+ hash_plot(p $ data , plotly_build(s ))
70
+ }
71
+
72
+ # ' @export
73
+ ggplotly.ggplot <- function (p = ggplot2 :: last_plot(), width = NULL ,
74
+ height = NULL , tooltip = " all" , source = " A" , ... ) {
35
75
l <- gg2list(p , width = width , height = height , tooltip = tooltip , source = source )
36
76
hash_plot(p $ data , l )
37
77
}
@@ -44,9 +84,10 @@ ggplotly <- function(p = ggplot2::last_plot(), width = NULL, height = NULL,
44
84
# ' tooltip. The default, "all", means show all the aesthetic tooltips
45
85
# ' (including the unofficial "text" aesthetic).
46
86
# ' @param source Only relevant for \link{event_data}.
87
+ # ' @param ... currently not used
47
88
# ' @return a 'built' plotly object (list with names "data" and "layout").
48
89
# ' @export
49
- gg2list <- function (p , width = NULL , height = NULL , tooltip = " all" , source = " A" ) {
90
+ gg2list <- function (p , width = NULL , height = NULL , tooltip = " all" , source = " A" , ... ) {
50
91
# ------------------------------------------------------------------------
51
92
# Our internal version of ggplot2::ggplot_build(). Modified from
52
93
# https://github.com/hadley/ggplot2/blob/0cd0ba/R/plot-build.r#L18-L92
@@ -425,55 +466,55 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
425
466
gglayout $ annotations ,
426
467
make_label(
427
468
faced(axisTitleText , axisTitle $ face ), x , y , el = axisTitle ,
428
- xanchor = " center" , yanchor = " middle"
469
+ xanchor = " center" , yanchor = " middle" , annotationType = " axis "
429
470
)
430
471
)
431
472
}
432
473
}
433
474
}
434
-
435
- if (has_facet(p )) {
436
- gglayout [[axisName ]]$ title <- " "
437
- }
438
-
475
+ if (has_facet(p )) gglayout [[axisName ]]$ title <- " "
439
476
} # end of axis loop
440
477
478
+ # theme(panel.border = ) -> plotly rect shape
441
479
xdom <- gglayout [[lay [, " xaxis" ]]]$ domain
442
480
ydom <- gglayout [[lay [, " yaxis" ]]]$ domain
443
481
border <- make_panel_border(xdom , ydom , theme )
444
482
gglayout $ shapes <- c(gglayout $ shapes , border )
445
-
483
+
446
484
# facet strips -> plotly annotations
447
- if (! is_blank(theme [[" strip.text.x" ]]) &&
448
- (inherits(p $ facet , " wrap" ) || inherits(p $ facet , " grid" ) && lay $ ROW == 1 )) {
449
- vars <- ifelse(inherits(p $ facet , " wrap" ), " facets" , " cols" )
450
- txt <- paste(
451
- p $ facet $ labeller(lay [names(p $ facet [[vars ]])]), collapse = " , "
485
+ if (has_facet(p )) {
486
+ col_vars <- ifelse(inherits(p $ facet , " wrap" ), " facets" , " cols" )
487
+ col_txt <- paste(
488
+ p $ facet $ labeller(lay [names(p $ facet [[col_vars ]])]), collapse = " , "
452
489
)
453
- lab <- make_label(
454
- txt , x = mean(xdom ), y = max(ydom ),
455
- el = theme [[" strip.text.x" ]] %|| % theme [[" strip.text" ]],
456
- xanchor = " center" , yanchor = " bottom"
457
- )
458
- gglayout $ annotations <- c(gglayout $ annotations , lab )
459
- strip <- make_strip_rect(xdom , ydom , theme , " top" )
460
- gglayout $ shapes <- c(gglayout $ shapes , strip )
461
- }
462
- if (inherits(p $ facet , " grid" ) && lay $ COL == nCols && nRows > 1 &&
463
- ! is_blank(theme [[" strip.text.y" ]])) {
464
- txt <- paste(
490
+ if (is_blank(theme [[" strip.text.x" ]])) col_txt <- " "
491
+ if (inherits(p $ facet , " grid" ) && lay $ ROW != 1 ) col_txt <- " "
492
+ if (nchar(col_txt ) > 0 ) {
493
+ col_lab <- make_label(
494
+ col_txt , x = mean(xdom ), y = max(ydom ),
495
+ el = theme [[" strip.text.x" ]] %|| % theme [[" strip.text" ]],
496
+ xanchor = " center" , yanchor = " bottom"
497
+ )
498
+ gglayout $ annotations <- c(gglayout $ annotations , col_lab )
499
+ strip <- make_strip_rect(xdom , ydom , theme , " top" )
500
+ gglayout $ shapes <- c(gglayout $ shapes , strip )
501
+ }
502
+ row_txt <- paste(
465
503
p $ facet $ labeller(lay [names(p $ facet $ rows )]), collapse = " , "
466
504
)
467
- lab <- make_label(
468
- txt , x = max(xdom ), y = mean(ydom ),
469
- el = theme [[" strip.text.y" ]] %|| % theme [[" strip.text" ]],
470
- xanchor = " left" , yanchor = " middle"
471
- )
472
- gglayout $ annotations <- c(gglayout $ annotations , lab )
473
- strip <- make_strip_rect(xdom , ydom , theme , " right" )
474
- gglayout $ shapes <- c(gglayout $ shapes , strip )
505
+ if (is_blank(theme [[" strip.text.y" ]])) row_txt <- " "
506
+ if (inherits(p $ facet , " grid" ) && lay $ COL != nCols ) row_txt <- " "
507
+ if (nchar(row_txt ) > 0 ) {
508
+ row_lab <- make_label(
509
+ row_txt , x = max(xdom ), y = mean(ydom ),
510
+ el = theme [[" strip.text.y" ]] %|| % theme [[" strip.text" ]],
511
+ xanchor = " left" , yanchor = " middle"
512
+ )
513
+ gglayout $ annotations <- c(gglayout $ annotations , row_lab )
514
+ strip <- make_strip_rect(xdom , ydom , theme , " right" )
515
+ gglayout $ shapes <- c(gglayout $ shapes , strip )
516
+ }
475
517
}
476
-
477
518
} # end of panel loop
478
519
479
520
# ------------------------------------------------------------------------
0 commit comments