Skip to content

Commit 88591f6

Browse files
committed
improve ggplotly.ggmatrix logic
1 parent 8b15c00 commit 88591f6

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

R/ggplotly.R

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,35 @@ ggplotly <- function(p = ggplot2::last_plot(), width = NULL, height = NULL,
3838
#' @export
3939
ggplotly.ggmatrix <- function(p = ggplot2::last_plot(), width = NULL,
4040
height = NULL, tooltip = "all", source = "A", ...) {
41-
plotList <- list()
42-
for (i in seq_len(p$nrow)) {
43-
for (j in seq_len(p$ncol)) {
44-
plotList <- c(plotList, list(p[i, j]))
41+
subplotList <- list()
42+
for (i in seq_len(p$ncol)) {
43+
columnList <- list()
44+
for (j in seq_len(p$nrow)) {
45+
thisPlot <- p[j, i]
46+
if (i == 1) {
47+
if (p$showYAxisPlotLabels) thisPlot <- thisPlot + ylab(p$yAxisLabels[j])
48+
} else {
49+
# y-axes are never drawn on the interior, and diagonal plots are densities,
50+
# so it doesn't make sense to synch zoom actions on y
51+
thisPlot <- thisPlot +
52+
theme(
53+
axis.ticks.y = element_blank(),
54+
axis.text.y = element_blank()
55+
)
56+
}
57+
columnList <- c(columnList, list(ggplotly(thisPlot, tooltip = tooltip)))
4558
}
59+
# conditioned on a column in a ggmatrix, the x-axis should be on the
60+
# same scale.
61+
s <- subplot(columnList, nrows = p$nrow, margin = 0.01, shareX = TRUE, titleY = TRUE)
62+
#if (i == 3) browser()
63+
subplotList <- c(subplotList, list(s))
4664
}
47-
# TODO:
48-
# (1) how to show x/y titles? Should these be arguments in subplot?
49-
# (2) it only makes since to share axes on the lower diagonal
50-
l <- get_plot(do.call(subplot, c(plotList, list(nrows = p$nrow, ...))))
51-
l$layout$title <- p$title
52-
hash_plot(p$data, l)
65+
s <- subplot(subplotList, nrows = 1)
66+
if (nchar(p$title) > 0) {
67+
s <- layout(s, title = p$title)
68+
}
69+
layout(s, width = width, height = height)
5370
}
5471

5572
#' @export
@@ -673,8 +690,8 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
673690
l <- list(data = setNames(traces, NULL), layout = compact(gglayout))
674691
# ensure properties are boxed correctly
675692
l <- add_boxed(rm_asis(l))
676-
l$width <- width
677-
l$height <- height
693+
l$layout$width <- width
694+
l$layout$height <- height
678695
l$source <- source
679696
structure(l, class = "plotly_built")
680697
}

0 commit comments

Comments
 (0)