4

I have an existing dataset with three factors. I would like to plot these three factors using facet_grid() and have them ordered based on how they are ordered in the dataset instead of alphabetical order. Is this possible to do somehow without modifying my data structure?

Here's the data:

https://dl.dropboxusercontent.com/u/22681355/data.csv

data<-read.csv("data.csv", head=T)

ggplot(data, aes(time,a, color="one")) + 
    geom_line(linetype=1, size=0.3) + 
    scale_y_continuous(breaks=seq(0,1,0.2)) + 
    scale_x_continuous(breaks=seq(100,300,50)) + 
    theme_bw() + 
    geom_line(aes(time,b)) + 
    geom_line(aes(time,c)) + 
    geom_line(aes(time,d))+facet_wrap(~X.1)
Arun
  • 116,683
  • 26
  • 284
  • 387
user1723765
  • 6,179
  • 18
  • 57
  • 85

1 Answers1

13

This question appears quite too often on SO. You've to get the desired column (by which you're facetting) as a factor with levels in the order you desire, as follows:

data$X.1 <- factor(data$X.1, levels=unique(data$X.1))

Now, plot it and you'll get the facetted plot in the desired order.

enter image description here

Arun
  • 116,683
  • 26
  • 284
  • 387
  • 3
    You'll only get the facets in the correct order if the factor order is correct. Don't forget you can set the order separate to the levels. – Andy Clifton Aug 05 '13 at 21:11