Question

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)

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top