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)

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:
|
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?