データの流れを指標や時間軸で表現するのに便利なAlluvial diagramsです。「ggplot2」パッケージを使用しているので、「ggplot2」パッケージのコマンドがそのまま利用可能できます。
パッケージバージョンは0.1。実行コマンドはwindows 7およびOS X 10.11.2のR version 3.2.4で確認しています。
パッケージのインストール
下記コマンドを実行してください。
#パッケージのインストール install.packages("devtools") devtools::install_github("corybrunson/ggalluvial")
実行コマンドの紹介
詳細はコマンド、パッケージのヘルプを確認してください。
#パッケージの読み込み library("ggalluvial") ###データ例の作成##### n <- 30 TestData <- data.frame(Group = sample(paste0("Group", 1:3), n, replace = TRUE), Judge = sample(c("Yes", "No"), n, replace = TRUE), Device = sample(c("Pc", "Mobile", "Tablet"), n, replace = TRUE), Freq = sample(0:50, n, replace = TRUE)) ######## #帯グラフをプロット:geom_alluviumコマンド #出現頻度を指定:freqオプション #x軸方向の項目を指定:axisXオプション;項目数はXに数字で指定 AlluviumPlot <- ggplot(TestData, aes(freq = Freq, axis1 = Group, axis2 = Judge, axis3 = Device)) + #帯グラフのプロット #aesにfill,alpha,colorの設定が可能,項目をまたぐときは:を使用 geom_alluvium(aes(fill = Group:Judge, color = Device)) #プロット AlluviumPlot #棒グラフをプロット:geom_stratumコマンド #出現頻度を指定:freqオプション #x軸方向の項目を指定:axisXオプション;項目数はXに数字で指定 StratumPlot <- ggplot(TestData, aes(freq = Freq, axis1 = Group, axis2 = Judge, axis3 = Device)) + #棒グラフのプロット geom_stratum() + #テキストを追加 #statオプションに"stratum"を指定 geom_text(stat = "stratum") #プロット StratumPlot #geom_alluviumとgeom_stratumコマンドの組み合わせ ggplot(TestData, aes(freq = Freq, axis1 = Group, axis2 = Judge, axis3 = Device)) + geom_alluvium(aes(fill = Group:Judge, color = Device)) + geom_stratum() + geom_text(stat = "stratum") #facet_wrapコマンドを使用 ggplot(TestData, aes(freq = Freq, axis1 = Group, axis2 = Judge)) + geom_alluvium(aes(fill = Group:Judge, color = Device)) + geom_stratum() + geom_text(stat = "stratum") + facet_wrap(~ Device, scales = "free_y") + scale_x_continuous(breaks = 1:2, labels = c("Group", "Judge"))
出力例
・geom_alluviumとgeom_stratumコマンドの組み合わせ
少しでも、あなたのウェブや実験の解析が楽になりますように!!