2014-12-01 59 views
0

我是R的新手。我製作了我的數據盒狀圖,但是目前R按字母順序排列因素。如何維護我的數據的原始順序?這是我的代碼:如何使用原始數據順序(不按字母順序)生成箱圖?

boxplot(MS~Code,data=Input) 

我有40個變量,我希望boxplot與原始數據幀列出他們相同的順序。我讀過,我可能能夠設置sort.names = FALSE來維持原始順序,我不明白代碼片段的位置。

有沒有辦法在輸入到boxplot之前重新定義我的輸入?

謝謝。

+0

確實[這](http://stackoverflow.com/questions/4260698/r-ordering-in-boxplot)幫助? – jlhoward 2014-12-01 23:29:48

+0

我忘了什麼? – jlhoward 2014-12-01 23:52:30

回答

1

因素,因爲你在第3行

data(InsectSprays) 
data <- InsectSprays 
data$spray <- factor(data$spray, c("B", "C", "D", "E", "F", "G", "A")) 
boxplot(count ~ spray, data = data, col = "lightgray") 

以上答案是那裏的方式98%希望再次變量。

set.seed(1) 
# original order is E - A 
Input <- data.frame(Code=rep(rev(LETTERS[1:5]),each=5), 
        MS=rnorm(25,sample(1:5,5))) 
boxplot(MS~Code,data=Input) # plots alphabetically 

Input$Code <- with(Input,factor(Code,levels=unique(Code))) 
boxplot(MS~Code,data=Input) # plots in original order 

+0

必須有一個更簡單的方法?我有40個因素,我不想試圖找出第一個來。當我把它們放到R中時,它們已經處於有序狀態。 – val 2014-12-01 23:39:13

+1

使用'data $ spray < - with(data,factor(spray,levels = unique(spray))' – jlhoward 2014-12-02 00:01:08