2017-05-14 1 views
-1

나는 데이터 막대의 그룹 차이를 나타내는 막대 막대로 막대 그래프를 만들려고 노력하고 있습니다. 그러나 오류 막대는 막대 위에 그리고 막대 가운데에 더 많이 나타나 펑키 나오고 있습니다.R (ggplot2)에서 오류 막대 만들기

내 코드

:

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

X = Hippo_6_9NAACre

: I는 다음과 같은 기능을 이용하여 표준 오차를 계산

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) 
관련 문제