简体   繁体   中英

R plotly subplot add space between plots

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:

在此输入图像描述

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

在此输入图像描述

How should I do ?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM