Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -459,27 +459,35 @@ gg2list <- function(p, width = NULL, height = NULL,

# panel -> plotly.js axis/anchor info
# (assume a grid layout by default)
layout$layout$xaxis <- layout$layout$COL
layout$layout$yaxis <- layout$layout$ROW
layout$layout$xanchor <- nRows
layout$layout$yanchor <- 1
layout$layout <- dplyr::mutate(layout$layout,
xaxis = COL,
yaxis = ROW,
xanchor = nRows,
yanchor = 1L)
if (inherits(plot$facet, "FacetWrap")) {
if (plot$facet$params$free$x) {
layout$layout$xaxis <- layout$layout$PANEL
layout$layout$xanchor <- layout$layout$ROW
}
if (plot$facet$params$free$y) {
layout$layout$yaxis <- layout$layout$PANEL
layout$layout$yanchor <- layout$layout$COL
layout$layout$xanchor <- nPanels
}
if (plot$facet$params$free$x && plot$facet$params$free$y) {
layout$layout$xaxis <- layout$layout$PANEL
layout$layout$yaxis <- layout$layout$PANEL
layout$layout$xanchor <- layout$layout$PANEL
layout$layout$yanchor <- layout$layout$PANEL
layout$layout <- dplyr::mutate(layout$layout,
xaxis = PANEL,
yaxis = PANEL,
xanchor = PANEL,
yanchor = PANEL)
} else if (plot$facet$params$free$x) {
layout$layout <- dplyr::mutate(layout$layout,
xaxis = PANEL,
xanchor = ROW)
} else if (plot$facet$params$free$y) {
layout$layout <- dplyr::mutate(layout$layout,
yaxis = PANEL,
yanchor = COL)
}
# anchor X axis to the lowest plot in its column
layout$layout <- dplyr::group_by(layout$layout, xaxis) %>%
dplyr::mutate(xanchor = max(as.integer(yaxis))) %>%
dplyr::ungroup() %>%
dplyr::mutate(xanchor = if (is.factor(yaxis)) levels(yaxis)[xanchor] else xanchor)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't immediately follow why dplyr::mutate(xanchor = if (is.factor(yaxis)) levels(yaxis)[xanchor] else xanchor) is necessary, do you have an example in mind where this becomes relevant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it looks a bit ugly. I had trouble with some of the plots (maybe even the ones in the tests) when either a PANEL or COL is a factor.
There was some error happening afterwards, but unfortunately I forgot the details. So I had to convert xanchor back to a factor.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alyst tests are passing without it -- I would be very grateful if you can report a case where 3c85052 doesn't do what you'd expect!

layout$layout <- as.data.frame(layout$layout)

# format the axis/anchor to a format plotly.js respects
layout$layout$xaxis <- paste0("xaxis", sub("^1$", "", layout$layout$xaxis))
layout$layout$yaxis <- paste0("yaxis", sub("^1$", "", layout$layout$yaxis))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-ggplot-facets.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,5 @@ test_that("when y scales are free, x-axes are still anchored on exterior", {
info <- expect_doppelganger_built(p, "facet_wrap-free_y-2")
xaxes <- info$layout[grep("^xaxis", names(info$layout))]
yaxes <- info$layout[grep("^yaxis", names(info$layout))]
expect_equivalent(unique(sapply(xaxes, "[[", "anchor")), "y5")
expect_equivalent(unique(sapply(xaxes, "[[", "anchor")), c("y5", "y4"))
})