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