Skip to content

plotly::subplot with >= 3 rows/cols results in different sized plots for inner/boundary plots #2057

Open
@avsdev-cw

Description

@avsdev-cw

As per title, when making a subplot with >= 3 rows or columns with a margin (default is 0.02 but the issue is easier to see with more plots/bigger margins)

library(magrittr)
p <- plotly::plot_ly(mtcars, x = ~cyl, y = ~mpg, color = ~rownames(.data), type = "bar") %>% plotly::layout(showlegend = FALSE)
plotly::subplot(p, p, p, p, p, p, p, p, p, nrows = 3, margin = 0.1)

image

There is a PR (#622) which aims to fix it, but the PR also does some extra things like make the margins fixed rather than scaled. I think it's possible to provide a much quicker & simpler fix as an alternative, given that this has been known about since 2016.

I think in a nutshell its due to the function get_domains & it's for loops:

plotly.R/R/subplots.R

Lines 385 to 401 in 4bb1e44

xs <- vector("list", ncols)
for (i in seq_len(ncols)) {
xs[[i]] <- c(
xstart = widths[i] + if (i == 1) 0 else margins[1],
xend = widths[i + 1] - if (i == ncols) 0 else margins[2]
)
}
xz <- rep_len(xs, nplots)
ys <- vector("list", nrows)
for (i in seq_len(nplots)) {
j <- ceiling(i / ncols)
ys[[i]] <- c(
ystart = 1 - (heights[j]) - if (j == 1) 0 else margins[3],
yend = 1 - (heights[j + 1]) + if (j == nrows) 0 else margins[4]
)
}

It's those if's that are the problem, can they be removed without causing too much trouble?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions