Customizing the sankey chart to cater large datasets

前端 未结 1 1224
一向
一向 2020-12-12 07:46

kindly run the script below, I have created a Sankey chart in R and plotly using data from \"patients\" dataset of the bupaR library. Please see the snapshot for reference.

相关标签:
1条回答
  • 2020-12-12 08:22

    This should do it

    sankeyData <- patients %>% 
      group_by(employee,handling) %>% 
      count()
    sankeyNodes <- list(label = c(sankeyData$employee,sankeyData$handling))
    trace2 <- list(
      domain = list(
        x = c(0, 1), 
        y = c(0, 1)
      ), 
      link = list(
        label = paste0("Case",1:nrow(sankeyData)), 
        source = sapply(sankeyData$employee,function(e) {which(e == sankeyNodes$label) }, USE.NAMES = FALSE) - 1, 
        target = sapply(sankeyData$handling,function(e) {which(e == sankeyNodes$label) }, USE.NAMES = FALSE) - 1, 
        value = sankeyData$n
      ), 
      node = list(label = sankeyNodes$label), 
      type = "sankey"
    )
    data2 <- list(trace2)
    p <- plot_ly()
    p <- add_trace(p, domain=trace2$domain, link=trace2$link, 
                   node=trace2$node, type=trace2$type)
    p
    
    0 讨论(0)
提交回复
热议问题