Plotly Sankey图的动画帧在不同步骤中相同。

huangapple go评论163阅读模式
英文:

Plotly Sankey diagram animated frame same for different steps

问题

我在尝试在不同帧上使用桑基图的动画时遇到了一个问题。尽管数据框中有不同的值,但两个步骤的桑基图都显示相同的内容。

以下是重现错误的代码:

  1. output <- c("A", "B", "C", "D", "E")
  2. input <- c("B", "B", "G", "E", "G")
  3. value <- c(1, 2, 1, 3, 2)
  4. year <- c(2020, 2020, 2021, 2021, 2021)
  5. color <- c("red", "blue", "green", "yellow", "orange")
  6. # 将向量组合成数据框
  7. df <- data.frame(output, input, value, year, color)
  8. all_et <- as.list(unique(c(df$input, df$output)))
  9. source <- as.list(match(as.list(df$input), all_et) - 1)
  10. target <- as.list(match(as.list(df$output), all_et) - 1)
  11. plot_ly(
  12. type = "sankey",
  13. orientation = "h",
  14. valuesuffix = "TWh",
  15. node = list(
  16. pad = 10,
  17. thickness = 20,
  18. group = 1,
  19. line = list(width = 0),
  20. label = all_et,
  21. color = 'lightgray'
  22. ),
  23. link = list(
  24. source = source,
  25. target = target,
  26. line = list(color = "black", width = 0.01),
  27. value = as.list(df$value),
  28. color = as.list(df$color) #, # 需要由 plot_ly 进一步开发:等待新版本 js 2020.04.29
  29. ),
  30. frame = ~df$year
  31. )

有没有办法解决这个问题?

谢谢你!

英文:

I have an issue when trying to use the animation of Sankeys through different frames. I get for both steps the same Sankey, despite having different values in the data frame.

[![Frame 2020][1]][1]
[![Frame 2021][2]][2]

The code to reproduce the error is here below

  1. output &lt;- c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;)
  2. input &lt;- c(&quot;B&quot;, &quot;B&quot;, &quot;G&quot;, &quot;E&quot;, &quot;G&quot;)
  3. value &lt;- c(1, 2, 1, 3, 2)
  4. year &lt;- c(2020, 2020, 2021, 2021, 2021)
  5. color &lt;- c(&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;, &quot;yellow&quot;, &quot;orange&quot;)
  6. # Combine the vectors into a dataframe
  7. df &lt;- data.frame(output, input, value, year, color)
  8. all_et &lt;- as.list(unique(c(df$input, df$output)))
  9. source &lt;- as.list(match(as.list(df$input), all_et) - 1)
  10. target &lt;- as.list(match(as.list(df$output), all_et) - 1)
  11. plot_ly(
  12. type = &quot;sankey&quot;,
  13. orientation = &quot;h&quot;,
  14. valuesuffix = &quot;TWh&quot;,
  15. node = list(
  16. pad = 10,
  17. thickness = 20,
  18. group = 1,
  19. line = list(width = 0),
  20. label = all_et,
  21. color = &#39;lightgray&#39;
  22. ),
  23. link = list(
  24. source = source,
  25. target = target,
  26. line = list(color = &quot;black&quot;, width = 0.01),
  27. value = as.list(df$value),
  28. color = as.list(df$color) #, # needs further development by plot_ly: waiting for new release js 2020.04.29
  29. ),
  30. frame = ~df$year
  31. )

Any idea how to solve this issue?

Thank you
[1]: https://i.stack.imgur.com/CIiQv.png
[2]: https://i.stack.imgur.com/LNnfL.png

答案1

得分: 1

以下是翻译好的部分:

"The links are expected to be numeric representations of source and target starting at index 0 (i.e. "A" = 0, "B" = 1, etc.)."
"链接应该是源和目标的数字表示,从索引0开始(即“A”= 0,“B”= 1,依此类推)。"

"Here is an adaption of your code that meets these requirements:"
"以下是符合这些要求的代码适应版本:"

"output <- c(0, 1, 2, 3, 4)"
"output <- c(0, 1, 2, 3, 4)"

"input <- c(1, 1, 5, 4, 5)"
"input <- c(1, 1, 5, 4, 5)"

"value <- c(1, 2, 1, 3, 2)"
"value <- c(1, 2, 1, 3, 2)"

"year <- c(2020, 2020, 2021, 2021, 2021)"
"year <- c(2020, 2020, 2021, 2021, 2021)"

"color <- c("red", "blue", "green", "yellow", "orange")"
"color <- c("red", "blue", "green", "yellow", "orange")"

"# Combine the vectors into a dataframe"
"# 将向量组合成数据框"

"df <- data.frame(output, input, value, year, color)"
"df <- data.frame(output, input, value, year, color)"

"plot_ly("
"plot_ly("

"type = "sankey","
"type = "sankey","

"orientation = "h","
"orientation = "h","

