title: Underscore plot in R
body: <p><strong>Introduction and Current Work Done</strong></p>
  
  <p>[<em>Note</em>: For those interested, I have provided code at the end for reproducing my example.]</p>
  
  <p>I have some data and I have conducted an ANOVA analysis and obtained Tukey's pairwise comparisons:</p>
  
  <pre><code>model1 = aov(trt ~ grp, data = df)
  anova(model1)
  
  &gt; TukeyHSD(model1)
            diff         lwr       upr     p adj
  B-A 0.03481504 -0.40533118 0.4749613 0.9968007
  C-A 0.36140489 -0.07874134 0.8015511 0.1448379
  D-A 1.53825179  1.09810556 1.9783980 0.0000000
  C-B 0.32658985 -0.11355638 0.7667361 0.2166301
  D-B 1.50343674  1.06329052 1.9435830 0.0000000
  D-C 1.17684690  0.73670067 1.6169931 0.0000000
  </code></pre>
  
  <p>I can also plot Tukey's pairwise comparisons </p>
  
  <pre><code>&gt; plot(TukeyHSD(model1))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/t6n9S.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t6n9S.png" alt="Tukey&#39;s pairwise comparisons for simulated data"></a></p>
  
  <p>We can see from Tukey's confidence intervals and the plot that <code>A-B</code>, <code>B-C</code> and <code>A-C</code> are not significantly different.</p>
  
  <p><strong>Problem</strong></p>
  
  <p>I have been asked to create something called an "underscore plot" which is described as follows:</p>
  
  <p>We plot the group means on the real line and we draw a line segment between group means to indicate that there is no significant difference between those two particular groups.</p>
  
  <p>Obtaining the means is not difficult:</p>
  
  <pre><code>&gt; aggregate(df$trt ~ df$grp, FUN = mean)
    df$grp   df$trt
  1      A 2.032086
  2      B 2.066901
  3      C 2.393491
  4      D 3.570338
  </code></pre>
  
  <p><strong>Desired Output</strong></p>
  
  <p>Using the data in this example, the desired plot should appear like the one below:</p>
  
  <p><a href="https://i.stack.imgur.com/gGr9b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gGr9b.png" alt="Example underscore plot"></a></p>
  
  <p>There is a line segment between the groups that are not significantly different (i.e. a line segment between <code>A-B</code>, <code>B-C</code> and <code>A-C</code> as indicated by Tukey's). </p>
  
  <p><strong>Note:</strong> Please note that the plot above is not to scale and it was created in keynote for illustrative purposes only.</p>
  
  <p>Is there a way to get the "underscore plot" described above using R (using either base R or a library such as <code>ggplot2</code>)? </p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here is the code that I used to create the example above:</p>
  
  <pre><code>library(data.table)
  
  set.seed(3)
  A = runif(20, 1,3)
  A = data.frame(A, rep("A", length(A)))
  B = runif(20, 1.25,3.25)
  B = data.frame(B, rep("B", length(B)))
  C = runif(20, 1.5,3.5)
  C = data.frame(C, rep("C", length(C)))
  D = runif(20, 2.75,4.25)
  D = data.frame(D, rep("D", length(D)))
  
  df = list(A, B, C, D)
  df = rbindlist(df)
  
  colnames(df) = c("trt", "grp")
  </code></pre>