Rで解析:フォレストプロットの体裁が簡単!!「forestploter」パッケージ

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

フォレストプロットの体裁が簡単にできるパッケージの紹介です。パッケージ利用のカギになるのはコマンドに適応したデータの作成でしょうか。紹介では2グループの信頼区間を表示するフォレストプロットを作成しています。実行コマンド内の「データ例の作成」を確認いただければコツを理解できると思います。

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

スポンサーリンク

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

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

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

実行コマンド

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

#必要パッケージの読み込み
library("forestploter")

###データ例の作成#####
#tidyverseパッケージがなければインストール
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}

TestData <- tibble(SubGroup = c("ALL", "System", "PC", "MOBILE",
                                "SEX", "Male", "Female"),
                   General = c(400, "", 150, 250, "", 150, 250),
                   Engineer = c(450, "", 300, 150, "", 150, 300),
                   HR_1 = c(1.125, NA, 2.00, 0.6, NA, 1.00, 1.2),
                   HR_2 = c(0.825, NA, 1.50, 0.3, NA, 0.85, 1.2),
                   Lower_1 = c(0.951, NA, 1.235, 0.523, NA, 0.547, 0.658),
                   Lower_2 = c(0.651, NA, 1.435, 0.223, NA, 0.447, 1.158),
                   Upper_1 = c(1.183, NA, 2.235, 0.723, NA, 1.147, 1.358),
                   Upper_2 = c(0.853, NA, 1.735, 0.523, NA, 1.047, 1.358),
                   Se_1 = c(0.193, NA, 0.235, 0.023, NA, 0.147, 0.358),
                   Se_2 = c(0.083, NA, 0.135, 0.003, NA, 0.047, 0.258)) %>%
  #&#12464;&#12523;&#12540;&#12503;&#21517;&#20197;&#22806;&#12399;&#12300;&#12452;&#12531;&#12487;&#12531;&#12488;&#12301;&#12434;&#25407;&#20837;&#12377;&#12427;&#20966;&#29702;
  mutate(SubGroup = ifelse("" == General, SubGroup,
                           paste0("   ", SubGroup))) %>%
  #&#12464;&#12521;&#12501;&#12434;&#27491;&#12375;&#12356;&#20301;&#32622;&#12395;&#34920;&#31034;&#12377;&#12427;&#12383;&#12417;&#12398;&#35519;&#25972;&#21015;
  mutate(" " = paste(rep(" ", 20), collapse = " ")) %>%
  #HR (95% CI)_1&#12398;&#24773;&#22577;
  mutate("HR (95% CI)_1" = ifelse(is.na(HR_1), "",
                                paste0(HR_1, " (", Lower_1, " to ", Upper_1, ")"))) %>%
  #HR (95% CI)_2&#12398;&#24773;&#22577;
  mutate("HR (95% CI)_2" = ifelse(is.na(HR_2), "",
                                  paste0(HR_2, " (", Lower_2, " to ", Upper_2, ")")))   
########

#&#12501;&#12457;&#12524;&#12473;&#12488;&#12503;&#12525;&#12483;&#12488;&#12398;&#12486;&#12540;&#12510;&#20316;&#25104;:forest_theme&#12467;&#12510;&#12531;&#12489;
#&#12381;&#12398;&#20182;&#12458;&#12503;&#12471;&#12519;&#12531;&#12399;&#12504;&#12523;&#12503;&#21442;&#29031;
tm <- forest_theme(base_size = 10, #&#12486;&#12461;&#12473;&#12488;&#12469;&#12452;&#12474;
                   refline_lty = "solid", #&#12522;&#12501;&#12449;&#12524;&#12531;&#12473;&#12521;&#12452;&#12531;&#32218;&#31278;
                   refline_col = "red", #&#12522;&#12501;&#12449;&#12524;&#12531;&#12473;&#12521;&#12452;&#12531;&#33394;
                   ci_pch = c(15, 18), #&#28857;&#25512;&#23450;&#20516;&#12471;&#12531;&#12508;&#12523;&#31278;&#39006;
                   ci_col = c("#377eb8", "#4daf4a"), #&#20449;&#38972;&#21306;&#38291;&#12398;&#12402;&#12370;&#33394;
                   ci_lty = c(1, 2),&#12288;#&#20449;&#38972;&#21306;&#38291;&#12398;&#12402;&#12370;&#32218;&#31278;
                   footnote_col = "blue", #&#12501;&#12483;&#12488;&#12494;&#12540;&#12488;&#33394;
                   legend_name = "Group", #&#20961;&#20363;&#21517;
                   legend_value = c("KARADA_1", "KARADA_2"), #&#12464;&#12523;&#12540;&#12503;&#21517;
                   vertline_lty = c("dashed", "dotted"), #&#22402;&#30452;&#32218;&#31278;
                   vertline_col = c("#d6604d", "blue") #&#22402;&#30452;&#32218;&#31278;
                   )

