任何人都知道为什么我的SankeyNetwork没有绘制图形(显然没有错误吗?

问题描述 投票:0回答:1

我编写了下一个代码,一切正常,但是当我希望grpah出现在查看器屏幕中时,它不会出现。

library(networkD3)

links <- read.csv("C:/Users/tul45082/Desktop/CMA/DNA/linksV2.csv")
nodes <- read.csv("C:/Users/tul45082/Desktop/CMA/DNA/nodesV2.csv")

links$group <- as.factor(c("type_a", "type_b", "type_c", "type_d", 
                            "type_e","type_a", "type_a", "type_a", "type_b", 
                            "type_b","type_b", "type_b", "type_b", "type_c", 
                            "type_c","type_c", "type_c", "type_c", "type_d", 
                            "type_d","type_d", "type_e", "type_e", "type_e"))

nodes$group <- as.factor(c("n1","n2","n2","n2","n2","n2","n3","n3","n3","n3",
                            "n3","n3","n3","n3","n3","n3","n3","n3","n3","n3",
                            "n3","n3","n3","n3","n3"))

my_color <- 'd3.scaleOrdinal() . domain(["type_a","type_b", "type_c","type_d","type_e"])
.range(["#F3EAD1","#C8AE81","#B38C5A","#A38550","#522915"])'


sn <- sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
              Target = "target", Value = "value", NodeID = "names", 
              colourScale = my_color, LinkGroup = "group", 
              NodeGroup="group",fontSize = 10,fontFamily = "Arial", 
              nodeWidth = 0.5,nodePadding = 10, sinksRight = FALSE)
sn

谢谢您的帮助!

r graph show sankey-diagram
1个回答
0
投票

似乎您的数据框并不完美。

首先,将您的数据与sankeyNetwork文档中显示的数据集进行比较

energy <- jsonlite::fromJSON('https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json')
str(energy)

要解决,我将使您的数据尽可能接近示例数据集。乍一看,它意味着:

CMA_DNA<-data.frame(links = read.csv("C:/Users/tul45082/Desktop/CMA/DNA/linksV2.csv"),
                    nodes = read.csv("C:/Users/tul45082/Desktop/CMA/DNA/nodesV2.csv"),
                    links$group = as.factor(c("type_a", "type_b", "type_c", "type_d", 
                           "type_e","type_a", "type_a", "type_a", "type_b", 
                           "type_b","type_b", "type_b", "type_b", "type_c", 
                           "type_c","type_c", "type_c", "type_c", "type_d", 
                           "type_d","type_d", "type_e", "type_e", "type_e")),
                    nodes$group = as.factor(c("n1","n2","n2","n2","n2","n2","n3","n3","n3","n3",
                           "n3","n3","n3","n3","n3","n3","n3","n3","n3","n3",
                           "n3","n3","n3","n3","n3")),
                    links$my_color = 'd3.scaleOrdinal() . domain(["type_a","type_b", "type_c","type_d","type_e"]).range(["#F3EAD1","#C8AE81","#B38C5A","#A38550","#522915"])')

str(CMA_DNA)
© www.soinside.com 2019 - 2024. All rights reserved.