2

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

user12582271
  • 91
  • 11

1 Answers1

4

I think the problem is your use of both stat_summary and geom_bar. Geom_bar and geom_text will not use the manipulated data (i.e. y means) from your stat_summary argument, which results in incorrect values plotted. Is this a bit closer to what you were looking for? (you can adjust the position of the bar labels with vjust).

library(ggplot2)
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)) + 
  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) +
  stat_summary(aes(label = label), fun.y = mean, geom = "text", size = 3,
               vjust = -1)

bar plot screenshot

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Conner Sexton
  • 321
  • 1
  • 6
  • Thank you so much. But the problem here is that my data is showing in 4 columns. It should be only sample1 and sample2, Also is there any way you can either use n=12 or n=15 above error bar or below sample1 and sample2 (i.e., not both at the same time). – user12582271 Jan 12 '20 at 17:16
  • No problem! :) To better understand your question, do the values "n = 12" and "n = 15" correspond to sample1 and sample2 respectively? (i.e. sample 1: "n = 12", sample 2: "n = 15"). If so, replacing the line where you create the label variable with this code: ```survey$label = ifelse(survey$sample == "sample1", "n = 12", "n = 15")``` should solve this. As for the labels you could use the sample variable to label your x-axis, if I'm understanding the question correctly. Also apologies for the formatting, I'm new to this! – Conner Sexton Jan 12 '20 at 17:33
  • Thank you. Yes, the values n = 12" and "n = 15" correspond to sample1 and sample2 respectively. I recreated the plot using ifelse. It worked. Also you suggestion regarding the n=12 and n=15 to appear either above error bar or below the sample1 and sample2 (according to the preference and when I want I want add where ever I want), you suggested I could use sample variable to label my x-axis. Could you please let me know how I do this modification? – user12582271 Jan 12 '20 at 17:42
  • Yes, so you would just replace your x aesthetic in the first line of your ggplot code like so: ```ggplot(survey, aes(x=sample, y = values, fill=sample))``` – Conner Sexton Jan 12 '20 at 17:48
  • Thank you. I changed x= sample, it worked for n=12 and n=15 above the error bar. But when I changed vjust = -1 to x = 0, it is showing some error. This is where I am trying to keep n=12 and n= 15 below the sample1 and sample2. Here is the code: ggplot(survey, aes(x=sample, y = values, fill=sample)) + 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) + stat_summary(aes(label = label), fun.y = mean, geom = "text", size = 3, x = 0) – user12582271 Jan 12 '20 at 17:53
  • No prob! Where exactly are you trying to position these labels? the last stat_summary argument will place the labels above the bars, with the vjust option taking values between 0 and 1 (only minute adjustments above bars). – Conner Sexton Jan 12 '20 at 18:00
  • These are just below the sample1 and sample 2 legends of x-axis – user12582271 Jan 12 '20 at 18:02