R 以图方式固定一个节点坐标并保留一个节点的灵活坐标

R plotly fix one coordinate of nodes and leave one flexible

提问人:Moritz Schwarz 提问时间:11/8/2023 更新时间:11/14/2023 访问量:59

问:

赏金将在 1 小时后到期。这个问题的答案有资格获得 +50 声望赏金。莫里茨·施瓦茨(Moritz Schwarz)希望引起人们对这个问题的更多关注
我真的很想找到一个解决方案!

假设我使用 plotly 绘制桑基图:

enter image description here

library(plotly)
library(dplyr)

input <- tibble(label = c("A1", "A2", "B1", "B2", "C1", "C2"),
                color = c("blue", "blue", "red", "blue", "blue", "blue"),
                source = c(0,1,0,2,3,0),
                target = c(2,3,3,4,4,5),
                
                x = c(0.1,0.3,0.5,0.7,1,0.9),
                y = c(0.3,0.2,0.6,0.2,0.3,0.7),
                
                value =  c(8,4,2,8,4,2))

(fig <- plot_ly(
  type = "sankey",
  orientation = "h",
  
  node = list(
    label = input$label,
    color = input$color,
    pad = 150, # distance between nodes
    x = input$x, 
    y = input$y
  ),
  
  link = list(
    source = input$source, 
    target = input$target, 
    value = input$value
  )
))

在此示例中,我修复了坐标和坐标。但是假设我真的只关心两者中的一个(在这种情况下),并且想情节地选择第二个坐标。xyx

所以实际上我想做这样的事情:

input <- tibble(label = c("A1", "A2", "B1", "B2", "C1", "C2"),
                color = c("blue", "blue", "red", "blue", "blue", "blue"),
                source = c(0,1,0,2,3,0),
                target = c(2,3,3,4,4,5),
                
                x = c(0.1,0.3,0.5,0.7,1,0.9),
                y = NA,
                
                value =  c(8,4,2,8,4,2))

然后以图形方式忽略 x 和 y 坐标 - 事实上,我得到的结果与我只是删除节点列表中的 x 和 y 参数(注意例如 B2 的位置)相同。

enter image description here

所以它等价于这个:

(fig <- plot_ly(
  type = "sankey",
  orientation = "h",
  
  node = list(
    label = input$label,
    color = input$color,
    pad = 150 # distance between nodes
  ),
  
  link = list(
    source = input$source, 
    target = input$target, 
    value = input$value
  )
))

enter image description here

关于如何修复其中一个坐标的任何想法?

r 图地

评论


答:

0赞 Quinten 11/14/2023 #1

您可以在创建绘图后修改 y 坐标。这使您有机会为节点提供特定坐标,如下所示:plotly_build

library(tibble)
library(plotly)
library(dplyr)

input <- tibble(label = c("A1", "A2", "B1", "B2", "C1", "C2"),
                color = c("blue", "blue", "red", "blue", "blue", "blue"),
                source = c(0,1,0,2,3,0),
                target = c(2,3,3,4,4,5),
                
                x = c(0.1,0.3,0.5,0.7,1,0.9),
                y = NA,
                
                value =  c(8,4,2,8,4,2))

(fig <- plot_ly(
  type = "sankey",
  orientation = "h",
  
  node = list(
    label = input$label,
    color = input$color,
    pad = 150, # distance between nodes
    x = input$x, 
    y = input$y
  ),
  
  link = list(
    source = input$source, 
    target = input$target, 
    value = input$value
  )
))

your_fig <- plotly_build(fig)
your_fig$x$data[[1]]$node$y = c(NA, NA, NA, 1, NA, NA, NA)
your_fig

创建于 2023-11-14 with reprex v2.0.2

正如你所看到的,节点 B2 现在有一个不同的位置。而其他的仍然是一样的,无需手动更改。

评论

0赞 Moritz Schwarz 11/15/2023
谢谢Quinten - 这是一个非常有帮助的开始!但恐怕不完全是我想要的——现在 B2 仍然由一组 x 和 y 坐标定义。我希望所有节点都有一个固定的坐标,例如 x,然后所有坐标都使用 plotly 选择的 y 值
0赞 Moritz Schwarz 11/15/2023
有没有办法读出绘图发回的 x 和 y 坐标?