admin管理员组文章数量:1122846
that is the demo data
data = structure(list(Species = c("Cinachyrella", "Cinachyrella", "Cinachyrella",
"Cinachyrella", "Cinachyrella", "Cinachyrella", "Cinachyrella",
"Cinachyrella", "Cinachyrella", "Cinachyrella", "Cinachyrella",
"Cinachyrella", "Cinachyrella", "Mycale grandis", "Mycale grandis",
"Mycale grandis", "Mycale grandis", "Mycale grandis", "Mycale grandis",
"Mycale grandis", "Mycale grandis", "Mycale grandis", "Mycale grandis",
"Mycale grandis", "Mycale grandis", "Mycale grandis", "Mycale grandis",
"U-1", "U-1", "U-1", "U-1", "U-1", "U-1", "U-1", "U-1", "U-1",
"U-2", "U-2", "U-2", "U-2", "U-2", "U-2", "U-2", "U-2", "U-2",
"U-2", "U-2", "U-2", "U-2", "U-3", "U-3", "U-3", "U-3", "U-3",
"U-3", "U-3", "U-3", "U-5", "U-5", "U-5", "U-5", "U-5", "U-5",
"U-5", "U-6", "U-6", "U-6", "U-6", "U-6", "U-6", "U-6", "U-6"
), Method = c("Method-1", "Method-1", "Method-1", "Method-1",
"Method-1", "Method-1", "Method-1", "Method-2", "Method-2", "Method-2",
"Method-2", "Method-2", "Method-2", "Method-1", "Method-1", "Method-1",
"Method-1", "Method-1", "Method-1", "Method-1", "Method-2", "Method-2",
"Method-2", "Method-2", "Method-2", "Method-2", "Method-2", "Method-1",
"Method-1", "Method-1", "Method-2", "Method-2", "Method-2", "Method-2",
"Method-2", "Method-2", "Method-1", "Method-1", "Method-1", "Method-1",
"Method-1", "Method-1", "Method-1", "Method-2", "Method-2", "Method-2",
"Method-2", "Method-2", "Method-2", "Method-1", "Method-1", "Method-1",
"Method-2", "Method-2", "Method-2", "Method-2", "Method-2", "Method-1",
"Method-1", "Method-2", "Method-2", "Method-2", "Method-2", "Method-2",
"Method-1", "Method-1", "Method-2", "Method-2", "Method-2", "Method-2",
"Method-2", "Method-2"), Component = c("Fiber", "Fragment", "PP",
"X0.200.µm", "X1000.5000.µm", "X200.500.µm", "X500.1000.µm",
"Fiber", "Foam", "Fragment", "X1000.5000.µm", "X200.500.µm",
"X500.1000.µm", "Fiber", "Fragment", "PP", "X0.200.µm", "X1000.5000.µm",
"X200.500.µm", "X500.1000.µm", "Fiber", "Fragment", "Line", "PP",
"X1000.5000.µm", "X200.500.µm", "X500.1000.µm", "Fiber", "X200.500.µm",
"X500.1000.µm", "Fiber", "Fragment", "X0.200.µm", "X1000.5000.µm",
"X200.500.µm", "X500.1000.µm", "Fiber", "Fragment", "PP", "X0.200.µm",
"X1000.5000.µm", "X200.500.µm", "X500.1000.µm", "Fiber", "Fragment",
"X0.200.µm", "X1000.5000.µm", "X200.500.µm", "X500.1000.µm",
"Fiber", "X1000.5000.µm", "X200.500.µm", "Fiber", "Fragment",
"X1000.5000.µm", "X200.500.µm", "X500.1000.µm", "Fiber", "X1000.5000.µm",
"Fiber", "Fragment", "X1000.5000.µm", "X200.500.µm", "X500.1000.µm",
"Fiber", "X200.500.µm", "Fiber", "Fragment", "PP", "X0.200.µm",
"X200.500.µm", "X500.1000.µm"), Total = c(6L, 2L, 1L, 3L, 3L,
1L, 1L, 18L, 1L, 1L, 12L, 4L, 4L, 12L, 3L, 4L, 3L, 7L, 3L, 2L,
7L, 3L, 2L, 2L, 5L, 4L, 1L, 2L, 1L, 1L, 4L, 4L, 4L, 1L, 1L, 2L,
4L, 3L, 1L, 2L, 3L, 1L, 1L, 13L, 1L, 1L, 8L, 2L, 3L, 3L, 1L,
2L, 4L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 1L, 2L, 1L, 2L)), class = "data.frame", row.names = c(NA,
-72L))
I have used the code below to make sankey 5 layer sankey image
library(networkD3)
library(dplyr)
# Map nodes for each stage
nodes <- data.frame(name = unique(c(
data$Species, # Stage 1: Species
data$Method, # Stage 2: Methods
c("Fragment", "Fiber", "Foam", "Line"), # Stage 3: Shapes
c("X0.200.µm", "X200.500.µm", "X500.1000.µm", "X1000.5000.µm"), # Stage 4: Size ranges
c("PP", "PE", "PS", "PET", "Rayon", "Cellulose", "Acrylic", "Resin", "Nylon", "PBS") # Stage 5: Materials
)))
# Create links for all stages
links <- bind_rows(
# Stage 1 -> Stage 2
data %>% mutate(source = match(Species, nodes$name) - 1,
target = match(Method, nodes$name) - 1,
value = Total) %>%
select(source, target, value),
# Stage 2 -> Stage 3
data %>% mutate(source = match(Method, nodes$name) - 1,
target = match(Component, nodes$name) - 1,
value = Total) %>%
select(source, target, value),
# Stage 3 -> Stage 4
data %>% filter(Component %in% c("Fragment", "Fiber", "Foam", "Line")) %>%
mutate(source = match(Component, nodes$name) - 1,
target = match(
case_when(
Component == "Fragment" ~ "X0.200.µm",
Component == "Fiber" ~ "X200.500.µm",
Component == "Foam" ~ "X500.1000.µm",
Component == "Line" ~ "X1000.5000.µm"
), nodes$name
) - 1,
value = Total) %>%
select(source, target, value),
# Stage 4 -> Stage 5
data %>% mutate(source = match(Component, nodes$name) - 1,
target = match(
case_when(
Component %in% c("PP", "PE", "PS", "PET", "Rayon",
"Cellulose", "Acrylic", "Resin", "Nylon", "PBS") ~ Component
), nodes$name
) - 1,
value = Total) %>%
select(source, target, value)
)
# Generate the Sankey diagram
sankey <- sankeyNetwork(
Links = links,
Nodes = nodes,
Source = "source",
Target = "target",
Value = "value",
NodeID = "name",
units = "Tons",
fontSize = 12,
nodeWidth = 30
)
sankey
but when use the sankey code the picture does not appear. This diagram will include five stages: Species, Method, Fragment/Fiber/Foam/Line (Stage 3), Size Range (Stage 4), and Material Type (Stage 5). Additionally, links will have consistent colors based on their source stages.
that is the demo data
data = structure(list(Species = c("Cinachyrella", "Cinachyrella", "Cinachyrella",
"Cinachyrella", "Cinachyrella", "Cinachyrella", "Cinachyrella",
"Cinachyrella", "Cinachyrella", "Cinachyrella", "Cinachyrella",
"Cinachyrella", "Cinachyrella", "Mycale grandis", "Mycale grandis",
"Mycale grandis", "Mycale grandis", "Mycale grandis", "Mycale grandis",
"Mycale grandis", "Mycale grandis", "Mycale grandis", "Mycale grandis",
"Mycale grandis", "Mycale grandis", "Mycale grandis", "Mycale grandis",
"U-1", "U-1", "U-1", "U-1", "U-1", "U-1", "U-1", "U-1", "U-1",
"U-2", "U-2", "U-2", "U-2", "U-2", "U-2", "U-2", "U-2", "U-2",
"U-2", "U-2", "U-2", "U-2", "U-3", "U-3", "U-3", "U-3", "U-3",
"U-3", "U-3", "U-3", "U-5", "U-5", "U-5", "U-5", "U-5", "U-5",
"U-5", "U-6", "U-6", "U-6", "U-6", "U-6", "U-6", "U-6", "U-6"
), Method = c("Method-1", "Method-1", "Method-1", "Method-1",
"Method-1", "Method-1", "Method-1", "Method-2", "Method-2", "Method-2",
"Method-2", "Method-2", "Method-2", "Method-1", "Method-1", "Method-1",
"Method-1", "Method-1", "Method-1", "Method-1", "Method-2", "Method-2",
"Method-2", "Method-2", "Method-2", "Method-2", "Method-2", "Method-1",
"Method-1", "Method-1", "Method-2", "Method-2", "Method-2", "Method-2",
"Method-2", "Method-2", "Method-1", "Method-1", "Method-1", "Method-1",
"Method-1", "Method-1", "Method-1", "Method-2", "Method-2", "Method-2",
"Method-2", "Method-2", "Method-2", "Method-1", "Method-1", "Method-1",
"Method-2", "Method-2", "Method-2", "Method-2", "Method-2", "Method-1",
"Method-1", "Method-2", "Method-2", "Method-2", "Method-2", "Method-2",
"Method-1", "Method-1", "Method-2", "Method-2", "Method-2", "Method-2",
"Method-2", "Method-2"), Component = c("Fiber", "Fragment", "PP",
"X0.200.µm", "X1000.5000.µm", "X200.500.µm", "X500.1000.µm",
"Fiber", "Foam", "Fragment", "X1000.5000.µm", "X200.500.µm",
"X500.1000.µm", "Fiber", "Fragment", "PP", "X0.200.µm", "X1000.5000.µm",
"X200.500.µm", "X500.1000.µm", "Fiber", "Fragment", "Line", "PP",
"X1000.5000.µm", "X200.500.µm", "X500.1000.µm", "Fiber", "X200.500.µm",
"X500.1000.µm", "Fiber", "Fragment", "X0.200.µm", "X1000.5000.µm",
"X200.500.µm", "X500.1000.µm", "Fiber", "Fragment", "PP", "X0.200.µm",
"X1000.5000.µm", "X200.500.µm", "X500.1000.µm", "Fiber", "Fragment",
"X0.200.µm", "X1000.5000.µm", "X200.500.µm", "X500.1000.µm",
"Fiber", "X1000.5000.µm", "X200.500.µm", "Fiber", "Fragment",
"X1000.5000.µm", "X200.500.µm", "X500.1000.µm", "Fiber", "X1000.5000.µm",
"Fiber", "Fragment", "X1000.5000.µm", "X200.500.µm", "X500.1000.µm",
"Fiber", "X200.500.µm", "Fiber", "Fragment", "PP", "X0.200.µm",
"X200.500.µm", "X500.1000.µm"), Total = c(6L, 2L, 1L, 3L, 3L,
1L, 1L, 18L, 1L, 1L, 12L, 4L, 4L, 12L, 3L, 4L, 3L, 7L, 3L, 2L,
7L, 3L, 2L, 2L, 5L, 4L, 1L, 2L, 1L, 1L, 4L, 4L, 4L, 1L, 1L, 2L,
4L, 3L, 1L, 2L, 3L, 1L, 1L, 13L, 1L, 1L, 8L, 2L, 3L, 3L, 1L,
2L, 4L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 1L, 2L, 1L, 2L)), class = "data.frame", row.names = c(NA,
-72L))
I have used the code below to make sankey 5 layer sankey image
library(networkD3)
library(dplyr)
# Map nodes for each stage
nodes <- data.frame(name = unique(c(
data$Species, # Stage 1: Species
data$Method, # Stage 2: Methods
c("Fragment", "Fiber", "Foam", "Line"), # Stage 3: Shapes
c("X0.200.µm", "X200.500.µm", "X500.1000.µm", "X1000.5000.µm"), # Stage 4: Size ranges
c("PP", "PE", "PS", "PET", "Rayon", "Cellulose", "Acrylic", "Resin", "Nylon", "PBS") # Stage 5: Materials
)))
# Create links for all stages
links <- bind_rows(
# Stage 1 -> Stage 2
data %>% mutate(source = match(Species, nodes$name) - 1,
target = match(Method, nodes$name) - 1,
value = Total) %>%
select(source, target, value),
# Stage 2 -> Stage 3
data %>% mutate(source = match(Method, nodes$name) - 1,
target = match(Component, nodes$name) - 1,
value = Total) %>%
select(source, target, value),
# Stage 3 -> Stage 4
data %>% filter(Component %in% c("Fragment", "Fiber", "Foam", "Line")) %>%
mutate(source = match(Component, nodes$name) - 1,
target = match(
case_when(
Component == "Fragment" ~ "X0.200.µm",
Component == "Fiber" ~ "X200.500.µm",
Component == "Foam" ~ "X500.1000.µm",
Component == "Line" ~ "X1000.5000.µm"
), nodes$name
) - 1,
value = Total) %>%
select(source, target, value),
# Stage 4 -> Stage 5
data %>% mutate(source = match(Component, nodes$name) - 1,
target = match(
case_when(
Component %in% c("PP", "PE", "PS", "PET", "Rayon",
"Cellulose", "Acrylic", "Resin", "Nylon", "PBS") ~ Component
), nodes$name
) - 1,
value = Total) %>%
select(source, target, value)
)
# Generate the Sankey diagram
sankey <- sankeyNetwork(
Links = links,
Nodes = nodes,
Source = "source",
Target = "target",
Value = "value",
NodeID = "name",
units = "Tons",
fontSize = 12,
nodeWidth = 30
)
sankey
but when use the sankey code the picture does not appear. This diagram will include five stages: Species, Method, Fragment/Fiber/Foam/Line (Stage 3), Size Range (Stage 4), and Material Type (Stage 5). Additionally, links will have consistent colors based on their source stages.
Share Improve this question edited Nov 23, 2024 at 16:31 CJ Yetman 8,8182 gold badges29 silver badges60 bronze badges asked Nov 21, 2024 at 12:15 KaziKazi 778 bronze badges 3 |1 Answer
Reset to default 0You have NA
s in the target
column of your links
data frame. For example, this works with your above data
sankeyNetwork(
Links = filter(links, !is.na(target)),
Nodes = nodes,
Source = "source",
Target = "target",
Value = "value",
NodeID = "name",
units = "Tons",
fontSize = 12,
nodeWidth = 30
)
The NA
s are introduced by this bit of code
# Stage 4 -> Stage 5
data %>% mutate(source = match(Component, nodes$name) - 1,
target = match(
case_when(
Component %in% c("PP", "PE", "PS", "PET", "Rayon",
"Cellulose", "Acrylic", "Resin", "Nylon", "PBS") ~ Component
), nodes$name
) - 1,
value = Total) %>%
select(source, target, value)
You either need to rethink that bit, or possibly you want/need the values in data$Component
to be included as nodes in the nodes
data frame also.
本文标签: How to make Sankey Images in RStack Overflow
版权声明:本文标题:How to make Sankey Images in R? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311017a1934587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
sankey
orprint(sankey)
? Where is the code from? – Friede Commented Nov 21, 2024 at 15:10ggalluvial
package? – Zé Loff Commented Nov 21, 2024 at 16:56