title: Change plotting order of bars in ggplot2
body: <p>I'm preparing an appendix plot for a revised manuscript where I need to give information of the within-year ranges (variability) of several variables between years and sites. </p>
  
  <p>I figured the tidiest way to do this (I have 7 sites, 21 years, and 5 variables...) would be to use a rose plot using <code>coord_polar</code>. However, I stumbled upon something that has always frustrated me about ggplot - the default ordering assumptions. While factors are easily reordered based on some value, this seems to only work in a <strong>fixed</strong> fashion: as far as I've understood, the order needs to apply throughout the data frame. </p>
  
  <p>In this plot, the ordering needs to depend on a value which changes between years, and therefore the <code>colour</code> and <code>fill</code> values need to change in plotting order within the panel.</p>
  
  <p>To demonstrate, I've created a reproducible example coded below (pictured in the way it should not work)<a href="https://i.stack.imgur.com/gTVur.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gTVur.png" alt="example of wrong ordering"></a></p>
  
  <p>Basically, I always need the Site with the minimum value within a given Year to be plotted first (in the centre), followed outwards by the increase in value of the other sites, in order of the original value (see <code>order</code> and <code>diff</code> columns of the data frame). In other words, some years Site a will be at the centre, some years Site c will be in the centre, etc.</p>
  
  <p>Any help would be massively appreciated.</p>
  
  <pre><code>library('ggplot2')
  library('reshape2')
  library("plyr")
  
  ## reproducible example of problem: create dummy data
  madeup &lt;- data.frame(Year = rep(2000:2015, each=20), Site=rep(c("a","b","c","d"), each=5, times=16),
                       var1 = rnorm(n=16*20, mean=20, sd=5), var2= rnorm(n=16*20, mean=50, sd=1))
  
  ## create ranges of the data by Year and Site
  myRange &lt;- function(dat) {range=max(dat, na.rm=TRUE)-min(dat,na.rm = TRUE)}
  vardf &lt;- ddply(madeup, .(Site, Year), summarise, var1=myRange(var1),
                 var2=myRange(var2))
  
  varmelt &lt;- melt(vardf, id.vars = c("Site","Year"))
  varmelt$Site &lt;- as.character(varmelt$Site) # this to preserve the new order when rbind called
  varmelt &lt;- by(varmelt, list(varmelt$Year, varmelt$variable), function(x) {x &lt;- x[order(x$value),]
  x$order &lt;- 1:nrow(x)
  return(x)})
  varmelt &lt;- do.call(rbind, varmelt)
  
  ## create difference between these values so that each site gets plotted cumulatively on the rose plot
  ##  (otherwise areas close to the centre become uninterpretable)
  vartest &lt;- by(varmelt, list(varmelt$Year, varmelt$variable), function(x) {
    x$diff &lt;- c(x$value[1], diff(x$value))
    return(x)
  })
  vartest &lt;- do.call(rbind,vartest)
  
  ## plot rose plot to display how ranges in variables vary by year and between sites
  ## for this test example we'll just take one variable, but the idea is to facet by variable
  max1 &lt;- max(vartest$value[vartest$variable=='var1'])
  yearlength &lt;- length(2000:2015)
  ggplot(vartest[vartest$variable=="var1",], aes(x=factor(Year), y=diff)) +
    theme_bw() +
    geom_hline(yintercept = seq(0,max1, by=1), size=0.3, col="grey60",lty=3) +
    geom_vline(xintercept=seq(1,yearlength,1), size=0.3, col='grey30', lty=2) +
    geom_bar(stat='identity', width=1, size=0.5, aes(col=Site, fill=Site)) +
    scale_x_discrete() +
    coord_polar() +
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
  </code></pre>