简体   繁体   English

R Plotly:带表格的子图

[英]R Plotly: subplot with table

I can't figure out how to make a table subplot work properly using R and Plotly.我不知道如何使用 R 和 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.使用 subplot 函数,表格图的行为不符合预期。

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我正在使用 plotly 版本 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:我刚刚阅读了 Plotly 的文档:

In Plotly there is no native way to insert a Plotly Table into a Subplot.在 Plotly 中,没有将 Plotly 表插入到子图中的原生方法。

https://plot.ly/python/table-subplots/ 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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM