Rでggplot2:箱ひげ図を作成「geom_boxplot」コマンド

ggplot2
スポンサーリンク

「ggplot2」パッケージで箱ひげ図を作成する方法の紹介です。箱ひげ図はBoxplotとも呼ばれ、データ分布を確認するのに大変便利な方法です。基本的な使い方の他にドットプロットを追加するコマンドを紹介します。

スポンサーリンク

「ggplot2」のインストールと読み込み

「tidyverse」をインストールして「ggplot2」パッケージを利用するのが簡単です。

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

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

データ例を作成

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

# 例
set.seed(1234)
Box_data <- data.frame(x = sample(LETTERS[c(1, 5, 8)],
                                  size = 100, replace = TRUE),
                       y = sample(1:100, size = 100,
                                  replace = TRUE),
                       Group = sample(LETTERS[2:3],
                                      size = 100,
                                      replace = TRUE))

基本的なプロット

「ggplot」コマンドに「geom_boxplot」コマンドを追加することで作図が可能です。

# &#35373;&#23450;&#12394;&#12375;&#12391;&#12503;&#12525;&#12483;&#12488;
ggplot(Box_data, aes(x = x, y = y)) +
  geom_boxplot()

体裁の設定例

外れ値プロットの書式設定、ノッチの設定、塗色、平均値をプロットし線でつなげる、ドッドプロットを追加するなどの例です。

# &#22806;&#12428;&#20516;&#12398;&#26360;&#24335;&#35373;&#23450;:outlier.XXXX&#12458;&#12503;&#12471;&#12519;&#12531;
ggplot(Box_data, aes(x = x, y = y)) +
  geom_boxplot(
    # &#12471;&#12531;&#12508;&#12523;&#32218;&#33394;;&#21021;&#26399;&#20516;:NULL
    outlier.color = "black",
    # &#12471;&#12531;&#12508;&#12523;&#22615;&#33394;;&#21021;&#26399;&#20516;:NULL
    outlier.fill = "red",
    # &#12471;&#12531;&#12508;&#12523;&#31278;&#39006;;&#21021;&#26399;&#20516;:19
    outlier.shape = 22,
    # &#12471;&#12531;&#12508;&#12523;&#12469;&#12452;&#12474;;&#21021;&#26399;&#20516;:1.5
    outlier.size = 3,
    # &#12471;&#12531;&#12508;&#12523;&#32218;&#12469;&#12452;&#12474;;&#21021;&#26399;&#20516;:0.5
    outlier.stroke = 1.5,
    # &#12471;&#12531;&#12508;&#12523;&#22615;&#36879;&#26126;&#24230;;&#21021;&#26399;&#20516;:NULL
    outlier.alpha = 1)

# &#12494;&#12483;&#12481;&#12434;&#35373;&#23450;&#12377;&#12427;:notch&#12458;&#12503;&#12471;&#12519;&#12531;,notchwidth&#12458;&#12503;&#12471;&#12519;&#12531;
ggplot(Box_data, aes(x = x, y = y)) +
  geom_boxplot(
    # &#12494;&#12483;&#12481;&#35373;&#23450;;&#21021;&#26399;&#20516;:FALSE
    notch = TRUE,
    # &#12494;&#12483;&#12481;&#12469;&#12452;&#12474;;&#21021;&#26399;&#20516;:0.5
    # 1> notchwidth >0&#12391;&#32094;&#12426;
    # 1< notchwidth&#12391;&#33192;&#24373;
    notchwidth = 1.2)

## &#22615;&#33394;&#12398;&#35373;&#23450;:fill&#12458;&#12503;&#12471;&#12519;&#12531;
# x&#12391;&#22615;&#33394;&#12434;&#20998;&#12369;&#12427;
# &#20961;&#20363;&#12398;&#34920;&#31034;&#35373;&#23450;:show.legend&#12458;&#12503;&#12471;&#12519;&#12531;
ggplot(Box_data, aes(x = x, y = y, fill = x)) +
  geom_boxplot(show.legend = FALSE)

# group&#12391;&#22615;&#33394;&#12434;&#20998;&#12369;&#12427;
ggplot(Box_data, aes(x = x, y = y, fill = Group)) +
  geom_boxplot()

