R plotly sankey - 如何使列注释与每一列对齐

R plotly sankey - how to make the column annonation aligned with each column

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

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

a <- read.csv(header = TRUE, text = "date,Data Center,Customer,companyID,source,target,value 
6/1/2021,dcA,customer1,companyID1,open_list_view_1,open_card_2,1 
6/1/2021,dcA,customer1,companyID1,open_card_2,edit_card_3,1
6/1/2021,dcA,customer1,companyID1,edit_card_3,save_card_4,1 
6/1/2021,dcA,customer1,companyID1,save_card_4,back_to_card_list_5,2 
6/1/2021,dcA,customer1,companyID1,back_to_card_list_5,show_more_6,1
6/1/2021,dcA,customer1,companyID1,show_more_6,view_introduction_7,1
6/1/2021,dcA,customer1,companyID1,view_introduction_7,scroll_down_8,2
6/2/2021,dcA,customer2,companyID2,open_list_view_1,open_card_2,3
6/2/2021,dcA,customer2,companyID2,open_card_2,edit_card_3,1
6/2/2021,dcA,customer2,companyID2,edit_card_3,save_card_4,4
6/2/2021,dcA,customer2,companyID2,save_card_4,back_to_card_list_5,2 
6/2/2021,dcA,customer2,companyID2,back_to_card_list_5,show_more_6,1
6/2/2021,dcA,customer2,companyID2,show_more_6,view_introduction_7,1
6/2/2021,dcA,customer2,companyID2,view_introduction_7,scroll_down_8,5")

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轴值呢??

实际结果:

预期结果:

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

这个问题让我很烦恼;我 运行 以前做过类似的事情,这就是为什么你的问题让我感兴趣(). I've opened an issue about it on the plotly.r github