title: use ggrepel to display mean value as a text label
body: <p>I am creating a plot where I want to display mean value. I have managed to display both the mean and its corresponding value, but I find the plots to be too cluttered with the mean values thrown in, so I wanted to use <code>ggrepel::geom_label_repel</code> to display means in the form of text labels that are a bit farther removed from the data points. I tried something that doesn't work and would appreciate if someone can help me figure out how I can get the desired result. Thanks.</p>
  
  
  
  <pre class="lang-r prettyprint-override"><code>library(ggplot2)
  #&gt; Warning: package 'ggplot2' was built under R version 3.4.3
  library(ggrepel)
  #&gt; Warning: package 'ggrepel' was built under R version 3.4.2
  
  
  # function to plot mean
  fun_mean &lt;- function(x) {
    return(data.frame(
      y = as.numeric(as.character(mean(x, na.rm = TRUE))),
      label = as.numeric(as.character(mean(x, na.rm = TRUE)))
    ))
  }
  
  # preparing the basic plot
  plot &lt;-
    ggplot2::ggplot(data = iris,
                    mapping = aes(x = Species, y = Sepal.Length)) +
    geom_point(
      position = position_jitterdodge(
        jitter.width = NULL,
        jitter.height = 0.2,
        dodge.width = 0.75
      ),
      alpha = 0.5,
      size = 3,
      aes(color = factor(Species))
    ) +
    geom_violin(width = 0.5,
                alpha = 0.2,
                fill = "white") +
    geom_boxplot(
      width = 0.3,
      alpha = 0.2,
      fill = "white",
      outlier.colour = "black",
      outlier.shape = 16,
      outlier.size = 3,
      outlier.alpha = 0.7,
      position = position_dodge(width = NULL)
    ) +
    theme(legend.position = "none")
  
  
  # add the mean label to the plot
  plot &lt;- plot +
    stat_summary(
      fun.y = mean,
      geom = "point",
      colour = "darkred",
      size = 5
    ) +
    stat_summary(
      fun.data = fun_mean,
      geom = "text",
      vjust = -1.0,
      size = 5
    )
  
  # see the plot
  plot
  </code></pre>
  
  <p><img src="https://i.imgur.com/U2fH4sb.png" alt=""></p>
  
  <pre class="lang-r prettyprint-override"><code># adding geom_label_repel
  plot &lt;-
    plot +
    ggrepel::geom_label_repel(
      mapping = aes(label = mean),
      fontface = 'bold',
      color = 'black',
      inherit.aes = FALSE,
      max.iter = 3e2,
      box.padding = 0.35,
      point.padding = 0.5,
      segment.color = 'grey50',
      force = 2
    )
  
  plot # doesn't work :(
  #&gt; Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
  #&gt; Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 0, 150
  </code></pre>
  
  <p><img src="https://i.imgur.com/oI08Wjk.png" alt=""></p>
  
  <p>Created on 2018-02-10 by the <a href="http://reprex.tidyverse.org" rel="nofollow noreferrer">reprex
  package</a> (v0.1.1.9000).</p>