简体   繁体   中英

Add t values and confidence intervals to barplot in R

I am a very basic user of R, so I apologize beforehand for the simplicity of the question, or if the formulation is lacking.

I have a large data set, where I have one continuous numerical variable and two factors with 2 levels each.

This is (more or less) a reconstruction of my data based on generated/artificial data:

 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)

I managed to plot my data using 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")) 

The plot looks like this:

使用模拟数据绘图

What I would like to do now is to show that the difference in frequency between pseudowords is not significant, but it is significant between words. For that it would be great to have significance statistics (t values in my case) and also confidence intervals. I know how to compute these, but I don't know how to add them to the barplot.

I have looked extensively on the Internet but I could not find an example that resulted in what I want to see.

All assistance is much appreciated.

I'm going to assume that you want means and confidence intervals.

Currently you are plotting sums, because you have a stacked barplot. We can see that when adding a border color:

在此处输入图片说明

We can use stat_summary() to calculate means, and bootstrap confidence intervals:

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))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM