0

I have the following code, which plots 8 means with equal spacing between the bars. I need to get the bars to group together in pairs. I read a lot of answers to similar questions, but I am having trouble figuring out how my code is different-- why isn't it grouping the bars together in pairs??

x = as.numeric(c(filt_forr[1,1:4],filt_forr[1,5:8]))

opar <- par(lwd = 0.3)

plot = barplot(x,axes=FALSE, axisnames=FALSE,ylim=c(0,6),main="Radial Sway of Low and High Frequencies",xlab="Condition",beside=T,ylab="Radial Sway (mm)", cex.names=0.8, las=2, width = 2, col=c("#79c36a","deepskyblue2"),legend = c("Low Frequencies (<0.3 Hz)","Blue = High Frequencies (>0.3 Hz)"))

axis(1,labels=c("Closed/Silence","Closed/Silence","Closed/Noise", "Closed/Noise", "Open/Silence", "Open/Silence","Open/Noise","Open/Noise"),at=plot)

axis(2,at=seq(0,6, by=0.5))

sd = as.numeric(c(filt_forr[2,1:4],filt_forr[2,5:8]))

segments(plot,x-sd,plot,x+sd,lwd=2)

segments(plot-0.1,x-sd,plot+0.1,x-sd,lwd=2)

segments(plot-0.1,x+sd,plot+0.1,x+sd,lwd=2)

abline(c(0,0))

abline(v=0)

Here is a sample of what my data look like (row one has condition means and row 2 has standard deviations, which I use for the error bars):

Cond1Low Cond2Low Cond3Low Cond4Low Cond1High Cond2High Cond3High Cond4High

3.503900 2.714572 2.688209 2.739038 2.499589 2.159015 2.1308462 1.9066430

2.073565 1.680001 1.490732 1.259822 1.139851 0.732513 0.8940674 0.6708717

  • To make it easier to help you, your answer should be [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (include sample data so we can actually run it to see what you see). Also, if you are following the advice of other answers, why not include those so we can see how they are different. – MrFlick Oct 22 '14 at 17:59

1 Answers1

1

To get side-by-side barplots you generally pass in a matrix of values rather than a vector as you';ve done in your example. Just change everything to the appropriate matrix

X <- matrix(x, ncol=2)
plot <- barplot(t(X), beside=T, axes=FALSE, axisnames=FALSE, ylim=c(0,6), 
    main="Radial Sway of Low and High Frequencies",
    xlab="Condition", ylab="Radial Sway (mm)", 
    cex.names=0.8, las=2, width = 2, col=c("#79c36a","deepskyblue2"),
    legend = c("Low Frequencies (<0.3 Hz)","Blue = High Frequencies (>0.3 Hz)"))
axis(1,labels=c("Closed/Silence","Closed/Noise", 
    "Open/Silence","Open/Noise"),at=apply(plot,2,mean))
axis(2,at=seq(0,6, by=0.5))

SD <- matrix(as.numeric(c(filt_forr[2,1:4],filt_forr[2,5:8])), ncol=2)

segments(plot,t(X-SD),plot,t(X+SD),lwd=2)
segments(plot-0.1,t(X-SD),plot+0.1,t(X-SD),lwd=2)
segments(plot-0.1,t(X+SD),plot+0.1,t(X+SD),lwd=2)

abline(c(0,0))
abline(v=0)

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Oh I understand the problem!! Thank you very much! One follow-up question: why aren't the error bars in the plot centered vertically? See how the middle of the error bars aren't at the top of the data bars? – user3183306 Oct 23 '14 at 03:37
  • @user3183306 I guess we did need to change the standard error code too. I've updated the answer to draw those as well. – MrFlick Oct 23 '14 at 03:45