R中的Sankey图?

编程入门 行业动态 更新时间:2024-10-11 03:25:43
本文介绍了R中的Sankey图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用R中的Sankey Diagram可视化数据流。

我发现。我还添加了一个屏幕截图,以便您了解它的外观。

#加载程序包库(networkD3) #负载能量投影数据#负载能量投影数据 URL<-paste0( cdn.rawgit/christophergandrud/networkD3 /, master / JSONdata / energy.json)能源<-jsonlite :: fromJSON(URL)#绘制 sankeyNetwork(Links = Energy $ links, Nodes = Energy $ nodes,Source = source, Target = target,Value = value,NodeID = name, units = TWh,fontSize = 12,nodeWidth = 30)

I am trying to visualize my data flow with a Sankey Diagram in R.

I found this blog post linking to an R script that produces a Sankey Diagram, unfortunately it's quite raw and somewhat limited (see below for sample code and data).

Does anyone know of other scripts—or maybe even a package—that is more developed? My end goal is to visualize both data flow and percentages by relative size of diagram components, like in these examples of Sankey Diagrams.

I posted a somewhat similar question on the r-help list, but after two weeks without any responses I'm trying my luck here on stackoverflow.

Thanks, Eric

PS. I'm aware of the Parallel Sets Plot, but that is not what I'm looking for.

# thanks to, tonybreyal.wordpress/2011/11/24/source_https-sourcing-an-r-script-from-github/ sourc.https <- function(url, ...) { # install and load the RCurl package if (match('RCurl', nomatch=0, installed.packages()[,1])==0) { install.packages(c("RCurl"), dependencies = TRUE) require(RCurl) } else require(RCurl) # parse and evaluate each .R script sapply(c(url, ...), function(u) { eval(parse(text = getURL(u, followlocation = TRUE, cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))), envir = .GlobalEnv) } ) } # from gist.github/1423501 sourc.https("raw.github/gist/1423501/55b3c6f11e4918cb6264492528b1ad01c429e581/Sankey.R") # My example (there is another example inside Sankey.R): inputs = c(6, 144) losses = c(6,47,14,7, 7, 35, 34) unit = "n =" labels = c("Transfers", "Referrals\n", "Unable to Engage", "Consultation only", "Did not complete the intake", "Did not engage in Treatment", "Discontinued Mid-Treatment", "Completed Treatment", "Active in \nTreatment") SankeyR(inputs,losses,unit,labels) # Clean up my mess rm("inputs", "labels", "losses", "SankeyR", "sourc.https", "unit")

Sankey Diagram produced with the above code,

解决方案

This plot can be created through the networkD3 package. It allows you to create interactive sankey diagrams. Here you can find an example. I also added a screenshot so you have an idea what it looks like.

# Load package library(networkD3) # Load energy projection data # Load energy projection data URL <- paste0( "cdn.rawgit/christophergandrud/networkD3/", "master/JSONdata/energy.json") Energy <- jsonlite::fromJSON(URL) # Plot sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", units = "TWh", fontSize = 12, nodeWidth = 30)

更多推荐

R中的Sankey图?

本文发布于:2023-07-18 14:18:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Sankey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!