2

I often facet according to a numeric variable, but I want the facet label to be more explanatory than the simple number. I usually create a new label variable that has the numeric value pasted to explanatory text. However, when values have more than one place before the decimal, the first number is used for sorting the factor. Any suggestions to avoid this?

iris[,1:4]<-iris[,1:4]*10

This would work fine for iris, when it does not have more than one value before the decimal.

iris$Petal.Width.label<-paste("Petal.Width=", iris$Petal.Width)



iris$Petal.Width.label<-paste("Petal.Width=", iris$Petal.Width)


qplot(data=iris,
      x=Sepal.Length,
      y=Sepal.Width,
      colour=Species)+facet_wrap(~Petal.Width.label)

Plot with miss ordered facets

Related to:
ggplot: How to change facet labels?
How to change the order of facet labels in ggplot (custom facet wrap labels)

Community
  • 1
  • 1
Etienne Low-Décarie
  • 13,063
  • 17
  • 65
  • 87
  • 1
    Reordering the factor is the way to go for now, but hopefully `facet_wrap` will [eventually](https://github.com/hadley/ggplot2/issues/25) have a `labeller` argument just like `facet_grid`. – joran Jul 01 '12 at 14:49
  • I din't even know of the labeller argument. Thanks! – Etienne Low-Décarie Jul 02 '12 at 13:57

1 Answers1

5

Just reoder the levels of your label:

data(iris)
iris[ , 1:4] <- iris[ , 1:4] * 10
iris$Petal.Width.label <- paste("Petal.Width=", iris$Petal.Width)
# reoder levels by Petal.Width
iris$Petal.Width.label2 <- factor(iris$Petal.Width.label, 
                                  levels = unique(iris$Petal.Width.label[order(iris$Petal.Width)]))
qplot(data = iris,
      x = Sepal.Length,
      y = Sepal.Width,
      colour = Species)+
        facet_wrap( ~Petal.Width.label2)

enter image description here

EDi
  • 13,160
  • 2
  • 48
  • 57