2017-09-20 178 views
0

我正在嘗試生成一條河流,但是我看起來像邊緣的輸出來自每個條的錯誤側面,這使得它很難解釋。 enter image description here爲什麼我的河牀很奇怪?

我使用的數據是:

邊緣:

 N1  N2  Value 
1  off off.1 0.77149877 
2 weak off.1 0.47474747 
3 medium off.1 0.17537313 
4 strong off.1 0.03603604 
5  off low.1 0.06879607 
6 weak low.1 0.26262626 
7 medium low.1 0.13432836 
8 strong low.1 0.01351351 
9  off medium.1 0.12530713 
10 weak medium.1 0.23232323 
11 medium medium.1 0.54850746 
12 strong medium.1 0.20720721 
13 off strong.1 0.03439803 
14 weak strong.1 0.03030303 
15 medium strong.1 0.14179104 
16 strong strong.1 0.74324324 

節點:

 ID x y 
1  off 1 4 
2  weak 1 3 
3 medium 1 2 
4 strong 1 1 
5 off.1 2 4 
6 low.1 2 3 
7 medium.1 2 2 
8 strong.1 2 1 

和情節的代碼是:

library(RColorBrewer) 
library(riverplot) 
palette = paste0(brewer.pal(5, "Set2"), "60") 
styles = lapply(nodes$y, function(n) { 
    list(col = palette[n+1], lty = 0, textcol = "black") 
}) 
names(styles) = nodes$ID 
nlabels <- c(off = 'off', weak = 'weak', medium = 'medium',strong = 'strong', 
      off.1 = 'off', weak.1 = 'weak', medium.1 = 'medium', strong.1 = 'strong') 
x <- makeRiver(nodes, edges, node_styles = styles, node_labels = nlabels) 
plot(x) 
+0

你能否更詳細地解釋你的問題? –

+0

嗨@MarcoSandri。問題在於,通常在河流邊緣應組織邊緣,使頂部節點的鏈接從頂部離開,從第二個頂部離開第二個,等等。這意味着單個節點的邊不會交叉。然而,在這個陰謀這個規則似乎是相反的(見圖) –

+1

下面你可以找到我的建議,以生成一個圖(我希望)你在找什麼。讓我知道。 –

回答

2

問題可以解決編輯指定的情節節點的垂直位置(nodes_ypos選項):

x <- makeRiver(nodes, edges, node_styles = styles, node_labels = nlabels, 
       node_ypos=1:4) 
plot(x) 

enter image description here

使用的sankeyNetwork功能networkD3你可以得到類似的(互動)劇情:

edges <- read.table(text=" 
n  N1  N2  Value 
1  off off.1 0.77149877 
2 weak off.1 0.47474747 
3 medium off.1 0.17537313 
4 strong off.1 0.03603604 
5  off low.1 0.06879607 
6 weak low.1 0.26262626 
7 medium low.1 0.13432836 
8 strong low.1 0.01351351 
9  off medium.1 0.12530713 
10 weak medium.1 0.23232323 
11 medium medium.1 0.54850746 
12 strong medium.1 0.20720721 
13 off strong.1 0.03439803 
14 weak strong.1 0.03030303 
15 medium strong.1 0.14179104 
16 strong strong.1 0.74324324 
", header=T) 

nodes <- read.table(text=" 
n  ID x y 
1  off 1 4 
2  weak 1 3 
3 medium 1 2 
4 strong 1 1 
5 off.1 2 4 
6 low.1 2 3 
7 medium.1 2 2 
8 strong.1 2 1 
", header=T) 

nodes$ID <- as.character(nodes$ID) 
edges$N1 <- as.numeric(factor(edges$N1, levels=c("off","weak","medium","strong")))-1 
edges$N2 <- as.numeric(factor(edges$N2, levels=c("off.1","low.1","medium.1","strong.1")))+3 

library(networkD3) 
sankeyNetwork(Links = edges, Nodes = nodes, Source = "N1", 
      Target = "N2", Value = "Value", NodeID = "ID", 
      iterations=0, 
      units = "", fontSize = 12, nodeWidth = 30) 

enter image description here

+0

太棒了!謝謝你,完美的作品 –

相關問題