R Plotly:带表格的子图 [英] R Plotly: subplot with table

查看:79
本文介绍了R Plotly:带表格的子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用 R 和 Plotly 使表格子图正常工作.

以下内容说明了我的意思.我有两张图,一张散点图和一张桌子.使用 subplot 函数,表格图的行为不符合预期.

我要么在散点图上失去交互性,要么有重叠的散点图/表格图.如果我遗漏了什么或者这是一个错误,请告诉我.

我使用的是 plotly 4.9.0 版

提前致谢!

图书馆(情节)df <- data.frame(x = 1:10,y = 1:10)p1<-plot_ly(数据= df,类型=分散",模式=线",x = ~x,y = ~y) %>%布局(xaxis = 列表(固定范围= T),yaxis = 列表(固定范围=T))%>%配置(显示模式栏 = F)p2<-情节_ly(类型 = '表',header = list(values = c("x", "y")),单元格 = 列表(值 = rbind(df$x,df$y)))subplot(p1, p2, nrows = 2) # 重叠图subplot(p2, p1, nrows = 2) # 在 p2 上失去交互性subplot(p1, p1, nrows = 2) # 按预期工作

解决方案

我刚刚阅读了 Plotly 的文档:

<块引用>

在 Plotly 中,没有本地方式将 Plotly 表插入到子情节.

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

但似乎我们可以创建自己的布局.对于表格,我添加了域"以将其定位在左侧:

p2 <-情节_ly(类型 = '表',域 = 列表(x=c(0,0.5), y=c(0,1)),header = list(values = c("x", "y")),单元格 = 列表(值 = rbind(df$x,df$y)))

然后为左侧的表格和右侧的散点图添加布局":

subplot(p1, p2, nrows = 2) %>%布局(xaxis = 列表(域= c(0.5,1.0)),xaxis2 = list(domain=c(0,0.5)))

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

这篇关于R Plotly:带表格的子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