2012-07-01 31 views
2

我經常根據數值變量來分面,但我希望facet標籤比簡單數字更具解釋性。我通常創建一個新的標籤變量,其數值粘貼到解釋性文本。但是,如果值在小數點前有多個地方,則使用第一個數字來排序因子。任何建議,以避免這一點?如何爲數值創建相當有序的構面標籤?

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

對於虹膜,當它在小數點前沒有多個值時,這將工作正常。

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

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

+1

重新排序因子是現在的方式,但希望'facet_wrap'將[最終](https://github.com/hadley/ggplot2/issues/25)有一個'labeller'參數,就像'facet_grid' 。 – joran

+0

我甚至不知道貼標籤的參數。謝謝! –

回答

5

只是reoder您的標籤的層次:

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

相關問題