2017-05-14 25 views
-1

私は、私が持っているデータセットのグループの違いを表すためにエラーバーを持つ棒グラフを作成しています。しかし、エラーバーは、バーの上とバーの真ん中に表示されているので、ファンキーになっています。R(ggplot2)でエラーバーを作成する

マイコード

std <- function(x) sd(x)/sqrt(length(x)) 

X = Hippo_6_9NAACre

:私は次の関数を使用して標準誤差を計算

plot

:これは、このグラフを生成

ggplot(MRS_Hippo_NAA_Cre_Data_copy, aes(Type, Hippo_6_9NAACre, fill=Type)) + 
geom_bar(stat="summary", fun.y="mean", colour="black", size=.3) + 
geom_errorbar(aes(ymin=meanNAA-NAAse, ymax=meanNAA+NAAse), width=.2, 
position=position_dodge(.9)) + labs(x="Group", y="Right Posterior NAA/Cre") + 
scale_fill_manual(values=c("#0072B2", "#D55E00"), name="Group") + theme(text = 
element_text(size=18))` 

グラフがファンキーなエラーバーを生成している理由がわかりません。誰でも助けたり、洞察力を提供できますか?

+0

「meanNAA」と「NAAse」は、両方のグループを組み合わせたものです。もちろん、再現可能な例がないので、私は推測しているだけです。 – Axeman

+0

はい、そうです。私はグループのためにそれらを分けるべきですか? – erikapnt

答えて

1

私はまったく同じような問題を抱えていました。 は、最初にすべてのレイヤーに

geom_errorbar(aes(ymin=meanNAA-NAAse, 
ymax=meanNAA+NAAse), width=.2, position=position_dodge(.9)) 

を削除し、むしろ再びstatsummary機能を有する層を使用する場合があり、それを解決するには。これは、グループごとに区切られたエラーバーを生成します。 標準エラーを示すバーを表示するには、statsummaryから使用できるように、必要な値を返す適切な関数を作成する必要があります。 以下は、irisデータセットを使用した作業例です。

library(ggplot2) 

## create a function for standard error that can be used with stat_summary 
# I created the function inspecting the results returned by 'mean_cl_normal' that is the   
# function used in some examples of stat_summary (see ?stat_summary). 

mean_se = function(x){ 
se = function(x){sd(x)/sqrt(length(x))} 
data.frame(y=mean(x), ymin=mean(x)+se(x), ymax=mean(x)-se(x)) 
} 

## create the plot 
p = ggplot(iris, aes(x = Species, y = Sepal.Length), stat="identity") + 
stat_summary(fun.y = mean, geom = "col", fill = "White", colour = "Black", width=0.5) + 
stat_summary(fun.data = mean_se, geom = "errorbar", width=0.2, size=1) 

# print the plot 
print(p) 
関連する問題