Rで解析:川の流れのようにデータをプロット!「riverplot」パッケージの紹介

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

ウェブページの遷移だけでなく時間の変化・つながりを表現する方法にriverプロットという表現方法があります。riverプロットを作成できる「riverplot」パッケージを紹介します。

表現方法として非常に面白いと思います。

パッケージバージョンは0.10です。 実行コマンドはwindows 11のR version 4.1.2で確認しています。


スポンサーリンク

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

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

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

実行コマンドの紹介

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

#ライブラリの読み込み
library("riverplot")

#2色を指定し補色のカラーパレットを作成:ColRampPaletteコマンド
ColRampPalette <- colorRampPaletteAlpha(c("#4b61ba", "#a87963"))(10)
#&#12459;&#12521;&#12540;&#12497;&#12524;&#12483;&#12488;&#12398;&#12503;&#12525;&#12483;&#12488;
#&#36600;&#31684;&#22258;:x:0-2, y:0-2
#&#12503;&#12525;&#12483;&#12488;&#32972;&#26223;&#33394;&#12434;&#25351;&#23450;
par(bg = "#A9A9A9")
#&#22259;&#12398;&#25551;&#20889;
plot(0:2, 0:2, type = "n", axes = FALSE, xlab = "", ylab = "")
rasterImage(as.raster(ColRampPalette), 0, 2, 2, 0, interpolate = FALSE)

#&#24111;&#29366;&#12398;&#12521;&#12452;&#12531;&#12434;&#26082;&#23384;&#12398;&#12503;&#12525;&#12483;&#12488;&#12395;&#36861;&#21152;:curveseg&#12467;&#12510;&#12531;&#12489;
#curveseg(x0 = x&#36600;&#38283;&#22987;&#28857;, x1 = x&#36600;&#32066;&#20102;&#28857;, y0 = y&#36600;&#38283;&#22987;&#28857;, y1 = y&#36600;&#32066;&#20102;&#28857;,
#         width = &#24111;&#12398;&#22826;&#12373;, col = &#33394;, lty = &#26528;&#32218;&#12398;&#22826;&#12373;, form = "sin" or "line")
#ColRampPalette&#12467;&#12510;&#12531;&#12489;&#12391;&#20986;&#21147;&#12375;&#12383;&#22259;&#12395;&#36861;&#21152;
#form:sin
curveseg(x0 = 0, x1 = 1, y0 = 1, y1 = 0, width = 0.3,
         col= "#deb7a0", lty = 0, form = "sin")
#form:line
curveseg(x0 = 1, x1 = 2, y0 = 0, y1 = 1.7, width = 2 - 1.7,
         col= "#ffdd99", lty = 0, form = "line")

#&#12522;&#12496;&#12540;&#12503;&#12525;&#12483;&#12488;:makeRiver&#12467;&#12510;&#12531;&#12489;
#&#33521;&#35486;&#22823;&#25991;&#23383;&#12391;nodes&#12392;list&#12391;edges(&#35282;&#12522;&#12508;&#12531;&#12398;&#24133;)&#12434;&#35373;&#23450;
nodes <- c(LETTERS[1:4])
edges <- list(A = list(D = 10), B = list(D = 15), C = list(D = 8))
RiverData <- makeRiver(nodes, edges, node_xpos = c(1, 1, 1, 2),
                       node_labels = c(A = "&#12390;&#12377;&#12392;", B = "&#12486;&#12473;&#12488;",
                                       C = "TEST", D = "&#21512;&#27969;"),
                       node_styles = list(A = list(col = c("#4b61ba","#d9bb9c")),
                                          B = list(col = "#ffdd99")))
#MAC&#26085;&#26412;&#35486;&#25991;&#23383;&#21270;&#12369;&#38450;&#27490;
#par(family = "HiraKakuProN-W3")
#&#12503;&#12525;&#12483;&#12488;&#32972;&#26223;&#33394;&#12434;&#25351;&#23450;
par(bg = "#A9A9A9")
#&#22259;&#12398;&#25551;&#20889;
plot(RiverData)

#&#12522;&#12496;&#12540;&#12503;&#12525;&#12483;&#12488;&#12398;&#12487;&#12451;&#12501;&#12457;&#12523;&#12488;&#12398;&#33394;(&#12464;&#12524;&#12540;)&#12434;&#22793;&#26356;:default.style&#12467;&#12510;&#12531;&#12489;
ds <- default.style()
ds[["col"]] <- "red"
#&#12503;&#12525;&#12483;&#12488;&#32972;&#26223;&#33394;&#12434;&#25351;&#23450;
par(bg = "#A9A9A9")
#&#22259;&#12398;&#25551;&#20889;
plot(RiverData, default_style= ds)

#&#21442;&#32771;:riverplot.example&#12395;&#20351;&#29992;&#12373;&#12428;&#12390;&#12356;&#12427;&#12467;&#12510;&#12531;&#12489;&#12434;&#32057;&#20171;
#&#33394;&#12392;&#12521;&#12505;&#12523;&#12398;&#19968;&#37096;&#12434;&#22793;&#26356;&#12375;&#12390;&#12356;&#12414;&#12377;
#&#12487;&#12540;&#12479;&#12398;&#20316;&#25104;
ret <- list(nodes = data.frame(ID = LETTERS[1:8], x = c(1, 2, 2, 3, 3, 4, 5, 1),
                               labels = c(NA, NA, "&#12486;&#12473;&#12488;", rep(NA, 4), "&#12390;&#12377;&#12392;"),
                               stringsAsFactors = FALSE),
            styles = list(A = list(col = "#00990099", lty = 0, textcol = "white"),
                          H = list(col = "#4b61ba", textcol = "white"),
                          B = list(col = "#00006699", textcol = "white"),
                          F = list(col = "#ffdd99"), D = list(col = "#00FF0099")))

ret$edges <- data.frame(N1 = c("A", "A", "A", "H", "H", "H", "B", "B", "C", "C", "C"),
                        N2 = c("B", "C", "D", "D", "F", "G", "D", "F", "D", "E", "F"),
                        Value = c(10, 20, 5, 10, 10, 20, 5, 10, 20, 15, 10), stringsAsFactors = F)
rownames(ret$nodes) <- ret$nodes$ID

#&#20316;&#25104;&#12375;&#12383;&#12487;&#12540;&#12479;&#12434;riverplot&#12395;&#23550;&#24540;&#12377;&#12427;&#12424;&#12358;&#12395;&#12487;&#12540;&#12479;&#12434;&#22793;&#25563;
class(ret) <- c(class(ret), "riverplot")

#MAC&#26085;&#26412;&#35486;&#25991;&#23383;&#21270;&#12369;&#38450;&#27490;
#par(family = "HiraKakuProN-W3")
#&#12503;&#12525;&#12483;&#12488;&#32972;&#26223;&#33394;&#12434;&#25351;&#23450;
par(bg = "#A9A9A9")
#&#22259;&#12398;&#25551;&#20889;
plot(ret)

出力例

・ColRampPaletteコマンド

ColRampPalette

・curvesegコマンド(帯状のラインを既存のプロットに追加)
 ColRampPaletteコマンドで出力した図に追加しています。

curveseg

・makeRiverコマンド

makeRiver

・default.styleコマンド

default.style

・riverplot.exampleに使用されているコマンド

riverplot.example

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

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