【问题标题】:how to give different color for the lines in the Sankey plot to show different groups?如何为 Sankey 图中的线条赋予不同的颜色以显示不同的组?
【发布时间】:2021-12-02 11:51:00
【问题描述】:

我对 R 中的这个 Sankey 图有疑问。所以基本上我想根据变量组为连接源节点和目标节点的线赋予不同的颜色。以下是我从 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)

【问题讨论】:

    标签: r d3.js sankey-diagram htmlwidgets networkd3


    【解决方案1】:

    按照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 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2018-11-24
      相关资源
      最近更新 更多