Rで解析:ggplot2と組み合わせてUpSetプロット「ComplexUpset」パッケージ

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

複数の指標間の関係性を表現するUpSetプロットに情報の追加が簡単なパッケージの紹介です。UpSetプロットの表現を拡張できるのではないでしょうか。「ComplexUpset」パッケージ単独でも利用することができますが、「ggplot2」パッケージを利用して表現力のあるグラフの追加が可能です。

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

スポンサーリンク

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

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

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

実行コマンド

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

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

###データ例の作成#####
#tidyverseパッケージがなければインストール
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}
set.seed(1234)
n <- 300
TestData <- tibble(Group = sample(paste0("Group", 1:4), n, replace = TRUE),
                   value = sample(c(1:10), n, replace = TRUE),
                   binary_Data_1 = sample(c(0:1), n, replace = TRUE),
                   binary_Data_2 = sample(c(0:1), n, replace = TRUE),
                   binary_Data_3 = sample(c(0:1), n, replace = TRUE),
                   binary_Data_4 = sample(c(0:1), n, replace = TRUE))

#binary_Data&#12398;&#34892;&#21517;&#12434;&#21462;&#24471;
Binary_Name <- TestData %>%
  select(contains("binary")) %>%
  colnames()
########

#upset&#12398;&#12503;&#12525;&#12483;&#12488;:upset&#12467;&#12510;&#12531;&#12489;
#Setsize&#12392;&#20132;&#24046;&#34892;&#21015;&#12398;&#12503;&#12525;&#12483;&#12488;&#27178;&#27604;&#29575;:width_ratio&#12458;&#12503;&#12471;&#12519;&#12531;
#Setsize&#12392;&#20132;&#24046;&#34892;&#21015;&#12398;&#12503;&#12525;&#12483;&#12488;&#32294;&#27604;&#29575;:height_ratio&#12458;&#12503;&#12471;&#12519;&#12531;
upset(TestData, Binary_Name, name = "Binary_Name",
      width_ratio = 0.25, height_ratio = 0.3)

#&#34920;&#31034;&#12377;&#12427;&#26368;&#23567;&#12398;&#20132;&#24046;&#12398;&#32207;&#25968;:min_size&#12458;&#12503;&#12471;&#12519;&#12531;
upset(TestData, Binary_Name, name = "Binary_Name",
      width_ratio = 0.25, height_ratio = 0.2, min_size = 20,
      wrap = TRUE, set_sizes = upset_set_size())

#&#34920;&#31034;&#12377;&#12427;&#26368;&#23567;&#12363;&#12425;&#12398;&#20132;&#24046;&#12398;&#25968;:min_degree&#12458;&#12503;&#12471;&#12519;&#12531;/max_degree
upset(TestData, Binary_Name, name = "Binary_Name",
      width_ratio = 0.25, height_ratio = 0.2, min_degree = 3)

#&#34920;&#31034;&#12377;&#12427;&#20132;&#24046;&#12398;&#25968;:n_intersections&#12458;&#12503;&#12471;&#12519;&#12531;
upset(TestData, Binary_Name, name = "Binary_Name",
      n_intersections = 10)

#&#12300;ggploy2&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;&#12392;&#32068;&#12415;&#21512;&#12431;&#12379;&#12383;&#21033;&#29992;&#20363;
#upset&#12398;&#20307;&#35009;&#12434;&#25972;&#12360;&#12427;:base_annotations&#12458;&#12503;&#12471;&#12519;&#12531;
#upset&#12408;&#12398;&#12450;&#12494;&#12486;&#12540;&#12471;&#12519;&#12531;&#12398;&#20307;&#35009;&#12434;&#25972;&#12360;&#12427;:annotations&#12458;&#12503;&#12471;&#12519;&#12531;
upset(TestData, Binary_Name, name = "Binary_Name",
      width_ratio = 0.25, height_ratio = 0.3,
      base_annotations = list(
        #Intersection size&#12398;&#20307;&#35009;
        'Intersection size'= intersection_size(
          #&#12486;&#12461;&#12473;&#12488;&#12398;&#33394;
          text_colors = c(on_background = "brown", on_bar = "yellow"),
          #&#12486;&#12461;&#12473;&#12488;&#12398;&#20301;&#32622;,&#22238;&#36578;
          text = list(c(vjust = -0.1, hjust = -0.1, ngle = 45))
          )
        ),
      annotations = list(
        #annotations&#12398;&#20307;&#35009;,ggplot2&#12398;&#12467;&#12510;&#12531;&#12489;&#12364;&#21033;&#29992;&#12391;&#12365;&#12414;&#12377;
        #intersection size&#12398;&#12503;&#12525;&#12483;&#12488;&#12395;&#21512;&#12431;&#12379;&#12427;:intersection&#12434;&#35373;&#23450;&#12377;&#12427;
        #&#19978;&#27573;&#12398;annotations
        "value" = ggplot(mapping = aes(x = intersection,
                                       y = value,
                                       group = intersection,
                                       fill = intersection)) +
          geom_boxplot() +
          theme(legend.position = "none"),
        #&#19979;&#27573;&#12398;annotations
        "value_2" = ggplot(mapping = aes(fill = Group)) +
          geom_bar(stat = "count", position = "stack")
        )
      )

出力例

・upsetのプロット:upsetコマンド

・表示する最小の交差の総数:min_sizeオプション

・表示する最小からの交差の数:min_degreeオプション/max_degree

・表示する交差の数:n_intersectionsオプション

・「ggploy2」パッケージと組み合わせた利用例


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

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