Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to add error bar and n=sample size manually in ggplot2?

$
0
0

I am trying to plot a bar graph, but I have some problem with this code, which I am not sure what is the problem in my code. 1) I want a bar graph with mean_se, and also n = 12 and n = 15 below the sample1 and sample2. 2) In another case I want n = 12 and n= 15 above the error bar in each sample or in the middle of the bar graph. I am getting some wierd output. Here is the code

survey <- data.frame(sample=rep(c("sample1","sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2"),1), 
                 values=c(200, 100, 150, 175, 145, 50, 75, 60, 45, 56, 300, 200, 150, 100, 125, 25, 50, 75, 45, 35)) 
survey

survey$label = c("n=12", "n = 15") 
survey <- transform(survey, labelx = paste(sample, "\n", label)) 

ggplot(survey, aes(x=labelx, y = values, fill=sample)) + 
    geom_bar(stat="identity", position=position_dodge(width = 0.25),width=0.25)+ 
    stat_summary(geom = "bar", fun.y = mean, position = "dodge", width= 0.25) +
    stat_summary(geom = "errorbar", fun.data = mean_se, position = position_dodge2(0.9), width=0.1)+
    geom_text(aes(label = label), vjust = -1) 

enter image description here


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>