【问题标题】:R plotly sankey - how to make the column annonation aligned with each columnR plotly sankey - 如何使列注释与每一列对齐
【发布时间】:2022-10-18 15:28:53
【问题描述】:

我有以下代码,即为桑基图中的每一列添加注释。

library("plotly")
library("ggplot2")
library("dplyr")
library("xml2")
library("htmlwidgets")

a <- read.csv(header = TRUE, text = "date,Data Center,Customer,companyID,source,target,value 
")

node_names <- union(a$source, a$target)

node_names <- node_names[order(sub('.*_', '', node_names))]
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)


definePosition <- function(nodeList){
  #  nodeList = node_names
  # unique name endings
  endings = unique(sub('.*_', '', nodeList))
  # define intervals
  steps = 1/length(endings)
  # x-values for each unique name ending
  # for input as node position
  nodes_x = {}
  xVal = 0
  for (e in endings) {
    nodes_x[e] = xVal
    xVal = xVal + steps
    
  }
  # x and y values in list form
  x_values <- 0
  y_values <- 0
  i =1
  for (n in nodeList) {
    last = sub('.*_', '', n)
    x_values[i] = nodes_x[last]
    y_values[i] = 0.001 * length(x_values)
    i = i + 1
  }
  
  return(list(x_values, y_values))
  
}

position = definePosition(node_names)
node_x = position[[1L]]
node_y = position[[2L]]

#Plot
plot_ly(type='sankey',
             orientation = "h",
             arrangement = "snap",
             node = list (
               label = node_names,
               x = node_x,
               y = node_y,
               color = "steelblue",
               pad = 15,
               thinkness = 15,
               line = list(color = "black", width = 0.5)),
               link = list(source = links$source, target = links$target, value = links$value, color = "gainsboro")) %>%
  
add_annotations(x=unique(node_x)[1],y=1,xref = "x",yref = "paper",text = "<b>step1</b>",xanchor = 'left',showarrow = F, align = "center") %>%
add_annotations(x=unique(node_x)[2],y=1,xref = "x",yref = "paper",text = "<b>step2</b>",xanchor = 'left',showarrow = F, align = "center") %>%
add_annotations(x=unique(node_x)[3],y=1,xref = "x",yref = "paper",text = "<b>step3</b>",xanchor = 'left',showarrow = F, align = "center") %>%
add_annotations(x=unique(node_x)[4],y=1,xref = "x",yref = "paper",text = "<b>step4</b>",xanchor = 'left',showarrow = F, align = "center") %>%
add_annotations(x=unique(node_x)[5],y=1,xref = "x",yref = "paper",text = "<b>step5</b>",xanchor = 'left',showarrow = F, align = "center") %>%
add_annotations(x=unique(node_x)[6],y=1,xref = "x",yref = "paper",text = "<b>step6</b>",xanchor = 'left',showarrow = F, align = "center") %>%
add_annotations(x=unique(node_x)[7],y=1,xref = "x",yref = "paper",text = "<b>step7</b>",xanchor = 'left',showarrow = F, align = "center") %>%
add_annotations(x=unique(node_x)[8],y=1,xref = "x",yref = "paper",text = "<b>step8</b>",xanchor = 'left',showarrow = F, align = "center") %>%



layout(
  title = "<b>Opportunity Marketing User Behavior Monitor(Prod environment)</b>",
  font = list(
    size = 10,
    color = 'grey'
  ),
  margin = list(
    l = 30,
    r = 50,
    b = 50,
    t = 100,
    pad = 20
  ),
  xaxis = list(showgrid = F, zeroline = F, visiable = F, showticklabels = F),
  yaxis = list(showgrid = F, zeroline = F, visiable = F, showticklabels = F)
)

但运行代码后,添加的注释与每一列不对齐。 我的想法是获取每列的x轴位置,然后为每个add_annonation分配x参数。但是如何知道每列的 x 轴值?

【问题讨论】:

  • 嗨,我也与情节 sankey 图表搏斗,并希望进一步了解这一点。您能否修改您的问题以提供带有示例数据的minimal reproducible example
  • 嗨 even_of_the_hour - 感谢您对此进行调查。我在帖子中添加了一些示例数据。在 add_annonation 函数中,我还将 x 参数值从硬编码数字更新为 8 层的 x 轴位置参考。但是添加的 8 个文本仍然没有按预期与 8 个 sankey 列对齐。

标签: plotly sankey-diagram


【解决方案1】:

您的 definePosition() 函数似乎将您的节点标签放在预期的位置。它正在做它的工作。然而,节点并没有出现在它们应该出现的地方。

您正在尝试使用计算出的 node_xnode_y 向量来修复节点位置。然而,plotly 对手动节点位置的处理很挑剔,而且有点不透明。在某些情况下,它似乎会覆盖手动位置。我发现在您的情况下,在 node 中省略一个或两个参数 xy 将使您的节点与预期对齐。例如:

plot_ly(type='sankey',
        orientation = "h",
        arrangement = "snap",
        node = list (
          label = node_names,
          # x = node_x, # No longer setting node.x manually
          y = node_y,
          color = "steelblue",
          pad = 15,
          thinkness = 15,
          line = list(color = "black", width = 0.5)),
        link = list(source = links$source, target = links$target, value = links$value, color = "gainsboro")) %>%
  
  add_annotations(x=unique(node_x)[1],y=1,xref = "x",yref = "paper",text = "<b>step1</b>",xanchor = 'right',showarrow = F, align = "center") %>%
  add_annotations(x=unique(node_x)[2],y=1,xref = "x",yref = "paper",text = "<b>step2</b>",xanchor = 'right',showarrow = F, align = "center") %>%
  add_annotations(x=unique(node_x)[3],y=1,xref = "x",yref = "paper",text = "<b>step3</b>",xanchor = 'right',showarrow = F, align = "center") %>%
  add_annotations(x=unique(node_x)[4],y=1,xref = "x",yref = "paper",text = "<b>step4</b>",xanchor = 'right',showarrow = F, align = "center") %>%
  add_annotations(x=unique(node_x)[5],y=1,xref = "x",yref = "paper",text = "<b>step5</b>",xanchor = 'right',showarrow = F, align = "center") %>%
  add_annotations(x=unique(node_x)[6],y=1,xref = "x",yref = "paper",text = "<b>step6</b>",xanchor = 'right',showarrow = F, align = "center") %>%
  add_annotations(x=unique(node_x)[7],y=1,xref = "x",yref = "paper",text = "<b>step7</b>",xanchor = 'right',showarrow = F, align = "center") %>%
  add_annotations(x=unique(node_x)[8],y=1,xref = "x",yref = "paper",text = "<b>step8</b>",xanchor = 'right',showarrow = F, align = "center") %>%
  
  
  
  layout(
    title = "<b>Opportunity Marketing User Behavior Monitor(Prod environment)</b>",
    font = list(
      size = 10,
      color = 'grey'
    ),
    margin = list(
      l = 30,
      r = 50,
      b = 50,
      t = 100,
      pad = 20
    ),
    xaxis = list(showgrid = F, zeroline = F, visiable = F, showticklabels = F),
    yaxis = list(showgrid = F, zeroline = F, visiable = F, showticklabels = F)
  )

这个问题让我很困扰;我之前遇到过类似的事情,这就是为什么你的问题让我感兴趣(Why are fixed positions for nodes in a plotly sankey graph being overridden or ignored?)。我也开了一个issue about it on the plotly.r github

【讨论】:

  • 你好 even_of_the_hout - 非常感谢你的调查。我试过你的建议,它有效。最初,我尝试通过 node_x 和 node_y 来修复节点位置,但是在添加 annonations 时,我将 node_x 值分配给了 'x' 参数。所以我认为从 x 轴的角度来看,添加的文本和列通过 node-x 值定位在相同的 x 轴位置。但实际结果是他们不是。真是奇怪的行为。
猜你喜欢
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 2020-04-28
  • 2022-08-20
  • 1970-01-01
  • 2022-10-04
  • 1970-01-01
  • 2021-10-21
相关资源
最近更新 更多