Rで解析:ggplotでツリーマップ「treemapify」パッケージ

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

「ggplot2」パッケージを利用して、ツリーマップを描写するのに便利なパッケージの紹介です。gganimateパッケージのtransition_timeコマンドを利用してgifアニメーションの作成例も「コマンドの紹介」最下部に紹介します。

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


スポンサーリンク

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

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

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



コマンドの紹介

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

#パッケージの読み込み:libraryコマンド
library("treemapify")

#ggplot2を利用するためにtidyverseパッケージを読み込み
#tidyverseパッケージがなければインストール
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}

###データ例の作成#####
n <- 24
TestData <- data.frame("SubGroup" = sample(c("KA", "RA", "DA"), n, replace = TRUE),
                       "ID" = sample(letters[1:24], n, replace = FALSE),
                       "Area" = 1:n, "Point" = 0.1:(0.1*n),
                       "Facet" = sample(letters[1:2], n, replace = TRUE))
########

#&#12484;&#12522;&#12540;&#12510;&#12483;&#12503;&#20316;&#25104;:geom_treemap&#12467;&#12510;&#12531;&#12489;
ggplot(TestData, aes(area = Area, fill = Point)) +
  geom_treemap() +
  scale_fill_gradient2()

#&#12484;&#12522;&#12540;&#12510;&#12483;&#12503;&#12395;&#12486;&#12461;&#12473;&#12488;&#12434;&#36861;&#21152;:geom_treemap_text&#12467;&#12510;&#12531;&#12489;
#&#26360;&#24335;&#35373;&#23450;:fontface&#12458;&#12503;&#12471;&#12519;&#12531;
#&#12456;&#12522;&#12450;&#12469;&#12452;&#12474;&#12395;&#25991;&#23383;&#12469;&#12452;&#12474;&#12434;&#21512;&#12431;&#12379;&#12427;:geou&#12458;&#12503;&#12471;&#12519;&#12531;
ggplot(TestData, aes(area = Area, fill = Point, label = ID)) +
  geom_treemap() +
  geom_treemap_text(fontface = "bold", colour = "yellow",
                    place = "centre", grow = TRUE) +
  scale_fill_gradient2()

#&#12484;&#12522;&#12540;&#12510;&#12483;&#12503;&#12395;&#12469;&#12502;&#12464;&#12523;&#12540;&#12503;&#12398;&#24773;&#22577;&#12434;&#20184;&#19982;:geom_treemap_subgroup_XXX&#12467;&#12510;&#12531;&#12489;
###&#20998;&#21106;&#32218;&#12434;&#36861;&#21152;:geom_treemap_subgroup_border&#12467;&#12510;&#12531;&#12489;
###&#12486;&#12461;&#12473;&#12488;&#12434;&#36861;&#21152;:geom_treemap_subgroup_text&#12467;&#12510;&#12531;&#12489;
ggplot(TestData, aes(area = Area, fill = Point,
                     label = ID, subgroup = SubGroup)) +
  geom_treemap() + geom_treemap_subgroup_border(colour = "red") +
  geom_treemap_subgroup_text(place = "centre", grow = TRUE,
                             alpha = 0.8, colour = "red",
                             fontface = "italic", min.size = 0) +
  geom_treemap_text(fontface = "bold", colour = "green",
                    place = "centre", grow = TRUE, alpha = 0.5) +
  scale_fill_gradient2()

#&#21442;&#32771;:facet&#34920;&#31034;;ggplot2::facet_wrap&#12467;&#12510;&#12531;&#12489;&#12434;&#36969;&#24540;
ggplot(TestData, aes(area = Area, fill = SubGroup, label = ID)) +
  geom_treemap() +
  geom_treemap_text(fontface = "bold", colour = "white",
                    place = "centre", grow = TRUE) +
  facet_wrap( ~ Facet) + scale_fill_brewer(palette = "Set1")

###&#21442;&#32771;:gif&#12450;&#12491;&#12513;&#12540;&#12471;&#12519;&#12531;######
#gganimate&#12497;&#12483;&#12465;&#12540;&#12472;&#12364;&#12394;&#12369;&#12428;&#12400;&#12452;&#12531;&#12473;&#12488;&#12540;&#12523;
if(!require("gganimate", quietly = TRUE)){
  install.packages("gganimate");require("gganimate")
}
###&#12487;&#12540;&#12479;&#20363;&#12398;&#20316;&#25104;#####
n <- 150
TestData <- data.frame("SubGroup" = sample(c("KA", "RA", "DA"), n, replace = TRUE),
                       "ID" = sample(letters[1:24], n, replace = TRUE),
                       "Area" = 1:n, "Point" = 0.1:(0.1*n),
                       "Facet" = sample(letters[1:2], n, replace = TRUE),
                       "No" = sample(1:10, n, replace = TRUE))
########

#gganimate&#12497;&#12483;&#12465;&#12540;&#12472;&#12398;transition_time&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#12358;
AnimePlot <- ggplot(TestData, aes(area = Area, fill = Point, label = ID)) +
  geom_treemap() +
  geom_treemap_text(fontface = "bold", colour = "yellow",
                    place = "centre", grow = TRUE) +
  scale_fill_gradient2() +
  transition_time(No) +
  labs(title = "No: {frame_time}")

#&#20316;&#26989;&#12501;&#12457;&#12523;&#12480;&#12395;&#20445;&#23384;
anim_save("KARADA_treemap.gif", AnimePlot, nframes = 130)

出力例

・geom_treemapコマンド

・geom_treemap_textコマンド

・geom_treemap_subgroup_XXXコマンド

・ggplot2::facet_wrapコマンドを適応

・参考:「gganimateパッケージ」を利用したgifアニメーション


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

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