为什么我的河图看起来很奇怪?

why is my riverplot so weird looking?

我正在尝试生成河图,但我的输出看起来像是边缘来自每个条形的错误一侧,这使得它难以解释。

我使用的数据是:

边:

       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)

可以通过指定图上节点的垂直位置(nodes_ypos 选项)解决问题:

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

使用networkD3sankeyNetwork函数你可以得到类似的(交互式)情节:

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)