Plotly Sankey 图中的附加标签

Posted

技术标签:

【中文标题】Plotly Sankey 图中的附加标签【英文标题】:Additional Labels in Plotly Sankey Diagram 【发布时间】:2021-10-24 20:53:30 【问题描述】:

我在 R 中使用 Plotly 创建了一个桑基图,并希望在图的最左侧和最右侧添加垂直标签。这可能吗?我在 Plotly 中发现了注释的可能性,但无法让它与图表一起使用。

代码如下:

library(plotly)
fig <- plot_ly(
  type = "sankey",
  orientation = "h",
  
  node = list(
    label = c("Level 1", "Level 2", "Level 3",
              "Level 1", "Level 2", "Level 3", "Level 4", "Level 5"),
    color = c("rgba(34,139,34, 1)", "rgba(31, 119, 180, 1)", "rgba(255, 127, 14, 1)",
              "rgba(34,139,34, 1)", "rgba(31, 119, 180, 1)", "rgba(255, 127, 14, 1)", "rgba(148, 103, 189, 1)", "rgba(148, 103, 0, 1)"),
    pad = 20,
    thickness = 15,
    line = list(
      color = "black",
      width = 0.5
    )
  ),
  
  link = list(
    source = c(0,0,0,0,1,1,1,1,2,2,2,2),
    target = c(3,4,5,6,3,4,5,6,4,5,6,7),
    value =  c(482,52,1,1,20,127,19,1,1,1,3,1),
    color = c("rgba(34,139,34,0.4)", "rgba(34,139,34,0.4)", "rgba(34,139,34,0.4)", "rgba(34,139,34,0.4)",
              "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)",
              "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)")
  )
)
fig <- fig %>% layout(
  title = "Title of the Plot",
  font = list(
    size = 10
  )
)

fig

感谢您的帮助。

编辑:

我尝试使用“add_annotations”,但是一旦我将文本的 x 轴移动到低于 0 的位置(将文本向左移动),它就会消失:

%>% add_annotations(
  xref="paper",
  yref="paper",
  x=-0.1,
  y=0.5,
  text="Test",
  showarrow=FALSE
)

【问题讨论】:

【参考方案1】:

经过一番修改后,我自己找到了解决方案:

我在布局中添加了一些边距:

layout(
  title = "Name of the Plot",
  font = list(
    size = 10
  ),
  margin = list(
    r = 30,
    l = 30
  )

然后我使用“add_annotations”创建文本并使用“xshift”将其进一步向右/向左移动:

add_annotations(
  xref = "paper",
  yref = "paper",
  x = 1,
  y = 0.5,
  xshift = 25,
  text = "Text I wanted to add",
  font = list(
    size = 14
  ),
  showarrow = FALSE,
  textangle = 90

最终代码如下所示:

fig <- plot_ly(
  type = "sankey",
  orientation = "h",
  node = list(
    label = c("Level 1", "Level 2", "Level 3",
              "Level 1", "Level 2", "Level 3", "Level 4", "Level 5"),
    color = c("rgba(34,139,34, 1)", "rgba(31, 119, 180, 1)", "rgba(255, 127, 14, 1)",
              "rgba(34,139,34, 1)", "rgba(31, 119, 180, 1)", "rgba(255, 127, 14, 1)", "rgba(148, 103, 189, 1)", "rgba(214, 39, 40, 1)"),
    pad = 20,
    thickness = 15,
    line = list(
      color = "black",
      width = 0.5
    )
  ),
  
  link = list(
    source = c(0,0,0,1,1,1,1,2,2,2,2,2),
    target = c(3,4,5,3,4,5,6,3,4,5,6,7),
    value =  c(614,27,4,12,35,6,1,1,1,2,5,1),
    color = c("rgba(34,139,34,0.4)", "rgba(34,139,34,0.4)", "rgba(34,139,34,0.4)",
              "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)",
              "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)")
  )
) %>% layout(
  title = "Name of the plot",
  font = list(
    size = 10
  ),
  margin = list(
    r = 30,
    l = 30
  )
) %>% add_annotations(
  x = 1,
  y = 0.5,
  xshift = 25,
  text = "Text 1",
  font = list(
    size = 14
  ),
  showarrow = FALSE,
  textangle = 90
) %>% add_annotations(
  x = 0,
  y = 0.5,
  xshift = -25,
  text = "Text 2",
  font = list(
    size = 14
  ),
  showarrow = FALSE,
  textangle = -90
) %>% show()

【讨论】:

【参考方案2】:

不是答案,只是稍微改进代码的建议。

您可以一次性添加两个标签,而不是使用两次add_annotations

add_annotations(
  x = c(1,0),
  y = 0.5,
  xshift = c(25, -25),
  text = c("Text 1", "Text 2"),
  font = list(
    size = 14
  ),
  showarrow = FALSE,
  textangle = c(90, -90)
)

【讨论】:

以上是关于Plotly Sankey 图中的附加标签的主要内容,如果未能解决你的问题,请参考以下文章

Plotly:如何在 plotly express 折线图中更改图例的变量/标签名称?

仅在 Plotly 图中显示特定点的标签?

如何使用 Plotly 删除堆叠和分组条形图中的 x 轴刻度标签

如何在 Python 3 的 Plotly 中将其悬停在折线图中一次显示数据点及其标签?

桑基图中的标签

plotly sankey 图数据格式