Niccolò Ajroldi
Niccolò Ajroldi

Reputation: 45

Plotly: shareX between side-by-side plots

I would like to have two side by side plots sharing the same X-axis and the same toolbar. This means that, by zooming in the first plot, the second plot should automatically resize to the same zoomed region.

A way to do that could be to stack the plots one above the other, using shareX=TRUE, but I need them to be side by side.

In python there seems to be a way to do that, using fig.update_xaxes(matches='x'). Is there a similar option in R?

Here is a sample code:

library(plotly) 

n = 10
x = 1:n
y = rnorm(n)

fig1 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers') 
fig2 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers') 
fig  <- subplot(fig1, fig2, shareX = TRUE) # shareX actually not working here
fig

Thank you in advance!

Upvotes: 3

Views: 672

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33417

We can use matches in R just as we can in python.

Run schema() and navigate:

object ► layout ► layoutAttributes ► xaxis ► matches

for more info.

This keeps all (x&y) axes synced:

library(plotly) 

n = 10
x = 1:n
y = rnorm(n)

fig1 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers') 
fig2 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers', xaxis = "x") %>% layout(xaxis = list(matches = "x"))
fig  <- subplot(fig1, fig2, shareX = TRUE, shareY = TRUE)
fig

result

Upvotes: 2

Related Questions