【问题标题】:R plotly - sankey chart not returnedR plotly - 未返回桑基图
【发布时间】:2022-10-18 15:36:47
【问题描述】:

我在 RStudio 中运行下面的代码,并想用 plotly 创建 sankey 图表。代码运行没有错误。但不显示桑基图。这里有什么问题?

library("plotly")
a = read.csv('cleanSankey.csv', header=TRUE, sep=',')
node_names <- unique(c(as.character(a$source), as.character(a$target)))
nodes <- data.frame(name = node_names)
links <- data.frame(source = match(a$source, node_names) - 1,
                    target = match(a$target, node_names) - 1,
                    value = a$value)

nodes_with_position <- data.frame(
  "id" = names,
  "label" = node_names,
  "x" = c(0, 0.1, 0.2, 0.3,0.4,0.5,0.6,0.7),
  "y" = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7)
)

#Plot
plot_ly(type='sankey',
        orientation = "h",
        
        node = list(
          label = node_names,
          x = nodes_with_position$x,
          y = nodes_with_position$y,
          color = "grey",
          pad = 15,
          thinkness = 20,
          line = list(color = "grey", width = 0.5)),
 
         link = list(
           source = links$source,
           target = links$target,
           value = links$value))

sankey 被绘制,但是第二层的节点转到最后一层。如何固定节点位置?

【问题讨论】:

    标签: r plotly sankey-diagram


    【解决方案1】:

    您需要通过设置 arrangement 参数来定义节点位置,以停止自动调整位置。

    这需要一些技巧,因为您需要指定节点的坐标。您可以在这里找到更多详细信息:https://plotly.com/python/sankey-diagram/#define-node-position

    代码

    library(plotly)
    
    a <- read.csv("~/cleanSankey.csv")
    
    node_names <- unique(c(as.character(a$source), as.character(a$target)))
    
    # added id column for clarity, but it's likely not needed
    nodes <- data.frame(
      id = 0:(length(node_names)-1),
      name = node_names
    )
    
    links <- data.frame(
      source = match(a$source, node_names) - 1,
      target = match(a$target, node_names) - 1,
      value = a$value
    )
    
    # set the coordinates of the nodes
    nodes$x <- c(0, 1, 0.5)
    nodes$y <- c(0, 0.5, 1)
    
    # plot - note the `arrangement="snap"` argument
    plot_ly(
      type='sankey',
      orientation = "h",
      arrangement="snap", # can also change this to 'fixed'
      node = list(
        label = nodes$name,
        x = nodes$x,
        y = nodes$y,
        color = "grey",
        pad = 15,
        thinkness = 20,
        line = list(color = "grey", width = 0.5)
      ),
      link = list(
        source = links$source,
        target = links$target,
        value = links$value
      )
    )
    

    用排列=“快照”绘制输出:

    【讨论】:

    • 我认为您只需将arrangement="snap" 添加到您的plot_ly 函数中。如果这不起作用,请尝试arrangement="fixed"
    • 我在桑基图中有 8 层/列。根据您的建议,我收到错误“as.data.frame.default(x[[i]], optional = TRUE) 中的错误:无法将类‘‘function’’强制转换为 data.frame”。代码在帖子中更新。
    • 尝试添加排列参数,仍然失败。错误仍然是 as.data.frame.default(x[[i]], optional = TRUE) 中的错误:无法将类“函数”强制转换为 data.frame
    • 您是否收到我提供的示例数据或不同数据集的错误?
    • 我正在使用我自己的数据集。我在帖子中发布了一些示例 csv 数据。
    猜你喜欢
    • 2016-04-06
    • 2012-04-15
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 2020-07-23
    • 2019-10-20
    相关资源
    最近更新 更多