# &#22909;&#12415;&#12398;&#33394;&#12399;scale_fill_manual&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#29992;&#12377;&#12427;
ggplot(Box_data, aes(x = x, y = y, fill = x))+
  geom_boxplot() +
  scale_fill_manual(values = c("A" = "blue", "H" = "yellow"))

## Y&#36600;&#12398;&#20516;&#12391;&#21442;&#32771;&#32218;&#12434;&#36861;&#21152;&#12377;&#12427;
# geom_hline&#12467;&#12510;&#12531;&#12489;&#12434;&#36861;&#21152;&#12377;&#12427;
ggplot(Box_data, aes(x = x, y = y, fill = x)) +
  geom_boxplot(show.legend = FALSE) +
  geom_hline(color = "red", alpha = 1,
             linewidth = 1,
             yintercept = mean(Box_data$y),
             show.legend = NA) 

## &#24179;&#22343;&#20516;&#12395;&#12471;&#12531;&#12508;&#12523;&#12434;&#12503;&#12525;&#12483;&#12488;&#12375;&#32218;&#12391;&#12388;&#12394;&#12370;&#12427;
# stat_summary&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#12358;&#12398;&#12364;&#31777;&#21336;&#12391;&#12377;
ggplot(Box_data, aes(x = x, y = y)) +
  geom_boxplot() +
  # &#24179;&#22343;&#20516;&#12395;&#12471;&#12531;&#12508;&#12523;&#12434;&#12503;&#12525;&#12483;&#12488;
  stat_summary(fun.y = "mean", geom = 'point',
               color = "red", shape = 16, size = 8) +
  # &#24179;&#22343;&#20516;&#12434;&#32218;&#12391;&#12388;&#12394;&#12370;&#12427;
  stat_summary(aes(group = 1),
               fun.y = "mean", geom = 'line',
               color = "red", size = 2)

## &#12503;&#12525;&#12483;&#12488;&#12434;&#27178;&#26041;&#21521;&#12395;&#12377;&#12427;
# coord_flip&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#29992;&#12375;&#12414;&#12377;
ggplot(Box_data, aes(x = x, y = y)) +
  geom_boxplot() +
  # &#24179;&#22343;&#20516;&#12395;&#12471;&#12531;&#12508;&#12523;&#12434;&#12503;&#12525;&#12483;&#12488;
  stat_summary(fun.y = "mean", geom = 'point',
               color = "red", shape = 16, size = 8) +
  # &#24179;&#22343;&#20516;&#12434;&#32218;&#12391;&#12388;&#12394;&#12370;&#12427;
  stat_summary(aes(group = 1),
               fun.y = "mean", geom = 'line',
               color = "red", size = 2) +
  # &#27178;&#26041;&#21521;&#12395;&#12377;&#12427;
  coord_flip()

## &#12489;&#12483;&#12488;&#12503;&#12525;&#12483;&#12488;&#12434;&#36861;&#21152;&#12377;&#12427;
# geom_dotplot()&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#29992;&#12375;&#12414;&#12377;
ggplot(Box_data, aes(x = x, y = y)) +
  geom_boxplot() +
  # &#24179;&#22343;&#20516;&#12395;&#12471;&#12531;&#12508;&#12523;&#12434;&#12503;&#12525;&#12483;&#12488;
  geom_dotplot(binaxis = "y", stackdir = "center",
               binwidth = 2.5, fill = "red")

## &#12464;&#12523;&#12540;&#12503;&#12395;&#20998;&#12369;&#12390;&#12503;&#12525;&#12483;&#12488;
# fill&#12395;&#12464;&#12523;&#12540;&#12503;&#12434;&#35373;&#23450;&#12377;&#12427;
ggplot(Box_data, aes(x = x, y = y, fill = Group)) +
  geom_boxplot()

# facet_wrap&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#29992;&#12377;&#12427;
ggplot(Box_data, aes(x = x, y = y)) +
  geom_boxplot() +
  facet_wrap(~Group)

## &#12486;&#12461;&#12473;&#12488;&#12434;&#36861;&#21152;&#12377;&#12427;
# coord_flip&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#29992;&#12375;&#12414;&#12377;
ggplot(Box_data, aes(x = x, y = y)) +
  geom_boxplot() +
  annotate("text", x = 1, y = 110,
           label = "KARADA-GOOD", color = "red")

少しでも、あなたの解析に役に立ちますように!

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