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
  • What happens if you run sankey or print(sankey)? Where is the code from? – Friede Commented Nov 21, 2024 at 15:10
  • Have you tried the ggalluvial package? – Zé Loff Commented Nov 21, 2024 at 16:56
  • i don't know how execute 5 stages sankey in gg alluvial, could you help? i will acknowledge you. – Kazi Commented Nov 24, 2024 at 8:28
Add a comment  | 

1 Answer 1

Reset to default 0

You have NAs 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 NAs 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