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 ?

2 Answers 2

27

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.

Sign up to request clarification or add additional context in comments.

Comments

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")))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.