"valuesuffix = "TWh","
"valuesuffix = "TWh","

"node = list("
"node = list("

"pad = 10,"
"pad = 10,"

"thickness = 20,"
"thickness = 20,"

"group = 1,"
"group = 1,"

"line = list(width = 0),"
"line = list(width = 0),"

"label = c("A", "B", "C", "D", "E", "G"),"
"label = c("A", "B", "C", "D", "E", "G"),"

"color = 'lightgray'"
"color = 'lightgray'"
" ),"

"link = list("
"link = list("

"source = df$input,"
"source = df$input,"

"target = df$output,"
"target = df$output,"

"line = list(color = "black", width = 0.01),"
"line = list(color = "black", width = 0.01),"

"value = df$value,"
"value = df$value,"

"color = df$color"
"color = df$color"
" ),"

"frame = df$year"
"frame = df$year"
")"

英文:

The links are expected to be numeric representations of source and target starting at index 0 (i.e. "A" = 0, "B" = 1, etc.).

Here is an adaption of your code that meets these requirements:

  1. output &lt;- c(0, 1, 2, 3, 4)
  2. input &lt;- c(1, 1, 5, 4, 5)
  3. value &lt;- c(1, 2, 1, 3, 2)
  4. year &lt;- c(2020, 2020, 2021, 2021, 2021)
  5. color &lt;- c(&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;, &quot;yellow&quot;, &quot;orange&quot;)
  6. # Combine the vectors into a dataframe
  7. df &lt;- data.frame(output, input, value, year, color)
  8. plot_ly(
  9. type = &quot;sankey&quot;,
  10. orientation = &quot;h&quot;,
  11. valuesuffix = &quot;TWh&quot;,
  12. node = list(
  13. pad = 10,
  14. thickness = 20,
  15. group = 1,
  16. line = list(width = 0),
  17. label = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;G&quot;),
  18. color = &#39;lightgray&#39;
  19. ),
  20. link = list(
  21. source = df$input,
  22. target = df$output,
  23. line = list(color = &quot;black&quot;, width = 0.01),
  24. value = df$value,
  25. color = df$color
  26. ),
  27. frame = df$year
  28. )

答案2

得分: 0

根据Julian Selke的回答,解决方案是将字符串输入转换为数字,调整代码如下:

  1. output &lt;- c("A", "B", "C", "C", "B")
  2. input &lt;- c("B", "C", "A", "B", "A")
  3. year &lt;- c(2020, 2020, 2021, 2021, 2021)
  4. color &lt;- c("red", "blue", "green", "yellow", "orange")
  5. # 将向量合并成数据框
  6. df &lt;- data.frame(output, input, value, year, color)
  7. all_et &lt;- unique(c(df$input, df$output))
  8. source &lt;- match(as.list(df$input), all_et) - 1
  9. target &lt;- match(as.list(df$output), all_et) - 1
  10. plot_ly(
  11. type = "sankey",
  12. orientation = "h",
  13. valuesuffix = "TWh",
  14. node = list(
  15. pad = 10,
  16. thickness = 20,
  17. group = 1,
  18. line = list(width = 0),
  19. label = all_et,
  20. color = 'lightgray'
  21. ),
  22. link = list(
  23. source = source,
  24. target = target,
  25. line = list(color = "black", width = 0.01),
  26. value = df$value,
  27. color = df$color
  28. ),
  29. frame = df$year
  30. )
英文:

Based on the answer by Julian Selke, the solution is to transform the string inputs in numbers, adapting the code such as

  1. output &lt;- c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;, &quot;B&quot;)
  2. input &lt;- c(&quot;B&quot;, &quot;C&quot;, &quot;A&quot;, &quot;B&quot;, &quot;A&quot;)
  3. year &lt;- c(2020, 2020, 2021, 2021, 2021)
  4. color &lt;- c(&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;, &quot;yellow&quot;, &quot;orange&quot;)
  5. # Combine the vectors into a dataframe
  6. df &lt;- data.frame(output, input, value, year, color)
  7. all_et &lt;- unique(c(df$input, df$output))
  8. source &lt;- match(as.list(df$input), all_et) - 1
  9. target &lt;- match(as.list(df$output), all_et) - 1
  10. plot_ly(
  11. type = &quot;sankey&quot;,
  12. orientation = &quot;h&quot;,
  13. valuesuffix = &quot;TWh&quot;,
  14. node = list(
  15. pad = 10,
  16. thickness = 20,
  17. group = 1,
  18. line = list(width = 0),
  19. label = all_et,
  20. color = &#39;lightgray&#39;
  21. ),
  22. link = list(
  23. source = source,
  24. target = target,
  25. line = list(color = &quot;black&quot;, width = 0.01),
  26. value = df$value,
  27. color = df$color
  28. ),
  29. frame = df$year
  30. )

huangapple
  • 本文由 发表于 2023年2月13日 23:34:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定