3
votes

I can't figure out how to make a table subplot work properly using R and Plotly.

The following demonstrates what I mean. I have two plots, one scatter and one table. Using the subplot function, the table plot does not behave as expected.

I either lose interactivity on the scatter plot, or have overlapping scatter/table plots. Please let me know if I'm missing something or if this is a bug.

I'm using plotly version 4.9.0

Thanks in advance!

library(plotly)

df <- data.frame(x = 1:10,
                 y = 1:10)

p1 <- 
  plot_ly(data = df,
          type = "scatter",
          mode = "line",
          x = ~x,
          y = ~y) %>%
  layout(xaxis = list(fixedrange=T),
         yaxis = list(fixedrange=T)) %>%
  config(displayModeBar = F)

p2 <-
  plot_ly(
    type = 'table',
    header = list(values = c("x", "y")),
    cells = list(values = rbind(df$x, df$y))
  )


subplot(p1, p2, nrows = 2) # overlapping plots

subplot(p2, p1, nrows = 2) # loses interactivity on p2

subplot(p1, p1, nrows = 2) # works as expected
1

1 Answers

3
votes

I just read in the documentation for Plotly:

In Plotly there is no native way to insert a Plotly Table into a Subplot.

https://plot.ly/python/table-subplots/

But it seems we could create our own layout. For the table, I added 'domain' to locate it on the left side:

p2 <-
  plot_ly(
    type = 'table',
    domain = list(x=c(0,0.5), y=c(0,1)),
    header = list(values = c("x", "y")),
    cells = list(values = rbind(df$x, df$y))
  )

And then added 'layout' for the table on the left and scatterplot on the right:

subplot(p1, p2, nrows = 2) %>%
  layout(xaxis = list(domain=c(0.5,1.0)),
         xaxis2 = list(domain=c(0,0.5)))