为了账号安全,请及时绑定邮箱和手机立即绑定

R中的Sankey图?

R中的Sankey图?

明月笑刀无情 2019-12-10 10:53:24
我正在尝试使用R中的Sankey Diagram可视化我的数据流。我发现此博客文章链接到生成Sankey Diagram的R脚本,不幸的是,它是原始的并且有点受限制(请参见下面的示例代码和数据)。有谁知道其他脚本,或者甚至是软件包,它们更发达?我的最终目标是通过图表组件的相对大小来可视化数据流和百分比,就像这些Sankey Diagrams示例一样。我在r-help列表上发布了一个类似的问题,但是两周后没有任何回应,我在这里尝试了我的运气。谢谢,埃里克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/1423501sourc.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 messrm("inputs", "labels", "losses", "SankeyR", "sourc.https", "unit")用以上代码生成的Sankey图, 用上面的代码生成的Sankey图
查看完整描述

3 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

如果您想使用R进行操作,则最好的出价似乎是@Roman建议- 修改SankeyR函数。例如,下面是我的快速解决方法,只需将标签垂直放置,适当地偏移它们并减少输入引用的字体,以使其看起来更好。此修改仅更改SankeyR函数中的行171和223 :


    #line171 - change oversized font size of input label

    fontsize = max(0.5,frInputs[j]*1.5)#1.5 instead of 2.5 


    #line223 - srt changes from 35 to 90 to orient labels vertically, 

    #and offset adjusts them to get better alignment with arrows

    text(txtX, txtY, fullLabel, cex=fontsize, pos=4, srt=90, offset=0.1)

在此处输入图片说明


我不是三角学的王牌,但这确实是改变箭头方向所需要的。在我看来,这是理想的选择-如果您可以调整松动箭头,使它们水平而不是垂直。否则,为什么我的解决方案解决了标签方向问题,但并没有使图表更具可读性...


查看完整回答
反对 回复 2019-12-10
  • 3 回答
  • 0 关注
  • 576 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信