#&#12501;&#12457;&#12524;&#12473;&#12488;&#12503;&#12525;&#12483;&#12488;&#12398;&#20316;&#25104;:forest&#12467;&#12510;&#12531;&#12489;
#&#12503;&#12525;&#12483;&#12488;&#12487;&#12540;&#12479;&#12364;&#35079;&#25968;&#12398;&#22580;&#21512;&#12399;list&#24418;&#24335;&#12395;&#12377;&#12427;
ForestPlot <- forest(TestData[,c(1:3, 12:14)],
                     est = list(TestData$HR_1,
                                TestData$HR_2),
                     lower = list(TestData$Lower_1,
                                  TestData$Lower_2),
                     upper = list(TestData$Upper_1,
                                  TestData$Upper_2),
                     sizes = list(TestData$Se_1,
                                  TestData$Se_2),
                     ci_column = 4, #&#12503;&#12525;&#12483;&#12488;&#12434;&#25407;&#20837;&#12377;&#12427;&#21015;&#12398;&#20301;&#32622;
                     ref_line = 1, #&#12522;&#12501;&#12449;&#12524;&#12531;&#12473;&#12521;&#12452;&#12531;&#12398;&#22522;&#28310;
                     vert_line = c(0.7, 1.8), #&#22402;&#30452;&#32218;&#12398;&#36861;&#21152;
                     arrow_lab = c("General Better",
                                   "Engineer Better"),
                     footnote = "&#12487;&#12514;&#12487;&#12540;&#12479;&#12391;&#12377;",
                     theme = tm)



#&#12503;&#12525;&#12483;&#12488;
plot(ForestPlot)

#&#12501;&#12457;&#12524;&#12473;&#12488;&#12503;&#12525;&#12483;&#12488;&#20307;&#35009;&#12398;&#35519;&#25972;:edit_plot&#12467;&#12510;&#12531;&#12489;
###grid&#12497;&#12483;&#12465;&#12540;&#12472;&#12364;&#24517;&#35201;&#12391;&#12377;#####
#grid&#12497;&#12483;&#12465;&#12540;&#12472;&#12364;&#12394;&#12369;&#12428;&#12400;&#12452;&#12531;&#12473;&#12488;&#12540;&#12523;
if(!require("grid", quietly = TRUE)){
  install.packages("grid");require("grid")
}
#&#25991;&#23383;&#12434;&#36196;&#33394;,&#22826;&#23383;&#12395;&#12377;&#12427;:edit_plot&#12467;&#12510;&#12531;&#12489;
ColChange <- edit_plot(ForestPlot,
                       row = 3, #&#34892;&#12398;&#36984;&#25246;
                       gp = gpar(col = "red",
                                 fontface = "bold"))
plot(ColChange)

#&#32972;&#26223;&#12434;&#22615;&#12426;&#12388;&#12406;&#12377;:edit_plot&#12467;&#12510;&#12531;&#12489;
#which&#12458;&#12503;&#12471;&#12519;&#12531;:"background"&#12434;&#25351;&#23450;
FillChange <- edit_plot(ForestPlot, row = c(1, 5), which = "background",
                        gp = gpar(fill = "darkolivegreen1"))
plot(FillChange)

#&#12504;&#12483;&#12480;&#12540;&#12395;&#25991;&#23383;&#12434;&#25407;&#20837;:insert_text&#12467;&#12510;&#12531;&#12489;
#part&#12458;&#12503;&#12471;&#12519;&#12531;;"header"&#12434;&#25351;&#23450;
InsertHeader <- insert_text(ForestPlot,
                            text = "KARADA_GOOD",
                            col = 3:5, #&#25407;&#20837;&#20301;&#32622;
                            part = "header",
                            gp = gpar(col = "red", 
                                      fontface = "bold"))
plot(InsertHeader)

#&#25351;&#23450;&#12375;&#12383;&#34892;&#12395;&#25991;&#23383;&#12434;&#25407;&#20837;:insert_text&#12467;&#12510;&#12531;&#12489;
#part&#12458;&#12503;&#12471;&#12519;&#12531;;"body"&#12434;&#25351;&#23450;
InsertBody <- insert_text(ForestPlot,
                          text = "KARADA_GOOD",
                          row = 6, #&#25407;&#20837;&#21015;&#20301;&#32622;
                          just = "left", #&#25991;&#23383;&#12398;&#20301;&#32622;;"center","left","right"
                          gp = gpar(col = "red", 
                                    fontface = "bold"))
plot(InsertBody)

出力例

・フォレストプロットの作成:forestコマンド

・文字を赤色,太字にする:edit_plotコマンド

・背景を塗りつぶす:edit_plotコマンド

・ヘッダーに文字を挿入:insert_textコマンド

・partオプション;”body”を指定


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

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