Rで解析:継続的なデータ表現に便利です「waterfalls」パッケージ

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

継続的なデータを表現するのに便利なwaterfall plotを簡単に作成できるパッケージです。データの最後尾に合計をプロットも可能です。なお、「waterfalls」パッケージは「ggplot2」パッケージのコマンドが適応可能です。

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

スポンサーリンク

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

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

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

実行コマンド

詳細はコマンド、パッケージのヘルプを確認してください。なお、「fill_by_sign = TRUE」時の塗色を変更するときは下記リンクの回答を参考にしてはいかがでしょうか。

・Chnaging ggplot colors and increasing the size of the text (inside the plot area)

https://stackoverflow.com/questions/59843492/chnaging-ggplot-colors-and-increasing-the-size-of-the-text-inside-the-plot-area

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

###データ例の作成#####
set.seed(1234)
n <- 10
TestData <- data.frame(Group = paste0("Group", 1:n),
                       Data1 = round(rnorm(n), 1))
########

#waterfall&#12503;&#12525;&#12483;&#12488;&#12398;&#20316;&#25104;:waterfall&#12467;&#12510;&#12531;&#12489;
waterfall(values =  TestData$Data1,
          labels = TestData$Group,
          #&#12496;&#12540;&#34920;&#31034;&#12486;&#12461;&#12473;&#12488;&#12469;&#12452;&#12474;
          rect_text_size = 1,
          #&#12496;&#12540;&#34920;&#31034;&#12486;&#12461;&#12473;&#12488;&#20301;&#32622;
          rect_text_labels_anchor = "centre",
          #&#12496;&#12540;&#22615;&#33394;;&#25351;&#23450;&#12377;&#12427;&#22580;&#21512;&#12399;&#12300;fill_by_sign = FALSE&#12301;
          #fill_colours = colorRampPalette(c("red",
          #                                  "#d5e6f2"))(nrow(TestData)),
          #&#12496;&#12540;&#22615;&#33394;&#12434;&#12503;&#12521;&#12473;/&#12510;&#12452;&#12490;&#12473;&#12391;&#20998;&#12369;&#12427;
          fill_by_sign = TRUE,
          #&#12496;&#12540;&#26528;&#32218;
          rect_border = "black",
          #&#12496;&#12540;&#24133;
          rect_width = 0.7,
          #&#21512;&#35336;&#12496;&#12540;&#12434;&#21491;&#31471;&#12395;&#34920;&#31034;:TRUE/FALSE
          calc_total = TRUE,
          #&#21512;&#35336;&#12496;&#12540;&#12398;&#34920;&#31034;&#20869;&#23481;
          total_rect_text = sum(TestData$Data1),
          #&#21512;&#35336;&#12496;&#12540;&#22615;&#33394;
          total_rect_color = "red",
          #&#21512;&#35336;&#12496;&#12540;&#34920;&#31034;&#12486;&#12461;&#12473;&#12488;&#33394;
          total_rect_text_color = "white",
          #&#12496;&#12540;&#38291;&#12398;&#31227;&#21205;&#32218;&#34920;&#31034;
          draw_lines = TRUE,
          #&#12496;&#12540;&#38291;&#12398;&#31227;&#21205;&#32218;&#34920;&#31034;&#20301;&#32622;
          lines_anchors = c("right", "left"),
          #&#12496;&#12540;&#38291;&#12398;&#31227;&#21205;&#32218;&#31278;
          linetype = "dashed",
          #0&#22522;&#28310;&#32218;&#12398;&#34920;&#31034;&#20869;&#23481;:"none","behind","front" 
          draw_axis.x = "behind",
          #Y&#36600;&#12473;&#12465;&#12540;&#12523;&#12398;&#33258;&#21205;&#35519;&#25972;
          scale_y_to_waterfall = TRUE,
          #ggplot&#12458;&#12502;&#12472;&#12455;&#12463;&#12488;&#12391;&#20445;&#23384;
          print_plot = TRUE,
          #ggplot&#12458;&#12502;&#12472;&#12455;&#12463;&#12488;&#21517;&#12398;&#25351;&#23450;
          ggplot_object_name = "TestWater")

###&#35430;&#12375;&#12395;&#20986;&#21147;&#12375;&#12383;ggplot&#12458;&#12502;&#12472;&#12455;&#12463;&#12488;&#12300;TestWater&#12301;&#12434;
###ggplot2&#12497;&#12483;&#12465;&#12540;&#12472;&#12391;&#32232;&#38598;
#ggplot2&#12434;&#21547;&#12416;tidyverse&#12497;&#12483;&#12465;&#12540;&#12472;&#12398;&#35501;&#12415;&#36796;&#12415;
#tidyverse&#12497;&#12483;&#12465;&#12540;&#12472;&#12364;&#12394;&#12369;&#12428;&#12400;&#12452;&#12531;&#12473;&#12488;&#12540;&#12523;
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}

#&#32232;&#38598;
TestWater +
  #X&#36600;Y&#36600;&#12434;&#20837;&#12428;&#26367;&#12360;
  coord_flip() +
  #&#12464;&#12521;&#12501;&#12479;&#12452;&#12488;&#12523;&#12434;&#36861;&#21152;
  labs(title = "&#12363;&#12425;&#12384;&#12395;&#12356;&#12356;&#12418;&#12398;_TEST") +
  #&#12486;&#12540;&#12510;&#12434;&#35373;&#23450;
  theme(title = element_text(colour = "#ffffe0"),
        axis.title = element_blank(),
        axis.text = element_text(colour = "#ffffe0"),
        axis.text.x = element_text(size = 8),
        axis.text.y = element_text(size = 10),
        panel.grid = element_blank(), 
        panel.background = element_rect(fill = "lightgray"),
        plot.background = element_rect(fill = "#0a0a0a"))
########

出力例

・waterfallコマンド

・ggplotオブジェクト「TestWater」をggplot2パッケージで編集


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

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