R Plotly: subplot with table

你说的曾经没有我的故事 提交于 2021-02-10 12:39:20

问题


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:


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


来源:https://stackoverflow.com/questions/56267164/r-plotly-subplot-with-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!