我正在嘗試使用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)
在此處輸入圖片說明
我不是三角學的王牌,但這確實是改變箭頭方向所需要的。在我看來,這是理想的選擇-如果您可以調整松動箭頭,使它們水平而不是垂直。否則,為什么我的解決方案解決了標簽方向問題,但并沒有使圖表更具可讀性...
- 3 回答
- 0 關注
- 687 瀏覽
添加回答
舉報
0/150
提交
取消