英文:
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 <- 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")
# Combine the vectors into a dataframe
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) #, # 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 <- c(0, 1, 2, 3, 4)
input <- c(1, 1, 5, 4, 5)
value <- c(1, 2, 1, 3, 2)
year <- c(2020, 2020, 2021, 2021, 2021)
color <- c("red", "blue", "green", "yellow", "orange")
# Combine the vectors into a dataframe
df <- data.frame(output, input, value, year, color)
plot_ly(
type = "sankey",
orientation = "h",
valuesuffix = "TWh",
node = list(
pad = 10,
thickness = 20,
group = 1,
line = list(width = 0),
label = c("A", "B", "C", "D", "E", "G"),
color = 'lightgray'
),
link = list(
source = df$input,
target = df$output,
line = list(color = "black", width = 0.01),
value = df$value,
color = df$color
),
frame = df$year
)
答案2
得分: 0
根据Julian Selke的回答,解决方案是将字符串输入转换为数字,调整代码如下:
output <- c("A", "B", "C", "C", "B")
input <- c("B", "C", "A", "B", "A")
year <- c(2020, 2020, 2021, 2021, 2021)
color <- c("red", "blue", "green", "yellow", "orange")
# 将向量合并成数据框
df <- data.frame(output, input, value, year, color)
all_et <- unique(c(df$input, df$output))
source <- match(as.list(df$input), all_et) - 1
target <- 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 <- c("A", "B", "C", "C", "B")
input <- c("B", "C", "A", "B", "A")
year <- c(2020, 2020, 2021, 2021, 2021)
color <- c("red", "blue", "green", "yellow", "orange")
# Combine the vectors into a dataframe
df <- data.frame(output, input, value, year, color)
all_et <- unique(c(df$input, df$output))
source <- match(as.list(df$input), all_et) - 1
target <- 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
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论