title: Box plot error when adding mean
body: <p>I realize this question has been asked previously and it has been suggested to use ggplot, lattice etc. My question relates to adding the mean value onto boxplot according to a categorical variable.</p>
  
  <p>Here is my code and it does not work:</p>
  
  <p>STEP 1: I created a vector using tapply to get the mean value of bmi z-scores at age 4 years according to maternal obesity group:</p>
  
  <pre><code>means &lt;- tapply(therapy$zbmi_4,therapy$Gruppe,mean,na.rm=TRUE) # calculate means
  </code></pre>
  
  <p>The output looks like:</p>
  
  <pre><code>&gt;means
  Normal-weight         Obese 
  -0.03207792        0.60130081
  </code></pre>
  
  <p>STEP 2: I created a simple boxplot of bmi z-scores at age 4 years according to maternal obesity group:</p>
  
  <pre><code>plot(therapy$Gruppe,therapy$zbmi_4,
  xlab = 'Maternal BMI groups',
  ylab = 'Offspring BMI z-scores at 4 years',
  cex.lab=1.5, cex.axis=1.4, cex.main=1.6, cex.sub=1.5,
  col=c("white","grey"), main="Effect of maternal obesity",
  pch=16,cex=1)
  points(x=therapy$Gruppe,y=means,pch=19, col="red") # add means
  legend("topleft", legend=c("P-value &lt;0.0001"), bty = "n", cex=1.5)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/F3HKW.png" alt="The output looks like this (without points) &lt;code&gt;x=therapy$Gruppe,y=means,pch=19, col=&quot;red&quot;)&lt;/code&gt;"></p>
  
  <p>The error message is:</p>
  
  <pre><code>&gt;points(x=therapy$Gruppe,y=means,pch=19, col="red", type="l") # add means
  Error in xy.coords(x, y) : 'x' and 'y' lengths differ
  </code></pre>
  
  <p>I totally understand this issue. Since, there are 949 observations and only 2 means.</p>
  
  <pre><code>&gt; length(therapy$Gruppe)
  [1] 949
  &gt; length(means)
  [1] 2
  </code></pre>
  
  <p>Now, am I thinking too much or is there a very simple way of adding a "dot" in each of the box plot for mean bmi z-score according to maternal group of obesity (normal-weight versus obese). I would really appreciate any sort of help and suggestions.</p>
  
  <p>Thank you so much in advance</p>