2016-05-19 8 views
1

各ボックスプロットの下に観測数を追加したい(図のように、赤い四角形は不要)。 :) しかし、このタイプのボックスプロットに注釈を付ける方法はわかりません(下図参照)。 multiple boxplot annotate number of observationsggplotにグループ化されたboxplotを注釈付ける - boxplot以下の観測数を追加する

どのようにすればいいですか?

これは私がこの図をプロットするために使用したコードです。

ggplot(data=MIOT1, aes(stage, time, fill=resp)) + 
geom_boxplot(color= "black", lwd=0.3) + 
stat_summary(fun.y=mean, geom="point", shape=0, size=1, colour="black", position=position_dodge(width=0.75)) + 
scale_fill_manual(values=c("grey25", "grey50", "grey67")) + 
annotation_custom(mygrobA) + 
scale_y_continuous(limits=c(-10,124)) + 
theme(panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    strip.background = element_rect(colour="black"), 
    panel.border = element_rect(colour = "black", fill="transparent")) + 
xlab(bquote(' ')) + 
ylab(bquote('Minimum Consecutive Time (s)')) + 
labs(title="SATIATION\n") + 
theme(axis.title.y = element_text(colour="black",size=10,face="bold"), 
    axis.text.x = element_text(colour="black",size=8, face="plain"), 
    axis.text.y = element_text(colour="black",size=8, face="plain"), 
    axis.title.x = element_text(colour="black",size=10,face="bold")) + 
theme(panel.background = element_rect(fill = "white")) + 
theme(plot.title = element_text(lineheight=.8, size=10, face="bold")) + 
theme(legend.title=element_blank(), legend.key = element_rect(fill = NA, colour = NA)) + 
theme(legend.position="none") + 
theme(legend.background = element_rect(fill=NA)) + 
theme(plot.margin = unit(c(.25,.25,.0,.0), "cm"))<i> 

例DATA MIOT1は、数値変数(y軸)であり、Iは、二つのグループ化要素(開発stage- X軸)とレスポンス(応答しない、海岸、ラグーン)を考慮しています。

stage resp time 
pre  U  100 
pre  U  80 
pre  U  50 
pre  C  20 
flex  U  80 
flex  U  90 
flex  C  10 
flex  C  20 
post  U  40 
post  U  30 
post  U  60 
post  C  80 
post  C  100 
post  L  50 
post  L  40 

よう

何かがありがとうございます! ペドロ

+1

私たちにいくつかのサンプルデータを入力してください得ることを我々一緒に働くことができます。今、私たちはあなたの問題を再現するのが難しい「MIOT1」が何であるか分かりません。 'dput(MIOT1)'が役に立ちます。詳細については、次の記事を参照してください。http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

+0

質問のサンプルデータを追加しました。 ありがとうございました! –

答えて

2

ここでは、内蔵のmtcarsデータフレームを使用して、それを行う方法の簡単な例です:あなたのケースでは

ggplot(mtcars, aes(factor(cyl), mpg)) + 
    geom_boxplot() + 
    geom_text(stat="count", aes(label=..count..), y=min(mtcars$mpg)- 0.6) 

、それは

ggplot(data=MIOT1, aes(stage, time, fill=resp)) + 
    geom_boxplot(color= "black", lwd=0.3) + 
    geom_text(stat="count", aes(label=..count..), y=min(MIOT1$time)) 

あなたのようなものになりますテキストラベルの位置をyに調整する必要があります。また、ラベルのためのスペースを確保するためにy軸の範囲を調整する必要があります。

更新:報告したエラーは再現できましたが、修正方法がわかりません。代わりに、データをあらかじめ集計してからプロットに追加することができます。 SUMMARY EIPI10、IN

library(dplyr) 

# Get counts by desired grouping variables 
counts = mtcars %>% group_by(cyl, am) %>% tally 


ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + 
    geom_boxplot(position=position_dodge(0.9)) + 
    geom_text(data=counts, aes(label=n, y=min(mtcars$mpg) - 0.6), 
     position=position_dodge(0.9)) 
+0

返信eipi10ありがとうございます。 私はほとんどそこにいます。 しかし、あなたが提供したコードは、「開発段階」(x軸)の各グループの数を得ることができます。これは私が3つのカウントを持っていることを意味します。しかし、私は各ボックスプロットのカウントを9カウントとしたい。 もう一度ありがとうございます。 –

+0

eipi10 ... 9カウントがすべてそこにあることに気がつきましたが、それらは3のブロックで中央に重ねられます。 私はposition.dodgeを試しましたが、動作しません。 提案がありますか? –

+0

'position = position.dodge(0.5)'のようなものです(ボックスプロットと同じくらし量を使います)。 – eipi10

0

は私の質問に答え:ここでは例です

ライブラリ(dplyr)

希望のグループ化変数によって数が

counts = mtcars %>% group_by(cyl, am) %>% tally 


ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + 
geom_boxplot(position=position_dodge(0.9)) + 
geom_text(data=counts, aes(label=n, y=min(mtcars$mpg) - 0.6), position=position_dodge(0.9) 
関連する問題