Rで解析:ggplot2でテキストをピッタリプロット「ggfittext」パッケージ

ggplot2パッケージでテキストをグラフのエリア内にプロットするのに便利なパッケージの紹介です。エリア内にピッタリ収まるように自動で文字サイズを調整してくれます。使い道が多いのではないでしょうか。

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

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

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

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

実行コマンド

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

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

###データ例の作成#####
#tidyverseパッケージがなければインストール
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}
tibble(Text = c("kara", "だ", "Ni", "いい", "MO", "の"),
       Value = sample(1:10, 6),
       x = rep(1:3, 2),
       y = rep(2:1, each = 3)) -> TestData
########

#文字のプロット調整:geom_fit_textコマンド,geom_bar_text
#中央揃え:reflow = TRUE/FALSE
#エリアに合わせて文字の拡大・縮小:grow = TRUE/FALSE
#文字の位置:place = "top","topright","right","bottomright","bottom","bottomleft"
#文字色の自動調整:contrast = TRUE/FALSE

#参考例1:grow = TRUE
ggplot(TestData, aes(x = x, y = y, fill = Value, label = Text)) +
  geom_tile() +
  scale_fill_gradient(low = "black", high = "yellow") +
  geom_fit_text(reflow = TRUE, grow = TRUE, contrast = TRUE)

#参考例2:grow = FALSE
ggplot(TestData, aes(x = x, y = y, fill = Value, label = Text)) +
  geom_tile() +
  scale_fill_gradient(low = "black", high = "yellow") +
  geom_fit_text(reflow = TRUE, grow = FALSE, contrast = TRUE)

#参考例3:grow = TRUE
ggplot(TestData, aes(x = x, y = Value, fill = Value, label = Text)) +
  geom_col(position = "dodge") +
  geom_bar_text(reflow = FALSE, grow = TRUE,
                contrast = TRUE, place = "topright")

#参考例4:grow = FALSE
ggplot(TestData, aes(x = x, y = Value, fill = Value, label = Text)) +
  geom_col(position = "dodge") +
  geom_bar_text(reflow = FALSE, grow = FALSE,
                contrast = TRUE, place = "topright")

出力例

・参考例1:grow = TRUE

・参考例2:grow = FALSE

・参考例3:grow = TRUE

・参考例4:grow = FALSE

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

Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.
タイトルとURLをコピーしました