我對 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)
uj5u.com熱心網友回復:
按照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 包(v2.0.1)于 2021 年 12 月 2 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/375830.html
標籤:r d3.js 桑基图 htmlwidgets 网络d3
上一篇:魚眼效果D3js影像幻燈片適用于矩形/物件,但不適用于影像檔案,錯誤:<影像>屬性x:預期長度,“NaN”
下一篇:如果未找到影像的回退
