4

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 Answer 1

4

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

Edit (11/30/23):

Thanks to @YBS for recommending a function from the manipulateWidget package for placing a table beneath the plot. This package generally can:

...be used to create in a very easy and quick way a graphical interface that lets the user modify the data or the parameters of an interactive chart.

In this case, you can try:

manipulateWidget::combineWidgets(list = list(p1, p2))

You can also specify arguments for the number and size of rows and columns of multiple interactive plots.

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

4 Comments

Do you know how to add the frame parameter to plot_ly() in this solution? If frame is the time, I need the plot and the table to update regarding to the time. It works for the plot, but the table shows the whole table even if I add the frame parameter. So I suppose type table does not have it. Do you have an idea?
I'm sorry I don't - maybe you might want to post a separate question on this?
I did one month ago, but unfortunately, I could not find somebody to help me
Try manipulateWidget::combineWidgets(list = list(p1, p2)) to get table below the plot.

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.