【发布时间】: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
)
【问题讨论】:
标签: r plotly r-plotly sankey-diagram