gpt4 book ai didi

r - R 中的桑基图?

转载 作者:行者123 更新时间:2023-12-02 10:50:57 28 4
gpt4 key购买 nike

我正在尝试使用 R 中的桑基图可视化我的数据流。

我找到了this blog post链接到生成 Sankey 图的 R 脚本;不幸的是,它非常原始并且有些有限(请参阅下面的示例代码和数据)。

有人知道其他更发达的脚本吗?甚至可能是一个包?我的最终目标是通过图表组件的相对大小来可视化数据流和百分比,例如 these examples of Sankey Diagrams .

我发布了a somewhat similar question on the r-help list ,但两周后没有任何回复,我正在 stackoverflow 上试试运气。

谢谢,埃里克

PS。我知道 Parallel Sets Plot ,但这不是我要找的。
# thanks to, https://tonybreyal.wordpress.com/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 https://gist.github.com/1423501
sourc.https("https://raw.github.com/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 code above

最佳答案

该图可以通过networkD3包创建。它允许您创建交互式桑基图。在这里您可以找到example 。我还添加了一个屏幕截图,以便您了解它的样子。

# Load package
library(networkD3)

# Load energy projection data
# Load energy projection data
URL <- paste0(
"https://cdn.rawgit.com/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)

enter image description here

关于r - R 中的桑基图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9968433/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com