title: Ordering factor levels with the same name across different variables - facet_grid in ggplot2
body: <p>I am plotting a timeline or time series plot of birds that I attached radio-transmitters to, and followed over the course of a breeding season. The timeline shows when I first tagged a bird, and when I stopped tracking a bird.</p>
  
  <p>Each bird is labelled with its radio tag frequency, which is 6 numbers (151.XXX). 2 radio tags, 151.164 and 151.094, were used in both years. This is problematic when I use facet_grid to plot the timelines by year. Basically all birds are plotted, but this messes up the ordering of the plots (I would like to have the initial date a bird was tagged to go in order; see plot below). Is it possible to keep these factor levels the same but fix the ordering?</p>
  
  <p>My data frame looks like this:  </p>
  
  <pre><code>dat&lt;-structure(list(freq = structure(c(21L, 1L, 32L, 8L, 11L, 16L, 
  5L, 30L, 13L, 26L, 10L, 19L, 22L, 34L, 23L, 4L, 17L, 33L, 36L, 
  3L, 14L, 31L, 24L, 35L, 15L, 20L, 27L, 29L, 6L, 18L, 28L, 25L, 
  12L, 9L, 7L, 2L), .Label = c("094_1", "094_2", "11", "112", "122", 
  "132", "152", "164_1", "164_2", "179", "191", "216", "226", "231", 
  "250", "251", "265", "280", "295", "338", "34", "372", "38", 
  "385", "429", "46", "475", "53", "558", "57", "71", "72", "831", 
  "876", "919", "965"), class = "factor"), site = structure(c(3L, 
  4L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
  3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 3L, 
  3L, 3L, 3L), .Label = c("BSLP", "GSPR", "HSGL", "SCFA"), class = "factor"), 
  zone = c(18L, 17L, 18L, 18L, 17L, 18L, 18L, 18L, 18L, 18L, 
  18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
  17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 
  18L, 18L), year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
  2014L, 2014L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L, 2015L, 
  2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
  2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
  2015L, 2015L, 2015L), jstart = c(106L, 113L, 119L, 119L, 
  122L, 124L, 125L, 125L, 128L, 131L, 104L, 104L, 104L, 104L, 
  104L, 105L, 105L, 105L, 105L, 105L, 106L, 106L, 109L, 109L, 
  110L, 110L, 110L, 110L, 111L, 111L, 115L, 121L, 139L, 142L, 
  147L, 159L), jend = c(147L, 154L, 133L, 129L, 143L, 170L, 
  168L, 170L, 164L, 178L, 153L, 160L, 147L, 156L, 140L, 141L, 
  160L, 122L, 166L, 160L, 133L, 160L, 149L, 160L, 162L, 158L, 
  155L, 159L, 162L, 163L, 156L, 160L, 194L, 173L, 196L, 202L
  )), .Names = c("freq", "site", "zone", "year", "jstart", 
  "jend"), class = "data.frame", row.names = c(NA, -36L))
  </code></pre>
  
  <p>And here is the code:</p>
  
  <pre><code>library(ggplot2)
  library(RColorBrewer)
  
  dat$freq&lt;-as.factor(dat$freq)
  dat$year&lt;-as.factor(dat$year)
  
  # change the factor names:
  levels(dat$freq) #just checking the levels
  levels(dat$freq)[levels(dat$freq)=="094_1"] &lt;- "151.094"
  levels(dat$freq)[levels(dat$freq)=="094_2"] &lt;- "151.094"
  levels(dat$freq)[levels(dat$freq)=="164_1"] &lt;- "151.164"
  levels(dat$freq)[levels(dat$freq)=="164_2"] &lt;- "151.164"
  levels(dat$freq)[levels(dat$freq)=="34"] &lt;- "151.034"
  levels(dat$freq)[levels(dat$freq)=="72"] &lt;- "151.072"
  levels(dat$freq)[levels(dat$freq)=="191"] &lt;- "151.191"
  levels(dat$freq)[levels(dat$freq)=="251"] &lt;- "151.251"
  levels(dat$freq)[levels(dat$freq)=="122"] &lt;- "151.122"
  levels(dat$freq)[levels(dat$freq)=="57"] &lt;- "151.057"
  levels(dat$freq)[levels(dat$freq)=="226"] &lt;- "151.226"
  levels(dat$freq)[levels(dat$freq)=="179"] &lt;- "151.179"
  levels(dat$freq)[levels(dat$freq)=="295"] &lt;- "151.295"
  levels(dat$freq)[levels(dat$freq)=="372"] &lt;- "151.372"
  levels(dat$freq)[levels(dat$freq)=="876"] &lt;- "151.876"
  levels(dat$freq)[levels(dat$freq)=="38"] &lt;- "151.038"
  levels(dat$freq)[levels(dat$freq)=="112"] &lt;- "151.112"
  levels(dat$freq)[levels(dat$freq)=="265"] &lt;- "151.265"
  levels(dat$freq)[levels(dat$freq)=="831"] &lt;- "151.831"
  levels(dat$freq)[levels(dat$freq)=="965"] &lt;- "151.965"
  levels(dat$freq)[levels(dat$freq)=="11"] &lt;- "151.011"
  levels(dat$freq)[levels(dat$freq)=="231"] &lt;- "151.231"
  levels(dat$freq)[levels(dat$freq)=="71"] &lt;- "151.071"
  levels(dat$freq)[levels(dat$freq)=="385"] &lt;- "151.385"
  levels(dat$freq)[levels(dat$freq)=="919"] &lt;- "151.919"
  levels(dat$freq)[levels(dat$freq)=="250"] &lt;- "151.250"
  levels(dat$freq)[levels(dat$freq)=="338"] &lt;- "151.338"
  levels(dat$freq)[levels(dat$freq)=="475"] &lt;- "151.475"
  levels(dat$freq)[levels(dat$freq)=="558"] &lt;- "151.558"
  levels(dat$freq)[levels(dat$freq)=="132"] &lt;- "151.132"
  levels(dat$freq)[levels(dat$freq)=="280"] &lt;- "151.280"
  levels(dat$freq)[levels(dat$freq)=="53"] &lt;- "151.053"
  levels(dat$freq)[levels(dat$freq)=="429"] &lt;- "151.429"
  levels(dat$freq)[levels(dat$freq)=="216"] &lt;- "151.216"
  levels(dat$freq)[levels(dat$freq)=="152"] &lt;- "151.152"
  levels(dat$freq)[levels(dat$freq)=="46"] &lt;- "151.046"
  
  # order the factors:
  dat$freq &lt;- factor(dat$freq, levels = rev(dat$freq[order(dat$year,dat$jstart)]))
  dat$freq # notice the changed order of factor levels
  
  # plot the data:
  ggplot(dat) +
   geom_segment(aes(x = jstart, y = freq, xend = jend, yend = freq, color=site), alpha=0.5, size = 3) +
   scale_color_brewer(palette="Set1", name="Study site") +
   xlab("Date") +
   ylab("Bird ID") +
   theme_bw() +
   theme(legend.position="bottom",
      legend.title = element_text(colour="black", size=16, face="bold"),
      legend.text = element_text(colour="black", size = 14, face = "plain"),
      axis.title.x = element_text(colour="black", size=16, vjust=-0.1, face="bold"),
      axis.text.x  = element_text(angle=0, vjust=1, size=14, colour="black"),
      axis.title.y =element_text(colour="black", size=16, vjust=1.3, face="bold"),
      axis.text.y  = element_text(angle=0, size=14, colour="black"),
      strip.text.y = element_text(size=18)) + 
  facet_grid(year ~ ., scales = "free_y", space = "free_y") +
  scale_x_continuous(labels = function(x) format(as.Date(as.character(x), "%j"), "%d-%b"))
  </code></pre>
  
  <p>The factors that are present in both years, 151.164 and 151.094, do not sort correctly for 2014. </p>
  
  <p><a href="https://i.stack.imgur.com/hER2Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hER2Y.png" alt="timelineplot"></a></p>
  
  <p>Is there a way to correct this? I would like to keep these factor level names the same (i.e. 151.XXX, and retain 151.164 and 151.094 for both years).</p>
  
  <p>Thanks, Jay</p>