title: Datapoints on top of complex barplots in R-software
body: <p>I have this fake data frame. I am looking at a quicker vectorization to add data points over the barplot of means. My solution would be hard to apply when many columns are present. My problem is that only a vector and not a matrix is allowed in the "points" functions. Do you have a smart solution?</p>
  
  <pre><code>df &lt;- data.frame(Test = 1:5,
      Factor= c("A","A","B","B","A"),
      V1=c(3.2,5.4,6.0,6.5,2),
      V2=c(5,5,8.6,7,1))
  
  str(df, list.len=ncol(df))
  colnames(df)
  dim(df)
  
  df.agg &lt;- aggregate(df[c(3,4)], by = list(Factor = df$Factor), mean)
  df.agg &lt;- df.agg[order(df.agg$Factor),]
  df.agg
  
  mat.agg &lt;- as.matrix(df.agg[c(2,3)])
  
  barx &lt;- barplot(mat.agg, 
          beside = T,
          ylim = c(0, 1.3*max(mat.agg)),
          col = colors()[c(5,16)][df.agg$Factor],
          legend.text = as.character(df.agg$Factor))
  barx
  barx &lt;- as.vector(barx)
  barx
  
  points(
    rep(barx[1], length(df[df$Factor == levels(df$Factor)[1], "V1"])),
    df[df$Factor == levels(df$Factor)[1], "V1"])
  
  points(
    rep(barx[2], length(df[df$Factor == levels(df$Factor)[2], "V1"])),
    df[df$Factor == levels(df$Factor)[2], "V1"])
  
  points(
    rep(barx[3], length(df[df$Factor == levels(df$Factor)[1], "V2"])),
    df[df$Factor == levels(df$Factor)[1], "V2"])
  
  points(
    rep(barx[4], length(df[df$Factor == levels(df$Factor)[2], "V2"])),
    df[df$Factor == levels(df$Factor)[2], "V2"])
  </code></pre>