如何为 Sankey 图中的线条赋予不同的颜色以显示不同的组?

how to give different color for the lines in the Sankey plot to show different groups?

我对 R 中的桑基图有疑问。所以基本上我想根据变量组为连接源节点和目标节点的线赋予不同的颜色。下面是我从 R 平台之一找到的代码。本质上,代码为您提供了情节,但连接线的颜色相似。我的问题是如何为线条赋予不同的颜色以了解特定组以特定颜色表示。

谢谢! 最好 广告

# Libraries
library(tidyverse)
library(viridis)
library(patchwork)
library(hrbrthemes)
library(circlize)

# Load dataset from github
data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/13_AdjacencyDirectedWeighted.csv", header=TRUE)
# Package
library(networkD3)

# I need a long format
data_long <- data %>%
  rownames_to_column %>%
  gather(key = 'key', value = 'value', -rowname) %>%
  filter(value > 0)
colnames(data_long) <- c("source", "target", "value")
data_long$target <- paste(data_long$target, " ", sep="")
data_long$group <- c(rep("A", 10), rep("B",7), rep("C", 8), rep("D", 10))

# From these flows we need to create a node data frame: it lists every entities involved in the flow
nodes <- data.frame(name=c(as.character(data_long$source), as.character(data_long$target)) %>% unique())

# With networkD3, connection must be provided using id, not using real name like in the links dataframe.. So we need to reformat it.
data_long$IDsource=match(data_long$source, nodes$name)-1 
data_long$IDtarget=match(data_long$target, nodes$name)-1

# Make the Network
sankeyNetwork(Links = data_long, Nodes = nodes,
              Source = "IDsource", Target = "IDtarget",
              Value = "value", NodeID = "name", 
              sinksRight=FALSE, nodeWidth=40, fontSize=13, nodePadding=20)

按照 networkD3::sankeyNetwork 文档中的示例,您可以向数据添加 links 变量并设置 LinkGroup 参数...

# Libraries
library(dplyr)
library(tidyr)
library(tibble)
library(networkD3)

# Load dataset from github
data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/13_AdjacencyDirectedWeighted.csv", header=TRUE)

data_long <- 
  data %>%
  rownames_to_column() %>%
  gather(key = 'key', value = 'value', -rowname) %>%
  filter(value > 0)

colnames(data_long) <- c("source", "target", "value")
data_long$target <- paste(data_long$target, " ", sep="")
data_long$group <- c(rep("A", 10), rep("B",7), rep("C", 8), rep("D", 10))

# From these flows we need to create a node data frame: it lists every entities involved in the flow
nodes <- data.frame(name=c(as.character(data_long$source), as.character(data_long$target)) %>% unique())

# With networkD3, connection must be provided using id, not using real name like in the links dataframe.. So we need to reformat it.
data_long$IDsource=match(data_long$source, nodes$name)-1 
data_long$IDtarget=match(data_long$target, nodes$name)-1


# Colour links
data_long$links$source <- sub(' .*', '',
                              data_long$nodes[data_long$links$source + 1, 'name'])


# Make the Network
sankeyNetwork(Links = data_long,
              Nodes = nodes,
              Source = "IDsource",
              Target = "IDtarget",
              Value = "value", 
              NodeID = "name", 
              sinksRight=FALSE,
              nodeWidth=40,
              fontSize=13,
              nodePadding=20,
              LinkGroup = 'source')

reprex package (v2.0.1)

于 2021-12-02 创建