R plotly subplot add space between plots
Asked Answered
E

2

16

I want to add space between plots but when I play with margin, it either overlaps or cuts. Here is the code:

library(plotly)

plotList <- function(nplots) {
  lapply(seq_len(nplots), function(x) plot_ly())
}
s1 <- subplot(plotList(6), nrows = 2, shareX = TRUE, shareY = TRUE)
s2 <- subplot(plotList(2), shareY = TRUE)
p <- subplot(s1, s2, plot_ly(), nrows = 3, margin = 0.04, heights = c(0.6, 0.3, 0.1))

print(p)

I obtain this:

enter image description here

Whereas i would rather like something like this (image done with paint) with more spacing between the different subplots:

enter image description here

How should I do ?

Edam answered 27/11, 2018 at 16:42 Comment(0)
T
26

I was facing the same problem but, fortunately, I found a solution. You can use margin as an argument in Subplot function as follows:

subplot(plot1,
        plot2,
        nrows = 2,
        margin = 0.07)

According to Plotly documentation, you can define only one or four values for each of the margins and those values should be between 0 and 1. If you provide only one value it will be used for all four margins or if you provide four values it will be used in the following order: the first one will be the left margin, the second one will be the right margin, the third one will be the top margin and the last one will be the bottom margin. You can play around and define the values that better fit the layout you want to create.

Tyeshatyg answered 6/12, 2018 at 4:21 Comment(0)
E
0

I found a solution by inserting blank plots between the normal plot. It is however a bit awkward but it works and I've found nothing better

blankplot<-plot_ly()%>%
  layout(xaxis=list(visible="FALSE",color="white",tickfont =list(color="white")),
         yaxis=list(visible="FALSE",color="white",tickfont =list(color="white")))
Edam answered 28/11, 2018 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.