How to make a googleVis multiple Sankey from a data.frame?

走远了吗. 提交于 2019-12-03 16:52:02

Function gvisSankey does accept mid-levels directly. These levels have to be coded in underlying data.

 source <- sample(c("NorthSrc", "SouthSrc", "EastSrc", "WestSrc"), 100, replace=T)
 mid <- sample(c("NorthMid", "SouthMid", "EastMid", "WestMid"), 100, replace=T)
 destination <- sample(c("NorthDes", "SouthDes", "EastDes", "WestDes"), 100, replace=T) 
 dummy <- rep(1,100) # For aggregation

Now, we'll reshape original data:

 library(dplyr)

 datSM <- dat %>%
  group_by(source, mid) %>%
  summarise(toMid = sum(dummy) ) %>%
  ungroup()

Data frame datSM summarises number of units from Source to Mid.

  datMD <- dat %>%
   group_by(mid, destination) %>%
   summarise(toDes = sum(dummy) ) %>%
   ungroup()

Data frame datMD summarises number of units from Mid to Destination. This data frame will be added to the final data frame. Data frame need to be ungroup and have same colnames.

  colnames(datSM) <- colnames(datMD) <- c("From", "To", "Dummy")

As the datMD is appended as the last one, gvisSankey will recognise the middle step automatically.

  datVis <- rbind(datSM, datMD)

  p <- gvisSankey(datVis, from="From", to="To", weight="dummy")
  plot(p)

Here is the plot:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!