Skip to content

Commit 0b74e8d

Browse files
committed
ggplotly: fix Xaxis anchor if the last row incomplete
attach X axis to the last non-empty row in a column
1 parent d9662a6 commit 0b74e8d

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

R/ggplotly.R

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -459,27 +459,35 @@ gg2list <- function(p, width = NULL, height = NULL,
459459

460460
# panel -> plotly.js axis/anchor info
461461
# (assume a grid layout by default)
462-
layout$layout$xaxis <- layout$layout$COL
463-
layout$layout$yaxis <- layout$layout$ROW
464-
layout$layout$xanchor <- nRows
465-
layout$layout$yanchor <- 1
462+
layout$layout <- dplyr::mutate(layout$layout,
463+
xaxis = COL,
464+
yaxis = ROW,
465+
xanchor = nRows,
466+
yanchor = 1L)
466467
if (inherits(plot$facet, "FacetWrap")) {
467-
if (plot$facet$params$free$x) {
468-
layout$layout$xaxis <- layout$layout$PANEL
469-
layout$layout$xanchor <- layout$layout$ROW
470-
}
471-
if (plot$facet$params$free$y) {
472-
layout$layout$yaxis <- layout$layout$PANEL
473-
layout$layout$yanchor <- layout$layout$COL
474-
layout$layout$xanchor <- nPanels
475-
}
476468
if (plot$facet$params$free$x && plot$facet$params$free$y) {
477-
layout$layout$xaxis <- layout$layout$PANEL
478-
layout$layout$yaxis <- layout$layout$PANEL
479-
layout$layout$xanchor <- layout$layout$PANEL
480-
layout$layout$yanchor <- layout$layout$PANEL
469+
layout$layout <- dplyr::mutate(layout$layout,
470+
xaxis = PANEL,
471+
yaxis = PANEL,
472+
xanchor = PANEL,
473+
yanchor = PANEL)
474+
} else if (plot$facet$params$free$x) {
475+
layout$layout <- dplyr::mutate(layout$layout,
476+
xaxis = PANEL,
477+
xanchor = ROW)
478+
} else if (plot$facet$params$free$y) {
479+
layout$layout <- dplyr::mutate(layout$layout,
480+
yaxis = PANEL,
481+
yanchor = COL)
481482
}
483+
# anchor X axis to the lowest plot in its column
484+
layout$layout <- dplyr::group_by(layout$layout, xaxis) %>%
485+
dplyr::mutate(xanchor = max(as.integer(yaxis))) %>%
486+
dplyr::ungroup() %>%
487+
dplyr::mutate(xanchor = if (is.factor(yaxis)) levels(yaxis)[xanchor] else xanchor)
482488
}
489+
layout$layout <- as.data.frame(layout$layout)
490+
483491
# format the axis/anchor to a format plotly.js respects
484492
layout$layout$xaxis <- paste0("xaxis", sub("^1$", "", layout$layout$xaxis))
485493
layout$layout$yaxis <- paste0("yaxis", sub("^1$", "", layout$layout$yaxis))

tests/testthat/test-ggplot-facets.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,5 @@ test_that("when y scales are free, x-axes are still anchored on exterior", {
140140
info <- expect_doppelganger_built(p, "facet_wrap-free_y-2")
141141
xaxes <- info$layout[grep("^xaxis", names(info$layout))]
142142
yaxes <- info$layout[grep("^yaxis", names(info$layout))]
143-
expect_equivalent(unique(sapply(xaxes, "[[", "anchor")), "y5")
143+
expect_equivalent(unique(sapply(xaxes, "[[", "anchor")), c("y5", "y4"))
144144
})

0 commit comments

Comments
 (0)