LoginSignup
11
11

More than 5 years have passed since last update.

ggplot2でbarの間にスペースを追加する

Last updated at Posted at 2015-08-07

2016/04/12追記: ggplot2 v2.1.0に対応させるため一部修正しました

Q

ヒストグラムを作る時、x軸はfactorのlevelsで並ぶと思うのですが、`position = "dodge"などの場合、どうやったらbarの間のスペースを追加あるいは調整できるのでしょうか。

NG_example.R
library(ggplot2)

Treatment <- rep(c('T','C'),each=2)
Gender <- rep(c('M','F'),2)
Response <- sample(1:100,4)
df <- data.frame(Treatment, Gender, Response)

hist <- ggplot(df, aes(x=Gender, y=Response, fill=Treatment))
hist + geom_bar(position = "dodge", stat = "identity") + scale_y_continuous(limits = c(0, 
    100), name = "") 

NG_example.R-1.png

widthを使えばいいみたいですが、うまくあたりません。

A

このような設定を試してみてください:

OK_example.R
hist + geom_bar(width = 0.4, position = position_dodge(width = 0.5))

OK_example_fig-1.png

これは2つのwidthの設定が含まれています:

  • geom_bar内のwidthは、barの幅を決定
  • position_dodge内のwidthは、それぞれのbarの位置を決定

よって、最初に設定しているgeom_bar(width = 0.4)では、barの幅を0.4として設定しています。また、position_dodge(width = 0.5)では、この場合赤色と青色のbarの間隔を0.5として設定しています。

おそらく色々と説明するよりも、この2つの設定値を色々変更して試してみたほうが、あなたの理解を助けるでしょう。ぜひ色々試してみてください。

補足

※ 2015/8/7に指摘を受けましたので補足しました。@berobero11さん、@kohskeさんありがとうございました。

設定例の一覧

上と同じパターンで、2つのwidthを色々設定してみたパターンを一覧できるものを作成しました:

OK_example.R
plot <- ggplot(df, aes(x=Gender, y=Response, fill=Treatment)) + 
  scale_y_continuous(limits = c(0, 100), name = "") +
  theme(legend.position = "none") +
  facet_wrap(~p) +
  mapply(function(w, pw)
    geom_bar(data = transform(df, p=sprintf("w_%.02f_pw_%.02f", w, pw)), stat = "identity", width= w, position = position_dodge(width = pw)),
    w = c(0.6,0.6,0.6,0.3,0.3,0.3), pw = c(0.4,0.7,1.0,0.4,0.7,1.0))
plot

OK_example2a.R-1.png

上の段がgeom_bar(width = 0.6)で、下の段がgeom_bar(width = 0.4)です。また左から順番にposition_dodge(width = c(0.4,0.7,1.0))という設定にしています。

mapplyで設定している部分については、複数のggplot2要素を関数で渡したい を参考に作成しています。

errorbarについて

errorbarについても、position = position_dodge(width = 0.9)のように設定できます:

OK_example3.R
ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(am))) + 
  stat_summary(fun.data = mean_se, position = position_dodge(width = 0.9), geom = "linerange") +
  stat_summary(fun.y = mean, geom = "bar", position = position_dodge(width = 0.9))

OK_example3.R-1.png

この場合、最初のstat_summaryでエラーバーを設定し、次のstat_summaryでバーを設定しています。両方のposition = position_dodgge(width = )は揃えておきましょう。

(2016/04/12追記: 以下のコードでlayer()geom_params=stat_params=が使えなくなり、params=に統合されたようです

ただし、stat_summaryでバーの幅を変えたい場合は面倒になります。stat_summary(width = 0.9)といった設定は反映されないからです。幅をかえるには、layerからparams = list(width = 0.6)とパラメータを指定していく方法があります:

ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(am))) + 
  stat_summary(fun.data = mean_se, position = position_dodge(width = 0.8), geom = "linerange") +
  layer(geom = "bar", params = list(width = 0.6, fun.data = mean_se), stat = "summary", 
        position = position_dodge(width = 0.8))

OK_example4.R-1.png

layerに関する説明は省略しますが、params=list()などはlayerに設定値を与えたものを作成します。興味がある方は、R言語上級ハンドブックのsection-54をご覧ください。

参考

この記事は、以下の記事をベースに日本語化しています:

関連する公式ヘルプトピックス

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11