Sankey plots

Sankey plots are often used to illustrate flows, for example treatment changes in medicine. The plot above illustrates the treatment changes in asthma patients. Some small flows are omitted.

Thanks to network3D package doing sankey plots has become fun! By implementing java scripts they can be interactive.

There are however some traps when using this package. The major one is the changing colors from plot to plot. We need to pass them via d3.scaleOrdinal() function and take care of the missing ones. You can use the following code:

  ##define 21 of types for colors 
  types.list<-c("type 0","type 1","type 2","type 3",
                "type 4", "type 5 ","type 6 ","type 7", "type 8 ","type 9", 
                "type 10","type 11", "type 12", "type 13","type 14","type 15",
                "type 16","type 17","type 18","type 19","type 20")
  colors <- paste(colors.list[which.not.missing], collapse = '", "')
  types <- paste(types.list[which.not.missing], collapse = '", "')
  ##prepare colors to JS form in sankeyNetwork function
  colorJS <- paste('d3.scaleOrdinal() .domain(["', types, '"]).range(["', colors, '"])')

Then pass it via sankeyNetwork:

sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize= 12, nodeWidth = 10,
LinkGroup ="group")

It is a nice and very handy idea to combine multistate models approach to create the flows of interest (see etm package in R!).

library(etm) <- etm(data[data$time>0,], c("1", "2", "3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22"), tra, s=start,t=stop, = NULL)
  ##now we calculate probabilities from start to stop time
  ##Now we can make links for sankey plot<-round(for.plot$P*100,digits=0)

Since the outcome sankey plot is a widget, you can save it using the following code:

p<-sankeyNetwork(Links = links, Nodes = nodes,
                Source = "source", Target = "target",
                Value = "value", NodeID = "name",
                fontSize= 12, nodeWidth = 10,
                LinkGroup ="group")
saveWidget(p, pat, selfcontained = F, libdir = "lib")

Selfcontained=F will save it in the library together with the dependent java scripts. You can choose the Self-contained=T option and then the script will be included in the resulting html file.

Unfortunately there is no automatic way of obtaining numbers next to the node names. Therefore, you need to use a trick and paste the numbers as the part of the node name using paste() function.

The next nice thing about the network3D package is that it can be easily implemented in Shiny, so you can have your own dashboard for creating sankey plots.

If you need any help message me in the comments!

Have fun!

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني.

ثيمات ووردبريس مجانية