title: How can I connect multiple points by group with geom_segment() in ggplot2?
body: <p>Im trying to connect my points with a line, like this, but for my plot:</p>
  <p><a href="https://i.stack.imgur.com/2UTki.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2UTki.png" alt="enter image description here" /></a></p>
  <p>My plot looks like this:</p>
  <p><a href="https://i.stack.imgur.com/N0rQj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N0rQj.png" alt="enter image description here" /></a></p>
  <p>I've tried to use geom_segment but it hasn't worked. My base code is:</p>
  <pre><code>ggplot(tidydf, aes(Genome_size, `Trio_number`, color = Group)) +
       geom_point() + scale_y_continuous(breaks = seq(0, 20, by = 1)) +
       ylab(&quot;Trio number&quot;) + xlab(&quot;Genome size (kb)&quot;) + theme_dotplot + scale_x_continuous(labels = comma) + theme(legend.position = &quot;bottom&quot;) + scale_color_brewer(palette = &quot;Set1&quot;) + theme(legend.title=element_blank()) + guides(colour = guide_legend(override.aes = list(size=4))) theme(legend.key=element_rect(fill='gray96')) +
       theme(plot.background = element_rect(fill = 'gray96')) + theme(legend.title = element_text(size=10)) +  theme(text=element_text(size=12,  family=&quot;Gujarati Sangam MN&quot;)) + theme(axis.title.x = element_text(vjust = 0, size = 11), axis.title.y = element_text(vjust = 2, size = 11)) + theme(axis.text = element_text(color = &quot;black&quot;, size = 9))
  </code></pre>
  <p>How would I use geom_segment to do this for my data? Thanks for your help!</p>
  <p>UPDATE: reproducible code</p>
  <pre><code>library(ggplot2)
  library(scales)
  
  set.seed(8675309)
  
  tidydf &lt;- data.frame(
    Genome_size = sample(1000:7000, 30, replace = T),
    Trio_number = sample(1:20, 30, replace = T),
    Group = sample(c('Free-living', 'Gut', 'Pathogen'), 30, replace = T)
  )
  
  p &lt;- 
  ggplot(tidydf, aes(Genome_size, `Trio_number`, color = Group)) +
    geom_point() +
    scale_y_continuous(breaks = seq(0, 20, by = 1)) +
    ylab(&quot;Trio number&quot;) + xlab(&quot;Genome size (kb)&quot;) +
    theme_light() +
    scale_x_continuous(labels = comma) +
    scale_color_brewer(palette = &quot;Accent&quot;) +
    guides(colour = guide_legend(override.aes = list(size=4))) +
    theme(
      legend.position = &quot;bottom&quot;,
      legend.key=element_rect(fill='gray96'),
      plot.background = element_rect(fill = 'gray96'),
      legend.title =element_text(size=10),
      text=element_text(size=12),
      axis.title.x = element_text(vjust = 0, size = 11),
      axis.title.y = element_text(vjust = 2, size = 11),
      axis.text = element_text(color = &quot;black&quot;, size = 9),
      
      # to make the theme look more similar to OP example
      panel.grid.major.x = element_blank(),
      panel.grid.minor.x = element_blank()
    )
  p
  </code></pre>
  <p>UPDATE: my data</p>
  <pre><code>structure(list(Trio_number = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 
  4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 
  11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 
  16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20), `Accession ID` = c(&quot;NC_007530.2&quot;, 
  &quot;NC_000964.3&quot;, &quot;NZ_CP053376.1&quot;, &quot;NZ_CP061527.1&quot;, &quot;NZ_CP009756.1&quot;, 
  &quot;NZ_CP050508.1&quot;, &quot;NZ_CP010820.1&quot;, &quot;NC_021181.2&quot;, &quot;NZ_CP009909.1&quot;, 
  &quot;NZ_LS483427.1&quot;, &quot;NZ_CP026999.1&quot;, &quot;NC_014151.1&quot;, &quot;NZ_CP019870.1&quot;, 
  &quot;NZ_CP030775.1&quot;, &quot;NC_015687.1&quot;, &quot;NZ_CP024727.1&quot;, &quot;NZ_CP040530.1&quot;, 
  &quot;NZ_CP032489.1&quot;, &quot;NZ_CP022389.1&quot;, &quot;NZ_CP054012.1&quot;, &quot;NC_014734.1&quot;, 
  &quot;NZ_AP014857.1&quot;, &quot;NZ_CP060111.1&quot;, &quot;NC_008576.1&quot;, &quot;NC_003103.1&quot;, 
  &quot;NC_016845.1&quot;, &quot;NZ_CP026328.2&quot;, &quot;NC_003197.2&quot;, &quot;NZ_CP033744.1&quot;, 
  &quot;NC_014414.1&quot;, &quot;NC_004557.1&quot;, &quot;NZ_CP030777.1&quot;, &quot;NC_014624.2&quot;, 
  &quot;NC_000962.3&quot;, &quot;NZ_CP028341.1&quot;, &quot;NC_013124.1&quot;, &quot;NZ_CP029543.1&quot;, 
  &quot;NZ_CP006712.1&quot;, &quot;NC_013739.1&quot;, &quot;NZ_AP022576.1&quot;, &quot;NZ_AP012326.1&quot;, 
  &quot;NC_009664.2&quot;, &quot;NC_008593.1&quot;, &quot;NZ_LR699011.1&quot;, &quot;NZ_LR134338.1&quot;, 
  &quot;NZ_CP028842.1&quot;, &quot;NZ_CP036345.1&quot;, &quot;NC_016584.1&quot;, &quot;NC_011725.1&quot;, 
  &quot;NZ_CP012938.1&quot;, &quot;NZ_CP023665.1&quot;, &quot;NZ_LS483376.1&quot;, &quot;NZ_LN877293.1&quot;, 
  &quot;NZ_LT605205.1&quot;, &quot;NZ_CP014150.1&quot;, &quot;NZ_CP039729.1&quot;, &quot;NC_016052.1&quot;, 
  &quot;NZ_CP008747.1&quot;, &quot;NZ_CP023011.2&quot;, &quot;NC_018631.1&quot;), Group = c(&quot;Pathogen&quot;, 
  &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, 
  &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, 
  &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, 
  &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, 
  &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, 
  &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, 
  &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, 
  &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, 
  &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, 
  &quot;Gut&quot;, &quot;Free-living&quot;, &quot;Pathogen&quot;, &quot;Gut&quot;, &quot;Free-living&quot;), Genome_size = c(5227.419, 
  4215.606, 4026.648, 4393.662, 4848.754, 5598.454, 4843.789, 1991.579, 
  5762.608, 1988.85, 2365.873, 4123.179, 4124.384, 3810.128, 3942.462, 
  2011.558, 6271.157, 4099.663, 2455.405, 4926.033, 3685.504, 4657.167, 
  6149.586, 4719.581, 1268.755, 5333.942, 2726.366, 4857.45, 4974.986, 
  2902.643, 2799.251, 2970.937, 4316.707, 4411.532, 2192.428, 2158.157, 
  3187.112, 2288.919, 6359.369, 6219.859, 2635.669, 4761.183, 2547.72, 
  3592.125, 6726.998, 3858.511, 3590.716, 5863.081, 5419.036, 6472.489, 
  4376.831, 3874.791, 5188.967, 4414.963, 3550.458, 2736.723, 2562.72, 
  2472.129, 2845.651, 1893.499)), row.names = c(NA, -60L), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>
  <p>I'll try geom_line! Thanks</p>