Rで解析:ggplot2のプロットを簡単アニメーション「gganimate」パッケージ

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

ggplot2のプロットを簡単にgifアニメーション化するパッケージの紹介です。非常に簡単に利用できるだけでなく、初期表示のシンボルの大きさや指定した色から本来の色へ変化するなどのシンボルへの視覚効果を設定することが可能です。大変面白いパッケージです。

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

スポンサーリンク

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

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

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

実行コマンド

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

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

###データ例の作成#####
set.seed(1234)
n <- 300
TestData <- data.frame(Year = sample(paste0(2021:2022), n, replace = TRUE),
                       Group = sample(paste0("Group", 1:3), n, replace = TRUE),
                       Data1 = runif(n) + sample(1:10, n, replace = TRUE),
                       Data2 = runif(n) * runif(n))
########

#&#12450;&#12491;&#12513;&#12540;&#12471;&#12519;&#12531;&#21270;&#12377;&#12427;ggplot&#12458;&#12502;&#12472;&#12455;&#12463;&#12488;&#12434;&#20316;&#25104;:transition_states&#12467;&#12510;&#12531;&#12489;
#&#12450;&#12491;&#12513;&#12540;&#12471;&#12519;&#12531;&#12398;&#38263;&#12373;&#12434;&#35373;&#23450;:transition_length&#12458;&#12503;&#12471;&#12519;&#12531;
#&#12450;&#12491;&#12513;&#12540;&#12471;&#12519;&#12531;&#38291;&#12398;&#38263;&#12373;&#12434;&#35373;&#23450;:state_length&#12458;&#12503;&#12471;&#12519;&#12531;
TestPlot_1 <- ggplot(TestData, aes(x = Data1, y = Data2,
                     size = Data1, color = Group)) +
  geom_point() +
  guides(size = "none") +
  transition_states(states = Year,
                    transition_length = 2,
                    state_length = 1)
#&#34920;&#31034;
TestPlot_1

#&#12450;&#12491;&#12513;&#12540;&#12471;&#12519;&#12531;&#12398;&#12452;&#12540;&#12472;&#12531;&#12464;&#35373;&#23450;:ease_aes&#12467;&#12510;&#12531;&#12489;
#&#12452;&#12540;&#12472;&#12531;&#12464;&#12392;&#12514;&#12487;&#12451;&#12501;&#12449;&#12452;&#12450;&#12434;&#12388;&#12394;&#12370;&#12390;&#35373;&#23450;&#12375;&#12414;&#12377;
#&#20363;:"quartic-in"
#&#12452;&#12540;&#12472;&#12531;&#12464;&#31278;&#39006;:quadratic,cubic,quartic,quintic,sine
#circular,exponential,elastic,back,bounce
#&#12514;&#12487;&#12451;&#12501;&#12449;&#12452;&#12450;&#31278;&#39006;:-in,-out,-in-out
TestPlot_2 <- TestPlot_1 +
  ease_aes(y = "bounce-out")
#&#34920;&#31034;
TestPlot_2

#&#12479;&#12452;&#12488;&#12523;&#12434;&#22793;&#21270;&#12377;&#12427;:ggplot2::ggtitle&#12467;&#12510;&#12531;&#12489;
#&#12300;glue&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;&#12398;&#35352;&#36848;&#12434;&#20351;&#12356;&#12414;&#12377;
TestPlot_3 <- TestPlot_2 +
  ggtitle("&#29694;&#22312;&#12398;&#34920;&#31034;&#12399; {closest_state} &#24180;&#12391;&#12377;&#12290;",
          subtitle = "Frame {frame} of {nframes}")
#&#34920;&#31034;
TestPlot_3

#&#33394;&#12293;&#12394;&#21177;&#26524;&#12434;&#20184;&#19982;
#enter_:&#34920;&#31034;&#21069;&#21177;&#26524;,exit_:&#34920;&#31034;&#24460;&#21177;&#26524;
TestPlot_4 <- TestPlot_3 +
  #&#12501;&#12455;&#12540;&#12489;&#12452;&#12531;&#21177;&#26524;&#12434;&#20184;&#19982;:enter_fade&#12467;&#12510;&#12531;&#12489;
  enter_fade() + 
  #&#21021;&#26399;&#34920;&#31034;&#12398;&#12471;&#12531;&#12508;&#12523;&#12398;&#22823;&#12365;&#12373;:enter_grow&#12467;&#12510;&#12531;&#12489;
  enter_grow(size = 10) +
  #&#21021;&#26399;&#12434;&#22522;&#28310;&#12363;&#12425;&#12378;&#12425;&#12375;&#12390;&#34920;&#31034;:enter_drift&#12467;&#12510;&#12531;&#12489;
  enter_drift(x_mod = -1.3, y_mod = 1.2) +
  #&#25351;&#23450;&#12375;&#12383;&#33394;&#12363;&#12425;&#26412;&#26469;&#12398;&#33394;&#12408;&#22793;&#21270;&#12377;&#12427;:enter_recolor&#12467;&#12510;&#12531;&#12489;
  enter_recolor(color = "black", fill = "black") +
  #&#12501;&#12455;&#12540;&#12489;&#12450;&#12454;&#12488;&#21177;&#26524;&#12434;&#20184;&#19982;:exit_fade&#12467;&#12510;&#12531;&#12489;
  exit_fade() +
  #&#26412;&#26469;&#12398;&#33394;&#12363;&#12425;&#25351;&#23450;&#12375;&#12383;&#33394;&#12408;&#22793;&#21270;&#12377;&#12427;:exit_recolo&#12467;&#12510;&#12531;&#12489;
  exit_recolor(color = "red") +
  #&#25351;&#23450;&#12375;&#12383;&#22522;&#28310;&#12408;&#12378;&#12425;&#12375;&#12390;&#34920;&#31034;:exit_drift&#12467;&#12510;&#12531;&#12489;
  exit_drift(x_mod = 10) +
  #&#25351;&#23450;&#12375;&#12383;&#12471;&#12531;&#12508;&#12523;&#12398;&#22823;&#12365;&#12373;&#12408;&#22793;&#21270;:exit_shrink&#12467;&#12510;&#12531;&#12489;
  exit_shrink(size = 10)

#&#34920;&#31034;
TestPlot_4

###&#12362;&#12414;&#12369;:BoxPlot#####
ggplot(TestData, aes(x = Group, y = Data2,
                     fill = Group)) +
  geom_boxplot() +
  guides(size = "none") +
  transition_states(states = Year,
                    transition_length = 2,
                    state_length = 1)

出力例

・色々な効果を付与:実行コマンドのTestPlot_4で表示されます。

・おまけ:BoxPlot


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

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