Hello I would need help in order to sort geom\_segment in my plot by the column `end_scaffold`.

Here is the code I used to produce the following plot :

[![enter image description here](https://i.stack.imgur.com/kDeR2.png)](https://i.stack.imgur.com/kDeR2.png)

```
library(ggplot2)
#Here I try to sort the data in order to get geom_segment sorted in the plot but it does not work 
tab<-tab[with(tab, order(-end_scaff,-end_gene)), ] 

ggplot(tab, aes(x = start_scaff, xend = end_scaff, 
                y = molecule, yend = molecule)) +
  geom_segment(size = 3, col = "grey80") +
  geom_segment(aes(x = ifelse(direction == 1, start_gene, end_gene),
                   xend = ifelse(direction == 1, end_gene, start_gene)),
               data = tab, 
               arrow = arrow(length = unit(0.1, "inches")), size = 2) +
  geom_text(aes(x = start_gene, y = molecule, label = gene),
            data = tab, nudge_y = 0.2) + 
  scale_y_discrete(limits = rev(levels(tab$molecule))) +
  theme_minimal()

```

does someone have an idea in order to sort the `geom_segment` by the column `end_scaffold` (descending) (where `scaffold_1254` should be on the top of the plot and `scaffold_74038` shoudl be on the bottom).

here are the data

```
> dput(tab)
structure(list(molecule = structure(c(2L, 6L, 6L, 3L, 7L, 4L, 
5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("", "scaffold_1254", "scaffold_15158", "scaffold_7180", 
"scaffold_74038", "scaffold_7638", "scaffold_8315"), class = "factor"), 
    gene = structure(c(8L, 6L, 5L, 3L, 7L, 4L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
    "G1", "G2", "G3", "G4", "G5", "G6", "G7"), class = "factor"), 
    start_gene = c(6708L, 9567L, 3456L, 10105L, 2760L, 9814L, 
    1476L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA), end_gene = c(11967L, 10665L, 4479L, 10609L, 
    3849L, 10132L, 2010L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA), start_scaff = c(1L, 1L, 
    1L, 1L, 1L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA), end_scaff = c(20072, 15336, 
    15336, 13487, 10827, 10155, 2010, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), strand = structure(c(2L, 
    2L, 3L, 2L, 3L, 2L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "backward", 
    "forward"), class = "factor"), direction = c(-1L, -1L, 1L, 
    -1L, 1L, -1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA)), row.names = c(7L, 5L, 4L, 2L, 
6L, 3L, 1L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L), class = "data.frame")

```