将R值和置信区间添加到R

时间:2017-03-08 15:24:58

标签: r ggplot2 statistics bar-chart confidence-interval

我是R的一个非常基本的用户,所以我事先为这个问题的简单性道歉,或者如果缺乏这个提法,我会道歉。

我有一个大型数据集,其中我有一个连续数值变量和两个因子,每个都有2个级别。

这是(或多或少)基于生成/人工数据重建我的数据:

 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

我现在要做的是表明伪词之间的频率差异不显着,但在单词之间有显着差异。为此,有重要性统计数据(在我的情况下是t值)和置信区间是很好的。我知道如何计算这些,但我不知道如何将它们添加到条形图中。

我在互联网上看了很多,但我找不到一个导致我想看到的例子。

非常感谢所有的帮助。

1 个答案:

答案 0 :(得分: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