【问题标题】:Interactivity in Sankey chart in R using networkD3使用networkD3在R中的桑基图中的交互性
【发布时间】:2017-10-11 12:06:42
【问题描述】:

我希望在这个 sankey 图上实现 onClick,以便通过单击链接,我应该看到两个节点之间链接的详细信息。类似于 plotly_click 函数

library(networkD3)
nodes = data.frame("name" = 
                 c("r1", # Node 0
                   "r2", # Node 1
                   "r3", # Node 2
                   "r4", # Node 3
                   "r5", # Node 4
                   "r6", # Node 5
                   "r7", # Node 6
                   "Blood Test", # Node 7
                   "Check Out", # Node 8
                   "Discuss Results", # Node 9
                   "MRI Scan", # Node 10
                   "Registration", # Node 11
                   "Triage and Assessment", # Node 12
                   "X-ray"))# Node 13
links = as.data.frame(matrix(c(
0, 11, 500, # Each row represents a link. The first number
1, 12, 500, # represents the node being conntected from. 
2, 7, 237, # the second number represents the node connected to.
3, 10, 236,
4, 13, 261,
5, 9, 495,
6, 8, 492),# The third number is the value of the node

byrow = TRUE, ncol = 3))
names(links) = c("source", "target", "value")
sankeyNetwork(Links = links, Nodes = nodes,
          Source = "source", Target = "target",
          Value = "value", NodeID = "name",
          fontSize= 12, nodeWidth = 30)

【问题讨论】:

    标签: r plotly sankey-diagram htmlwidgets networkd3


    【解决方案1】:

    您可以使用htmlwidgets::onRender 函数添加点击事件。不清楚您想查看什么详细信息,但是,例如,当您单击链接时,它会在警告框中显示链接的值...

    library(htmlwidgets)
    
    sn <- sankeyNetwork(Links = links, Nodes = nodes,
                  Source = "source", Target = "target",
                  Value = "value", NodeID = "name",
                  fontSize = 12, nodeWidth = 30)
    
    clickJS <- 'd3.selectAll(".link").on("click", function(d){ alert(d.value); })'
    htmlwidgets::onRender(sn, clickJS)
    

    【讨论】:

    【解决方案2】:

    这是一个基于parset 包的有趣解决方案:

    devtools::install_github("timelyportfolio/parsetR")
    library(parsetR) 
    
    links$source <- as.character(factor(links$source, labels=nodes[1:7,1]))
    links$target <- as.character(factor(links$target, labels=nodes[8:14,1]))
    
    parset(links, dimensions = c('source', 'target'), 
           value = htmlwidgets::JS("function(d) {return d.value}"),  
           tension = 0.5)
    

    【讨论】:

    • 嗨,Marco,非常感谢您的回复,您能帮我制作一个 sankey 图表吗。另外,我正在寻找实现 on click 以查看 R 中闪亮的数据。 d = event_data("plotly_click"),你在networkD3或googleVis中有这个命令的替代品吗?
    • 嗨,Marco,请帮我处理这个链接。
    • 糟糕,我错了,这里是stackoverflow.com/questions/46972729/…
    • 请帮我写这篇文章,stackoverflow.com/questions/47812506/…
    猜你喜欢
    • 2017-10-23
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 2012-04-15
    • 2021-06-23
    • 1970-01-01
    相关资源
    最近更新 更多