Rでggplot2:棒グラフの作成コマンド

ggplot2
スポンサーリンク

棒の長さで数量の大小を表す、棒グラフの作成コマンド「geom_col」の紹介です。数量は最大値、最小値、平均値、中央値などがあります。ですので、場合によっては元データから計算する必要がありますので注意が必要です。

また、エラーバーとして標準誤差(standard error)を使用することがあります。「ggplot2」パッケージには平均、平均+標準誤差、平均-標準誤差を算出する「mean_se」コマンドが収録されているので利用してはいかがでしょうか。

スポンサーリンク

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

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

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

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

対象データ

下記のような、データが対象データです。「dplyr::group_by」コマンドでグループ化、「dplyr::summarise_all」コマンドで平均値、標準偏差を求めています。

## 対象データ
# 一変量_X or Y:No
# 二変量_X and Y:Yes
#   文字:Yes
#   数字:Yes

# 例
set.seed(1234)
Col_data <- data.frame(x = sample(LETTERS[c(1, 5, 8)],
                                  size = 100, replace = TRUE),
                       y = sample(c(1, 3, 6), size = 100,
                                  replace = TRUE),
                       Group = sample(LETTERS[2:3],
                                      size = 100,
                                      replace = TRUE)) %>%
  group_by(Group, x) %>%
  summarise_all(list(mean = mean, sd = sd,
                     # &#24179;&#22343;&#12392;&#27161;&#28310;&#35492;&#24046;&#31639;&#20986;&#12395;&#20415;&#21033;&#12394;&#12300;mean_se&#12301;&#12467;&#12510;&#12531;&#12489;
                     se = ggplot2::mean_se))

# &#20197;&#19979;&#12300;mean_se&#12301;&#12467;&#12510;&#12531;&#12489;&#12392;&#21516;&#12376;
#summarise_all(list(mean = mean, sd = sd,
                     # &#27161;&#28310;&#35492;&#24046;&#12434;&#35336;&#31639;
#                     se = function(.) sd(.)/sqrt(length(.)))) %>%
  # &#24179;&#22343;&plusmn;&#27161;&#28310;&#35492;&#24046;&#12434;&#35336;&#31639;
#  mutate(ymin = mean - se,
#         ymax = mean + se)

基本的なプロット

ggplot(Col_data, aes(x = x, y = mean)) +
  geom_col()

体裁の設定例

枠線色、塗色、積み上げ・グループ毎に横並びなどの表現方法の設定例です。

## &#26528;&#33394;&#12398;&#35373;&#23450;:color&#12458;&#12503;&#12471;&#12519;&#12531;
# &#21508;&#38917;&#30446;&#12398;&#12415;&#12398;&#22615;&#20998;&#12369;&#12399;Group&#12458;&#12503;&#12471;&#12519;&#12531;&#12434;&#20351;&#29992;&#12375;&#12394;&#12356;
ggplot(Col_data, aes(x = x, y = mean,
                     color = Group,
                     Group = Group)) +
  geom_col()

# &#22909;&#12415;&#12398;&#33394;&#12399;scale_color_manual&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#29992;&#12377;&#12427;
ggplot(Col_data, aes(x = x, y = mean,
                     color = Group,
                     Group = Group)) +
  geom_col(linewidth = rep(c(1, 6), time = 3)) +
  scale_color_manual(values = c("C" = "red", "B" = "yellow"))

## &#22615;&#33394;&#12398;&#35373;&#23450;:fill&#12458;&#12503;&#12471;&#12519;&#12531;
# &#21508;&#38917;&#30446;&#12398;&#12415;&#12398;&#22615;&#20998;&#12369;&#12399;Group&#12458;&#12503;&#12471;&#12519;&#12531;&#12434;&#20351;&#29992;&#12375;&#12394;&#12356;
ggplot(Col_data, aes(x = x, y = mean,
                     fill = Group,
                     Group = Group))+
  geom_col()

# &#22909;&#12415;&#12398;&#33394;&#12399;scale_fill_manual&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#29992;&#12377;&#12427;
ggplot(Col_data, aes(x = x, y = mean,
                     fill = Group,
                     Group = Group))+
  geom_col() +
  scale_fill_manual(values = c("C" = "blue", "B" = "yellow"))

## &#34920;&#29694;&#26041;&#27861;:position&#12458;&#12503;&#12471;&#12519;&#12531;
# "dodge","dodge2","stack"
ggplot(Col_data, aes(x = x, y = mean, fill = Group)) +
  geom_col(position = position_stack(reverse = TRUE)) +
  scale_fill_manual(values = c("C" = "blue", "B" = "red"))

ggplot(Col_data, aes(x = x, y = mean, fill = Group)) +
  geom_col(position = "dodge") +
  scale_fill_manual(values = c("C" = "blue", "B" = "red"))

ggplot(Col_data, aes(x = x, y = mean, fill = Group)) +
  geom_col(position = "dodge2") +
  scale_fill_manual(values = c("C" = "blue", "B" = "red"))

ggplot(Col_data, aes(x = x, y = mean, fill = Group)) +
  geom_col(position = "stack") +
  scale_fill_manual(values = c("C" = "blue", "B" = "red"))

## 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(Col_data, aes(x = x, y = mean, fill = Group)) +
  geom_col() +
  geom_hline(color = "red", linewidth = mean(Col_data$mean),
             yintercept = 3,
             show.legend = NA) 

エラーバーを付けるいくつかの方法

## &#12456;&#12521;&#12540;&#12496;&#12540;&#12434;&#20184;&#12369;&#12427;&#12356;&#12367;&#12388;&#12363;&#12398;&#26041;&#27861;
# &#12464;&#12523;&#12540;&#12503;&#20998;&#39006;&#26178;&#12398;&#12509;&#12452;&#12531;&#12488;&#12399;position_XXXXX&#12467;&#12510;&#12531;&#12489;&#12434;&#21033;&#29992;&#12377;&#12427;
# geom_pointrange&#12467;&#12510;&#12531;&#12489;
ggplot(Col_data, aes(x = x, y = mean, fill = Group)) +
  geom_col(position = "dodge2") +
  geom_pointrange(aes(ymin = mean - sd,
                      ymax = mean + sd),
                  position = position_dodge2(width = 0.9),
                  colour = "red")

# geom_errorbar&#12467;&#12510;&#12531;&#12489;
ggplot(Col_data, aes(x = x, y = mean, fill = Group)) +
  geom_col(position = "dodge2") +
  geom_errorbar(aes(ymin = se$ymin,
                    ymax = se$ymax),
                position = position_dodge2(width = 0.9,
                                           padding = 0.5),
                colour = "red")

# geom_crossbar&#12467;&#12510;&#12531;&#12489;
ggplot(Col_data, aes(x = x, y = mean, fill = Group)) +
  geom_col(position = position_dodge(width = 0.9)) +
  geom_crossbar(aes(ymin = se$ymin,
                    ymax = se$ymax),
                position = position_dodge2(padding = 1.2),
                fatten = 2.5, colour = "yellow")

# geom_linerange&#12467;&#12510;&#12531;&#12489;
ggplot(Col_data, aes(x = x, y = mean, fill = Group)) +
  geom_col(position = position_dodge(width = 0.9)) +
  geom_linerange(aes(ymin = mean - sd,
                     ymax = mean + sd),
                position = position_dodge2(width = 0.9),
                colour = "red")

作図例

・表現方法:positionオプション:geom_col(position = “dodge2”)

・エラーバーを付けるいくつかの方法:geom_errorbarコマンド

・その他、コマンド実行で作成できる棒グラフ


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

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