title: R: Sort points in ggplot
body: <p>I have the following dataframe and I want to generate a scatter plot with points ordered by date.</p>
  <pre><code> structure(list(cluster = c(&quot;uid_1&quot;, &quot;uid_2&quot;, &quot;uid_3&quot;, &quot;uid_4&quot;, 
      &quot;uid_5&quot;, &quot;uid_6&quot;, &quot;Cluster_07&quot;, &quot;Cluster_07&quot;, &quot;Cluster_07&quot;, &quot;Cluster_07&quot;, 
      &quot;Cluster_07&quot;, &quot;uid_12&quot;, &quot;uid_13&quot;, &quot;Cluster_07&quot;, &quot;Cluster_07&quot;, 
      &quot;uid_16&quot;, &quot;Cluster_07&quot;, &quot;Cluster_07&quot;, &quot;uid_19&quot;, &quot;uid_20&quot;, &quot;uid_21&quot;, 
      &quot;uid_22&quot;, &quot;uid_23&quot;, &quot;uid_24&quot;, &quot;uid_25&quot;, &quot;uid_26&quot;, &quot;uid_27&quot;, &quot;uid_28&quot;, 
      &quot;uid_29&quot;, &quot;Cluster_50&quot;, &quot;uid_31&quot;, &quot;uid_32&quot;, &quot;uid_33&quot;, &quot;uid_34&quot;, 
      &quot;uid_35&quot;, &quot;uid_36&quot;, &quot;Cluster_50&quot;, &quot;uid_38&quot;, &quot;uid_39&quot;, &quot;uid_40&quot;, 
      &quot;Cluster_50&quot;), name = c(&quot;E569&quot;, &quot;E847&quot;, &quot;E848&quot;, &quot;E882&quot;, &quot;E398&quot;, 
      &quot;E443&quot;, &quot;E462&quot;, &quot;E970&quot;, &quot;E1078&quot;, &quot;E831&quot;, &quot;E866&quot;, &quot;E1047&quot;, &quot;E964&quot;, 
      &quot;E507&quot;, &quot;E774&quot;, &quot;E1106&quot;, &quot;E943&quot;, &quot;E1069&quot;, &quot;E867&quot;, &quot;E868&quot;, &quot;E44&quot;, 
      &quot;E60&quot;, &quot;E869&quot;, &quot;E482&quot;, &quot;E125&quot;, &quot;E126&quot;, &quot;E114&quot;, &quot;E123&quot;, &quot;E81&quot;, 
      &quot;E504&quot;, &quot;E178&quot;, &quot;E179&quot;, &quot;E180&quot;, &quot;E181&quot;, &quot;E299&quot;, &quot;E793&quot;, &quot;E219&quot;, 
      &quot;E182&quot;, &quot;E183&quot;, &quot;E184&quot;, &quot;E650&quot;), date = structure(c(18281, 18396, 
      18396, 18414, 18441, 18526, 18586, 18267, 18300, 18317, 18418, 
      18437, 18461, 18469, 18470, 18476, 18497, 18501, 18564, 18568, 
      18341, 18353, 18383, 18401, 18411, 18411, 18423, 18435, 18495, 
      18498, 18542, 18552, 18559, 18559, 18598, 18604, 18617, 18277, 
      18277, 18280, 18340), class = &quot;Date&quot;), group_id = c(3L, 8L, 19L, 
      28L, 30L, 31L, 1L, 1L, 1L, 1L, 1L, 4L, 5L, 1L, 1L, 6L, 1L, 1L, 
      7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 2L, 20L, 
      21L, 22L, 23L, 24L, 25L, 2L, 26L, 27L, 29L, 2L)), row.names = c(NA, 
      -41L), groups = structure(list(cluster = c(&quot;Cluster_07&quot;, &quot;Cluster_50&quot;, 
      &quot;uid_1&quot;, &quot;uid_12&quot;, &quot;uid_13&quot;, &quot;uid_16&quot;, &quot;uid_19&quot;, &quot;uid_2&quot;, &quot;uid_20&quot;, 
      &quot;uid_21&quot;, &quot;uid_22&quot;, &quot;uid_23&quot;, &quot;uid_24&quot;, &quot;uid_25&quot;, &quot;uid_26&quot;, &quot;uid_27&quot;, 
      &quot;uid_28&quot;, &quot;uid_29&quot;, &quot;uid_3&quot;, &quot;uid_31&quot;, &quot;uid_32&quot;, &quot;uid_33&quot;, &quot;uid_34&quot;, 
      &quot;uid_35&quot;, &quot;uid_36&quot;, &quot;uid_38&quot;, &quot;uid_39&quot;, &quot;uid_4&quot;, &quot;uid_40&quot;, &quot;uid_5&quot;, 
      &quot;uid_6&quot;), .rows = structure(list(c(7L, 8L, 9L, 10L, 11L, 14L, 
      15L, 17L, 18L), c(30L, 37L, 41L), 1L, 12L, 13L, 16L, 19L, 2L, 
          20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 3L, 31L, 
          32L, 33L, 34L, 35L, 36L, 38L, 39L, 4L, 40L, 5L, 6L), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;, 
      &quot;vctrs_vctr&quot;, &quot;list&quot;))), row.names = c(NA, -31L), class = c(&quot;tbl_df&quot;, 
      &quot;tbl&quot;, &quot;data.frame&quot;), .drop = TRUE), class = c(&quot;grouped_df&quot;, 
      &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>
  <p>I could generate the plot using the following code.</p>
  <pre><code> ggplot(subset_so, aes(reorder(date,factor(cluster)), reorder(factor(cluster),date))) +
    geom_line(aes(group=cluster), color='black') +
    geom_point(size=3) +
    ylab(&quot;cluster&quot;) + xlab(&quot;date&quot;) +
    theme_light() +
    theme(
      legend.position = &quot;bottom&quot;,
      legend.key=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.x = element_text(angle = 90, hjust = 1, size = 9),
      panel.grid.major.x = element_blank(),
      panel.grid.minor.x = element_blank())
  </code></pre>
  <p><a href="https://i.stack.imgur.com/8eYwZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8eYwZ.png" alt="enter image description here" /></a></p>
  <p>But what I want is, irrespective of other points, y axis should sort by date based on the first point in Cluster_07 and Cluster_50.</p>
  <p>For example,</p>
  <p>Cluster_07 has the earliest date so should be at the bottom-most on the y-axis. Similarly Cluster_50 should be placed before uid_21 in the y-axis.</p>
  <p>May I know, if there is a way to do this? Many thanks in advance!</p>