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

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

Plotly Sankey diagram animated frame same for different steps

问题

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

以下是重现错误的代码:

  output <- c("A", "B", "C", "D", "E")
  input <- c("B", "B", "G", "E", "G")
  value <- c(1, 2, 1, 3, 2)
  year <- c(2020, 2020, 2021, 2021, 2021)
  color <- c("red", "blue", "green", "yellow", "orange")

  # 将向量组合成数据框
  df <- data.frame(output, input, value, year, color)

  all_et <- as.list(unique(c(df$input, df$output)))
  source <- as.list(match(as.list(df$input), all_et) - 1)
  target <- as.list(match(as.list(df$output), all_et) - 1)

  plot_ly(
    type = "sankey",
    orientation = "h",
    valuesuffix = "TWh",

    node = list(
      pad = 10,
      thickness = 20,
      group = 1,
      line = list(width = 0),
      label = all_et,
      color = 'lightgray'
    ),
    link = list(
      source = source,
      target = target,
      line = list(color = "black", width = 0.01),
      value = as.list(df$value),
      color = as.list(df$color) #, # 需要由 plot_ly 进一步开发:等待新版本 js 2020.04.29
    ),
    frame = ~df$year
  )

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

谢谢你!

英文:

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

  output &lt;- c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;)
  input &lt;- c(&quot;B&quot;, &quot;B&quot;, &quot;G&quot;, &quot;E&quot;, &quot;G&quot;)
  value &lt;- c(1, 2, 1, 3, 2)
  year &lt;- c(2020, 2020, 2021, 2021, 2021)
  color &lt;- c(&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;, &quot;yellow&quot;, &quot;orange&quot;)

  # Combine the vectors into a dataframe
  df &lt;- data.frame(output, input, value, year, color)

  all_et &lt;- as.list(unique(c(df$input, df$output)))
  source &lt;- as.list(match(as.list(df$input), all_et) - 1)
  target &lt;- as.list(match(as.list(df$output), all_et) - 1)

  plot_ly(
    type = &quot;sankey&quot;,
    orientation = &quot;h&quot;,
    valuesuffix = &quot;TWh&quot;,

    node = list(
      pad = 10,
      thickness = 20,
      group = 1,
      line = list(width = 0),
      label = all_et,
      color = &#39;lightgray&#39;
    ),
    link = list(
      source = source,
      target = target,
      line = list(color = &quot;black&quot;, width = 0.01),
      value = as.list(df$value),
      color = as.list(df$color) #, # needs further development by plot_ly: waiting for new release js 2020.04.29
    ),
    frame = ~df$year
  )

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:

output &lt;- c(0, 1, 2, 3, 4)
input &lt;- c(1, 1, 5, 4, 5)
value &lt;- c(1, 2, 1, 3, 2)
year &lt;- c(2020, 2020, 2021, 2021, 2021)
color &lt;- c(&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;, &quot;yellow&quot;, &quot;orange&quot;)

# Combine the vectors into a dataframe
df &lt;- data.frame(output, input, value, year, color)

plot_ly(
  type = &quot;sankey&quot;,
  orientation = &quot;h&quot;,
  valuesuffix = &quot;TWh&quot;,
  
  node = list(
    pad = 10,
    thickness = 20,
    group = 1,
    line = list(width = 0),
    label = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;G&quot;),
    color = &#39;lightgray&#39;
  ),
  link = list(
    source = df$input,
    target = df$output,
    line = list(color = &quot;black&quot;, width = 0.01),
    value = df$value,
    color = df$color
  ),
  frame = df$year
)

答案2

得分: 0

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

output &lt;- c("A", "B", "C", "C", "B")
input &lt;- c("B", "C", "A", "B", "A")
year &lt;- c(2020, 2020, 2021, 2021, 2021)
color &lt;- c("red", "blue", "green", "yellow", "orange")

# 将向量合并成数据框
df &lt;- data.frame(output, input, value, year, color)

all_et &lt;- unique(c(df$input, df$output))
source &lt;- match(as.list(df$input), all_et) - 1
target &lt;- match(as.list(df$output), all_et) - 1

plot_ly(
  type = "sankey",
  orientation = "h",
  valuesuffix = "TWh",

  node = list(
    pad = 10,
    thickness = 20,
    group = 1,
    line = list(width = 0),
    label = all_et,
    color = 'lightgray'
  ),
  link = list(
    source = source,
    target = target,
    line = list(color = "black", width = 0.01),
    value = df$value,
    color = df$color
  ),
  frame = df$year
)
英文:

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

output &lt;- c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;, &quot;B&quot;)
input &lt;- c(&quot;B&quot;, &quot;C&quot;, &quot;A&quot;, &quot;B&quot;, &quot;A&quot;)
year &lt;- c(2020, 2020, 2021, 2021, 2021)
color &lt;- c(&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;, &quot;yellow&quot;, &quot;orange&quot;)

# Combine the vectors into a dataframe
df &lt;- data.frame(output, input, value, year, color)

  all_et &lt;- unique(c(df$input, df$output))
  source &lt;- match(as.list(df$input), all_et) - 1
  target &lt;- match(as.list(df$output), all_et) - 1

plot_ly(
  type = &quot;sankey&quot;,
  orientation = &quot;h&quot;,
  valuesuffix = &quot;TWh&quot;,

  node = list(
    pad = 10,
    thickness = 20,
    group = 1,
    line = list(width = 0),
    label = all_et,
    color = &#39;lightgray&#39;
  ),
  link = list(
    source = source,
    target = target,
    line = list(color = &quot;black&quot;, width = 0.01),
    value = df$value,
    color = df$color
  ),
  frame = df$year
)

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:

确定