title: Grouped bar plots with SEM errorbars using base graphics in R?
body: <p>Edit: Based on comments below I decided to be more explicit. So here is an example of the data I am working on.</p>
  
  <pre><code>&gt; example_data
     A     B        outcome
  1  2.31  1.47       Y
  2  2.14  1.32       N
  3  3.49  1.00       Y
  4  2.12  0.62       Y
  5  0.47  0.55       N
  6  3.36  0.50       N
  7  3.50  0.33       Y
  8  1.97  0.39       Y
  9  3.12  0.99       N
  10 2.04  0.89       Y
  11 2.78  0.36       Y
  12 1.83  0.70       N
  13 3.53  0.77       N
  14 2.25  0.39       N
  15 1.67  0.43       N
  16 3.09  1.10       Y
  </code></pre>
  
  <p>So, I have two variables <code>A</code> and <code>B</code>. They are subgroups of a larger variable, so they can be represented in the same y axis. I want to group them by another variable <code>outcome</code> which has two levels.</p>
  
  <p>I do the following   </p>
  
  <pre><code>&gt; dataset &lt;- example_data
  &gt; attach(dataset)
  &gt; means1&lt;- tapply(A,outcome,function(x) mean(x,na.rm=TRUE))
  &gt; means2&lt;- tapply(B,outcome,function(x) mean(x,na.rm=TRUE))
  &gt; std.err1&lt;- tapply(A,outcome,function(x)sd(x,na.rm=TRUE)/sqrt(length(na.omit(x))))
  &gt; std.err2&lt;- tapply(B,outcome,function(x)sd(x,na.rm=TRUE)/sqrt(length(na.omit(x))))
  &gt; matrix_means&lt;- matrix(c(means1,means2),byrow = TRUE)
  &gt; graph&lt;- barplot(matrix_means, beside=TRUE, space=c(0.2,0,0.2,0), axes=FALSE, ann=FALSE, col=c("white","black","red","blue"),ylim=c(0,4), names=c("Outcome-N","Outcome-Y","Outcome-N","Outcome-Y"), xpd=FALSE)
  &gt; axis(2, cex.axis=1.5)
  </code></pre>
  
  <p>Now I also need to use the arrows function to draw the SEM bar between the mean and <code>mean+sem</code> of each group. </p>
  
  <p>I can get the SEMs using <code>tapply</code>, but do not know how to place the arrows over each of the 4 bars. </p>
  
  <pre><code>&gt; arrows(graph, means1, graph, means1 + std.err1, graph, means2, graph, means2 + std.err2, code=3, angle=90, length=0.1)
  </code></pre>
  
  <p>This does not place any arrows on the plot. </p>
  
  <p><strong>Using <code>base</code> graphics and not <code>ggplot2</code></strong>, how do I do this? Can someone help? All my other graphs were done using the <code>GrapheR</code> package in <code>R</code>, so I am trying to do this one too using base graphics so they will all look the same for my manuscript. </p>
  
  <p>Thanks.</p>