1313# ' \code{tooltip = c("y", "x", "colour")} if you want y first, x second, and
1414# ' colour last.
1515# ' @param source Only relevant for \link{event_data}.
16+ # ' @param ... arguments passed onto methods.
1617# ' @seealso \link{signup}, \link{plot_ly}
1718# ' @return a plotly object
1819# ' @export
3132# ' }
3233# '
3334ggplotly <- 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" , ... ) {
3575 l <- gg2list(p , width = width , height = height , tooltip = tooltip , source = source )
3676 hash_plot(p $ data , l )
3777}
@@ -44,9 +84,10 @@ ggplotly <- function(p = ggplot2::last_plot(), width = NULL, height = NULL,
4484# ' tooltip. The default, "all", means show all the aesthetic tooltips
4585# ' (including the unofficial "text" aesthetic).
4686# ' @param source Only relevant for \link{event_data}.
87+ # ' @param ... currently not used
4788# ' @return a 'built' plotly object (list with names "data" and "layout").
4889# ' @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" , ... ) {
5091 # ------------------------------------------------------------------------
5192 # Our internal version of ggplot2::ggplot_build(). Modified from
5293 # 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
425466 gglayout $ annotations ,
426467 make_label(
427468 faced(axisTitleText , axisTitle $ face ), x , y , el = axisTitle ,
428- xanchor = " center" , yanchor = " middle"
469+ xanchor = " center" , yanchor = " middle" , annotationType = " axis "
429470 )
430471 )
431472 }
432473 }
433474 }
434-
435- if (has_facet(p )) {
436- gglayout [[axisName ]]$ title <- " "
437- }
438-
475+ if (has_facet(p )) gglayout [[axisName ]]$ title <- " "
439476 } # end of axis loop
440477
478+ # theme(panel.border = ) -> plotly rect shape
441479 xdom <- gglayout [[lay [, " xaxis" ]]]$ domain
442480 ydom <- gglayout [[lay [, " yaxis" ]]]$ domain
443481 border <- make_panel_border(xdom , ydom , theme )
444482 gglayout $ shapes <- c(gglayout $ shapes , border )
445-
483+
446484 # 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 = " , "
452489 )
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(
465503 p $ facet $ labeller(lay [names(p $ facet $ rows )]), collapse = " , "
466504 )
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+ }
475517 }
476-
477518 } # end of panel loop
478519
479520 # ------------------------------------------------------------------------
0 commit comments