0

I've got something like 20 facets on a geom_line ggplot2 plot, with an overlaid geom_rect based on timeseries data, all with a facet_wrap. I constantly need to update my plots, and the order of my facets ultimately may need to change on a daily basis.

My question is: is it possible to order my facets using my time-series data in geom_rect? I.e. make the first facet the one that has the first geom_rect shaded area, and so on and so forth?

Here is my code: x-axis is date, y-axis is incidence3, and faceted by geo....

ggplot () + 
geom_rect(data=total,
          aes(xmin=as.Date(xmin), 
              xmax=as.Date(xmax), 
              ymin=-Inf, 
              ymax=Inf), 
          fill='light blue', 
          alpha=0.3) + 
ylab("incidence") + xlab("time") +

facet_wrap(~geo) +
 geom_line(data=total, aes(x=as.Date(date), y=incidence3)) + 
facet_wrap(~geo, ncol=2, scale = "free_y")
Henrik
  • 65,555
  • 14
  • 143
  • 159
Rebecca
  • 1
  • 1
  • Order of facets is determined by the factor levels of, in your case, `geo`. Figure out how your `geom_rect` relates to the order, have that reflected in the order of levels in `geo` and you're done. If I had a reproducible example, I could probably offer more. – Roman Luštrik Feb 06 '15 at 13:41
  • 1
    Please provide a [**minimal, self contained example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – Henrik Feb 06 '15 at 13:42
  • Sorry - didn't realize about the minimal, self-contained example thing. Will work on it. Am really new to R, as well as this site. And Roman, my facets are currently in alphabetical order... – Rebecca Feb 06 '15 at 13:46
  • This question shows how facets are generally reordered by changing the order of levels in the column you are faceting by http://stackoverflow.com/questions/5490638/how-to-change-the-order-of-facet-labels-in-ggplot-custom-facet-wrap-labels – MattLBeck Feb 06 '15 at 14:44

0 Answers0