2017-03-08 178 views
1

我是R的一個非常基本的用戶,所以我爲此問題的簡單性或者如果公式缺乏提前道歉。將t值和置信區間添加到R中的barplot中

我有一個大型的數據集,我有一個連續的數值變量和兩個因子,每個有兩個級別。

這是(或多或少)我的數據的基礎上產生的/人工的數據的重建:

wordhigh.mu <- -2 
    wordlow.mu <- -2.5 
    pswordhigh.mu <- -1.5 
    pswordlow.mu <- -1.5 
    sigma <- 0.3 
wordshigh <- rnorm(50,mean = wordhigh.mu,sd=sigma) 
wordslow <- rnorm(50,mean = wordlow.mu,sd=sigma) 
pswordshigh <- rnorm(50,mean = pswordhigh.mu,sd=sigma) 
pswordslow <- rnorm(50,mean = pswordlow.mu,sd=sigma) 
value <- c(wordshigh,wordslow,pswordshigh,pswordslow) 
LexicalitySample <- c(rep("Word",100),rep("Pseudoword",100)) 
FrequencySample <- c(rep("High",50),rep("Low",50),rep("High",50),rep("Low",50)) 
new.table <- data.frame(ErpMinAv=value,Lexicality=LexicalitySample,Frequency=FrequencySample) 

我設法使用ggplot繪製我的數據:

ExampleBarPlot <- ggplot(new.table,aes(Lexicality,ErpMinAv,fill=Frequency)) + geom_bar(stat="identity",position="dodge") + xlab("Lexicality") + ylab("Microvolts") + labs(title = "Frequency effect for singular nouns and pseudoword controls") + scale_y_continuous("Microvolts",breaks = round(seq(0, -20, by = -0.5),1)) + guides(fill=guide_legend(title="Frequency"))+ scale_colour_manual(values = c("blue","red")) 

情節看起來像這樣的:

Plot using simulated data

我想這樣做ñ ow是表明pseudowords之間的頻率差異不顯着,但在詞語之間是顯着的。爲此,有意義統計(我的情況下t值)以及置信區間會很好。我知道如何計算這些,但我不知道如何將它們添加到barplot中。

我已經在互聯網上廣泛地看過,但我找不到一個導致我想看到的例子。

非常感謝所有幫助。

回答

3

我打算假設你想要的手段和置信區間。

目前您正在繪製總和,因爲您擁有堆疊的條形圖。我們可以看到,添加邊框顏色時:

enter image description here

我們可以使用stat_summary()計算手段,引導置信區間:

ggplot(new.table, aes(Lexicality,ErpMinAv,fill=Frequency)) + 
    stat_summary(geom = 'bar', fun.y = mean, position = position_dodge(0.9)) + 
    stat_summary(geom = 'errorbar', fun.data = mean_cl_boot, position = position_dodge(0.9), 
       width = 0.5) + 
    scale_y_continuous("Microvolts",breaks = round(seq(0, -20, by = -0.5),1)) 

enter image description here

+0

感謝您的答覆,並向我介紹stat_summary。很有用! – HernanLG