0

I'm trying to generate a riverplot, but the output that I have looks like the edges are coming from the wrong side of each bar which makes it difficult to interpret. enter image description here

The data I'm using is:

edges:

       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

nodes:

        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

and the code for the plot is:

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)
unknown
  • 853
  • 1
  • 10
  • 23
  • Could you explain in more detail your problem ? – Marco Sandri Sep 20 '17 at 14:24
  • Hi @MarcoSandri. The problem is that usually in a riverplot the edges should be organised such that the link to the top node leaves from the top and the second from the second top and so on. This means that the edges from a single node don't cross. However, in this plot this rule seems to be reversed (see photo) – unknown Sep 20 '17 at 14:59
  • 1
    Below you can find my proposal for generating a graph that is (I hope) what you are looking for. Let me know. – Marco Sandri Sep 20 '17 at 16:11

1 Answers1

2

The problem can be solved specifying the vertical positions of nodes on the plot (nodes_ypos option):

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

enter image description here

Using the sankeyNetwork function of the networkD3 you can get a similar (interactive) plot:

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

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58