title: Adding trend lines across groups and setting tick labels in a grouped violin plot or box plot
body: <p>I have <code>xy</code> grouped data that I'm plotting using <code>R</code>'s <code>ggplot2</code> <code>geom_violin</code> adding regression trend lines:</p>
  <p>Here are the data:</p>
  <pre><code>library(dplyr)
  library(plotly)
  library(ggplot2)
  
  set.seed(1)
  df &lt;- data.frame(value = c(rnorm(500,8,1),rnorm(600,6,1.5),rnorm(400,4,0.5),rnorm(500,2,2),rnorm(400,4,1),rnorm(600,7,0.5),rnorm(500,3,1),rnorm(500,3,1),rnorm(500,3,1)),
                   age = c(rep(&quot;d3&quot;,500),rep(&quot;d8&quot;,600),rep(&quot;d24&quot;,400),rep(&quot;d3&quot;,500),rep(&quot;d8&quot;,400),rep(&quot;d24&quot;,600),rep(&quot;d3&quot;,500),rep(&quot;d8&quot;,500),rep(&quot;d24&quot;,500)),
                   group = c(rep(&quot;A&quot;,1500),rep(&quot;B&quot;,1500),rep(&quot;C&quot;,1500))) %&gt;%
    dplyr::mutate(time = as.integer(age)) %&gt;%
    dplyr::arrange(group,time) %&gt;%
    dplyr::mutate(group_age=paste0(group,&quot;_&quot;,age))
  
  df$group_age &lt;- factor(df$group_age,levels=unique(df$group_age))
  </code></pre>
  <p>And my current plot:</p>
  <pre><code>ggplot(df,aes(x=group_age,y=value,fill=age,color=age,alpha=0.5)) + 
    geom_violin() + geom_boxplot(width=0.1,aes(fill=age,color=age,middle=mean(value))) + 
    geom_smooth(data=df,mapping=aes(x=group_age,y=value,group=group),color=&quot;black&quot;,method='lm',size=1,se=T) + theme_minimal()
  </code></pre>
  <p><a href="https://i.stack.imgur.com/LrTt7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LrTt7.png" alt="enter image description here" /></a></p>
  <p>My questions are:</p>
  <ol>
  <li>How do I get rid of the <code>alpha</code> part of the <code>legend</code>?</li>
  <li>I would like the <code>x-axis</code> <code>ticks</code> to be <code>df$group</code> rather than <code>df$group_age</code>, which means a <code>tick</code> per each <code>group</code> at the center of that <code>group</code> where the label is <code>group</code>. Consider a situation where not all <code>group</code>s have all <code>age</code>s - for example, if a certain <code>group</code> has only two of the <code>age</code>s and I'm pretty sure <code>ggplot</code> will only present only these two <code>age</code>s, I'd like the <code>tick</code> to still be centered between their two <code>age</code>s.</li>
  </ol>
  <p>One more question:</p>
  <p>It would also be nice to have the p-values of each fitted slope plotted on top of each <code>group</code>.</p>
  <p>I tried:</p>
  <pre><code>library(ggpmisc)
  my.formula &lt;- value ~ group_age
  ggplot(df,aes(x=group_age,y=value,fill=age,color=age,alpha=0.5)) + 
    geom_violin() + geom_boxplot(width=0.1,aes(fill=age,color=age,middle=mean(value))) + 
    geom_smooth(data=df,mapping=aes(x=group_age,y=value,group=group),color=&quot;black&quot;,method='lm',size=1,se=T) + theme_minimal() +
    stat_poly_eq(formula = my.formula,aes(label=stat(p.value.label)),parse=T)
  </code></pre>
  <p>But I get the same plot as above with the following <code>warning</code> message:</p>
  <pre><code>Warning message:
  Computation failed in `stat_poly_eq()`:
  argument &quot;x&quot; is missing, with no default 
  </code></pre>