Rで解析:Sankey Plotに便利です「sankey」パッケージ

Rの解析に役に立つ記事
スポンサーリンク

作業フローやKPIなどの描写に便利なSankey PlotやSankey Diagramと呼ばれる手法があります。データの準備に癖があるかもしれませんが有用な表現方法だと思います。

プロット体裁に必要な情報は表でまとめていますので参考にしてください。

パッケージバージョンは1.0.2。windows11のR version 4.2.2で確認しています。

スポンサーリンク

パッケージのインストール

下記、コマンドを実行してください。

#パッケージのインストール
install.packages("sankey")

実行コマンド

詳細はコメント、パッケージのヘルプを確認してください。

#パッケージの読み込み
library("sankey")

###データ例の作成#####
n <- 15
#&#32080;&#21512;&#12487;&#12540;&#12479;&#12434;&#29992;&#24847;
#&#21516;&#12376;&#38263;&#12373;&#12398;2&#12388;&#12398;&#37197;&#21015;
V1 <- sample(letters[1:24], n, replace = TRUE)
V2 <- sample(letters[1:24], n, replace = TRUE)

#&#37197;&#21015;&#12363;&#12425;&#12464;&#12521;&#12501;&#29992;&#12398;&#12487;&#12540;&#12479;&#12434;&#20316;&#25104;
GraphText <- NULL
for(i in seq(V1)){
  
  GraphText <- paste(GraphText, V1[i], V2[i], "\n", sep = " ")
  
}
########

#edge&#35501;&#12415;&#36796;&#12415;&#12487;&#12540;&#12479;&#12398;&#20316;&#25104;
edges <- read.table(stringsAsFactors = FALSE, textConnection(GraphText))

#edge&#12487;&#12540;&#12479;&#12398;&#20316;&#25104;:make_sankey&#12467;&#12510;&#12531;&#12489;
PlotEdgeData <- make_sankey(edges = edges)
#&#27083;&#36896;&#12398;&#30906;&#35469;
str(PlotEdgeData)
List of 2
$ nodes:'data.frame':	16 obs. of  19 variables:
..$ id     : chr [1:16] "a" "t" "p" "v" ...
..$ col    : chr [1:16] "#2ca25f" "#2ca25f" "#2ca25f" "#2ca25f" ...
..$ shape  : chr [1:16] "rectangle" "rectangle" "rectangle" "rectangle" ...
..$ lty    : num [1:16] 1 1 1 1 1 1 1 1 1 1 ...
..$ srt    : num [1:16] 0 0 0 0 0 0 0 0 0 0 ...
..$ textcol: chr [1:16] "black" "black" "black" "black" ...
..$ label  : chr [1:16] "a" "t" "p" "v" ...
..$ adjx   : num [1:16] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
..$ adjy   : num [1:16] 1 1 1 1 1 1 1 1 1 1 ...
..$ boxw   : num [1:16] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 ...
..$ cex    : num [1:16] 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 ...
..$ size   : num [1:16] 2 1 1 3 2 1 2 1 1 2 ...
..$ x      : num [1:16] 0 0 0 0 0 1 1 1 1 1 ...
..$ bottom : num [1:16] -1.35 -6.05 -9.75 -13.45 -19.15 ...
..$ top    : num [1:16] -3.35 -7.05 -10.75 -16.45 -21.15 ...
..$ center : num [1:16] -2.35 -6.55 -10.25 -14.95 -20.15 ...
..$ pos    : num [1:16] 2 2 2 2 2 1 1 1 1 1 ...
..$ textx  : num [1:16] -0.1 -0.1 -0.1 -0.1 -0.1 1 1 1 1 1 ...
..$ texty  : num [1:16] -2.35 -6.55 -10.25 -14.95 -20.15 ...
$ edges:'data.frame':	15 obs. of  6 variables:
..$ V1        : chr [1:15] "w" "w" "v" "v" ...
..$ V2        : chr [1:15] "e" "c" "c" "l" ...
..$ colorstyle: chr [1:15] "gradient" "gradient" "gradient" "gradient" ...
..$ curvestyle: chr [1:15] "sin" "sin" "sin" "sin" ...
..$ col       : chr [1:15] "#99d8c9" "#99d8c9" "#99d8c9" "#99d8c9" ...
..$ weight    : num [1:15] 1 1 1 1 1 1 1 1 1 1 ...
- attr(*, "class")= chr [1:3] "sankey" "simplegraph_df" "simplegraph"

#&#12503;&#12525;&#12483;&#12488;:sankey&#12467;&#12510;&#12531;&#12489;
sankey(PlotEdgeData)

###&#12464;&#12521;&#12501;&#20307;&#35009;&#12399;edge&#12487;&#12540;&#12479;&#20869;&#23481;&#12434;&#22793;&#26356;&#12375;&#12414;&#12377;########
#&#12494;&#12540;&#12489;&#12398;&#33394;&#12434;&#22793;&#26356;
PlotEdgeData$nodes$col <- ifelse(PlotEdgeData$nodes$id %in% c("a", "p", "v", "h"), "blue", "#2ca25f")
#&#12456;&#12483;&#12472;&#12398;&#33394;&#12434;&#22793;&#26356;
PlotEdgeData$edges$col <- ifelse(PlotEdgeData$edges$V1 %in% c("a", "p", "v", "h"), "red", "yellow")
#&#12456;&#12483;&#12472;&#12398;&#22615;&#12426;&#12388;&#12406;&#12375;&#26041;&#27861;&#12434;&#22793;&#26356;
PlotEdgeData$edges$colorstyle <- ifelse(PlotEdgeData$edges$V1 %in% c("a", "p", "v", "h"), "col", "gradient")
#&#12456;&#12483;&#12472;&#12398;&#22615;&#12426;&#12388;&#12406;&#12375;&#26041;&#27861;&#12434;&#22793;&#26356;
PlotEdgeData$edges$curvestyle <- ifelse(PlotEdgeData$edges$V1 %in% c("a", "p", "v", "h"), "line", "sin")
#&#12503;&#12525;&#12483;&#12488;
sankey(PlotEdgeData)

プロット体裁に必要な情報

適応パラメーター内容
nodescolノード色
nodessizeサイズ
nodesxx軸のプロット位置,0から始まります
nodesyy軸のプロット位置,0から始まります
nodesshaperectangle, point, invisibleが設定可能
nodeslty線の種類,基本plotと同じオプションが適応できます
nodessrtテキストラベルの角度
nodestextcolテキストラベルの色
nodeslabelラベル内容
nodesadjxラベルX位置の調整
nodesadjyラベルY位置の調整
nodesboxwノードの箱サイズ
nodescexラベルサイズ
edgescolorstyleエッジ塗りつぶし方法,col(単色),gradient(グラデーション)が設定可能,gradientはノードの色が適応されます
edgescurvestyleエッジスタイル,sin(カーブ),line(直線)が設定可能
edgescolエッジ色を指定


出力例

・sankeyコマンド

sankey

・体裁を変更してプロット

Csankey

少しでも、あなたの解析が楽になりますように!!

タイトルとURLをコピーしました