【问题标题】:Plotly animated Sankey rescaling RPlotly 动画 Sankey 重新缩放 R
【发布时间】:2021-09-06 19:14:05
【问题描述】:

我用 R 中的 Plotly 创建了一个大型动画 sankey 图。在动画的每一步,Plotly 都会重新调整链接的高度,而高度应该随着时间的推移保持不变。也就是说,具有恒定值的链接应始终具有相同的高度,并且不应重新缩放。在下面的示例中,A2 和 B2 之间的链接应保持相同的高度,而不是重新缩放以使链接填充 100% 的画布高度。 https://plotly.com/r/reference/#sankey 提到 ids 作为动画期间数据点对象恒定性的一个选项。我不确定它是否会成功,因为我还没有弄清楚如何实现它。这种或任何其他实现恒定规模的方式将非常有帮助。 结果应该看起来或多或少像 gif。我在这里通过创建虚拟节点来“欺骗”,这些节点占用了原本应该为空的空间。该方法对于端图的可行性较低,比 Mike Bostock 的规范能量预测 Sankey 更复杂

library(plotly)

S.links <- data.frame(source = c(0,1,0,2,3,3, 0,1,0,2,3,3),
                    target = c(2,3,3,4,4,5, 2,3,3,4,4,5),
                    value =  c(8,4,2,8,4,2, 16,4,2,16,4,2),
                    year = c("2001","2001","2001","2001","2001","2001","2002","2002","2002","2002","2002","2002"))

plot_ly(
  type = "sankey",
  orientation = "h",
  
  node = list(
    label = c("A1", "A2", "B1", "B2", "C1", "C2"),
    color = c("blue", "blue", "blue", "blue", "blue", "blue"),
    pad = 15,
    thickness = 20,
    line = list(
      color = "black",
      width = 0.5
    )
  ),
  
  link = S.links,
  frame = ~S.links$year
)

【问题讨论】:

    标签: r animation plotly sankey-diagram rescale


    【解决方案1】:

    我猜你需要 group 参数

    library(plotly)
    
    S.links <- data.frame(source = c(0,1,0,2,3,3, 0,1,0,2,3,3),
                          target = c(2,3,3,4,4,5, 2,3,3,4,4,5),
                          value =  c(8,4,2,8,4,2, 16,4,2,16,4,2),
                          year = c("2001","2001","2001","2001","2001","2001",
                                   "2002","2002","2002","2002","2002","2002"))
    S.links
    #>    source target value year
    #> 1       0      2     8 2001
    #> 2       1      3     4 2001
    #> 3       0      3     2 2001
    #> 4       2      4     8 2001
    #> 5       3      4     4 2001
    #> 6       3      5     2 2001
    #> 7       0      2    16 2002
    #> 8       1      3     4 2002
    #> 9       0      3     2 2002
    #> 10      2      4    16 2002
    #> 11      3      4     4 2002
    #> 12      3      5     2 2002
    
    plot_ly(
      type = "sankey",
      orientation = "h",
      
      node = list(
        label = c("A1", "A2", "B1", "B2", "C1", "C2"),
        color = c("blue", "blue", "blue", "blue", "blue", "blue"),
        pad = 15,
        thickness = 20, 
        # added the group argument
        group = 1,
        line = list(
          color = "black",
          width = 0.5
        )
      ),
      
      link = S.links,
      frame = ~S.links$year
    )
    

    【讨论】:

    • 我已经尝试过了,但它似乎不起作用。我检查了参考指南,节点只有一个 groups 属性,我也尝试过。在这两种情况下,它仍然重新调整了节点 A2 的高度,它具有相同的值。是否有您需要安装的软件包或者我错过了其他东西?
    • 我附上了一张它的样子的 gif。这里有什么问题?也许我错过了你的意思
    猜你喜欢
    • 1970-01-01
    • 2021-09-27
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    相关资源
    最近更新 更多