本文へスキップ
からだにいいもの

Rのトピックスを中心に『まだ、まだ、知らない、役に立つ情報?』を発信します。

Rで解析:カテゴリ数が違っても棒グラフの幅をそろえる「ggwidth」パッケージの紹介

データ分析や可視化において、ggplot2は非常に便利なツールです。しかし、棒グラフでは、プロットごとに見た目の幅が異なってしまうことがあります。この問題を解決するパッケージの紹介です。

パッケージバージョンは0.1.1。Windows 11 x64 (build 26200)のR version 4.6.1で確認しています。

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

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

# パッケージのインストール
install.packages("ggwidth")
# パッケージの読み込み
library("ggwidth")
# コマンド例実行で必要なパッケージを読み込み
library("ggplot2")
library("dplyr")
library("patchwork")
スポンサーリンク

コマンド例

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

全ての図に適用する幅の倍率をまとめて設定:set_equiwidthコマンド

オプション意味初期値
equiwidth幅のスケーリング係数を指定(正の有限数値)1
# 等幅の設定
set_equiwidth(1)
set_equiwidth(0.75)
set_equiwidth(1.33)

カテゴリ数やパネルサイズが異なっても見た目の幅を統一:get_widthコマンド

オプション意味初期値
n方向(xまたはy)の軸におけるカテゴリ数を指定;ファセット分割したプロットでは、各ファセット中の最大値を指定するNULL
n_dodgeドッジ(位置をずらす)するカテゴリの数を指定;position_dodge(preserve = “single”)と組み合わせて使うことを想定NULL
orientationジオムの向きを指定;縦向きのジオムには”x”、横向きのジオムには”y”c(“x”, “y”)
equiwidth見た目の幅を制御するスケーリング係数を指定;1が基準で、大きくすると太く、小さくすると細く見える。NULLの場合はset_equiwidth()で設定した値(未設定なら1)が使われるNULL
panel_widthsパネル幅をgrid::unitオブジェクトで指定;NULLの場合は現在のテーマの値を使用NULL
panel_heightsパネル高さをgrid::unitオブジェクトで指定;NULLの場合は現在のテーマの値を使用NULL
# パネルの幅・高さを指定したテーマを設定
set_theme(
  theme_grey() +
    theme(panel.widths  = rep(unit(75, "mm"), 2)) +
    theme(panel.heights = rep(unit(50, "mm"), 2))
)
# 幅のスケーリング係数を初期化
set_equiwidth(1)

# カテゴリ数3の縦向き棒グラフ
p1 <- mpg |>
  ggplot(aes(x = drv)) +
  geom_bar(
    width = get_width(n = 3),
    colour = "black",
    fill = "grey",
  )

# カテゴリ数7の縦向き棒グラフ
p2 <- diamonds |>
  ggplot(aes(x = color)) +
  geom_bar(
    width = get_width(n = 7),
    colour = "black",
    fill = "grey",
  )

# カテゴリ数7の横向き棒グラフ
p3 <- diamonds |>
  ggplot(aes(y = color)) +
  geom_bar(
    width = get_width(n = 7, orientation = "y"),
    colour = "black",
    fill = "grey",
  )

# ドッジされた棒グラフ(カテゴリ数3、ドッジ数4)
p4 <- mpg |>
  ggplot(aes(x = drv, group = factor(cyl))) +
  geom_bar(
    position = position_dodge(preserve = "single"),
    width = get_width(n = 3, n_dodge = 4),
    colour = "black",
    fill = "grey",
  )

# 4つのプロットを並べて表示し、幅の見た目がそろっていることを確認
p1 + p2 + p3 + p4

# ファセット分割用のサンプルデータを作成
d <- tibble::tibble(
  continent = c("Europe", "Europe", "Europe", "Europe", "Europe",
                "South America", "South America"),
  country   = c("AT", "DE", "DK", "ES", "PK", "TW", "BR"),
  value     = c(10L, 15L, 20L, 25L, 17L, 13L, 5L)
)

# 各大陸の中でカテゴリ数が最大のものを取得
max_n <- d |>
  count(continent) |>
  pull(n) |>
  max()

# ファセットごとにカテゴリ数が異なっても、幅の見た目をそろえて表示
d |>
  mutate(country = forcats::fct_rev(country)) |>
  ggplot(aes(y = country, x = value)) +
  geom_col(
    width = get_width(n = max_n, orientation = "y"),
    colour = "black",
    fill = "grey",
  ) +
  facet_wrap(~continent, scales = "free_y") +
  scale_y_discrete(continuous.limits = c(1, max_n)) +
  coord_cartesian(reverse = "y", clip = "off")

# パネル幅を指定する場合
mpg |>
  ggplot(aes(x = drv)) +
  geom_bar(
    width = get_width(n = 3, panel_widths = unit(160, "mm")),
    colour = "black",
    fill = "grey",
  ) +
  theme(panel.widths = unit(160, "mm"))

・4つのプロットを並べて表示し、幅の見た目がそろっていることを確認

・ファセットごとにカテゴリ数が異なっても、幅の見た目をそろえて表示

・パネル幅を指定する場合


この記事が誰かの役に立ちますように。

スポンサーリンク
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.