2016-07-14 131 views
0

我創建一個使用這個腳本的有色網:如何重新排序列在qplot(R)

require(reshape2) 
require(ggplot2) 
sum<-c(0.58,0.65,0.58,0.53,0.41,0.37) 
len<-c(0.24,0.36,0.24,0.21,0.07,0.12) 
mean<-c(0.83,0.81,0.83,0.83,0.80,0.75) 
cl2<-c(0.73,0.75,0.73,0.62,0.60,0.54) 
X<-c("het1","het3","het5","het7","het10","het15") 
df<-data.frame(X,sum,len,mean,cl2) 
a<-melt(df) 
names(a)<-c("het","variables","Correlation") 
qplot(variables, het, fill=Correlation, data=a,geom='tile') 

產生本圖:

enter image description here

正如你在圖中看到,y軸的行沒有正確排序。我需要對此進行排序,以正確順序獲得het1,het3,het5,het7,het10het15。我怎樣才能做到這一點?

回答

1

只是relevel因素創建情節之前:

require(reshape2) 
require(ggplot2) 
sum<-c(0.58,0.65,0.58,0.53,0.41,0.37) 
len<-c(0.24,0.36,0.24,0.21,0.07,0.12) 
mean<-c(0.83,0.81,0.83,0.83,0.80,0.75) 
cl2<-c(0.73,0.75,0.73,0.62,0.60,0.54) 
X<-c("het1","het3","het5","het7","het10","het15") 
df<-data.frame(X,sum,len,mean,cl2) 
a<-melt(df) 

names(a)<-c("het","variables","Correlation") 
a$het = factor(a$het, levels = X) 
qplot(variables, het, fill=Correlation, data=a,geom='tile') 
+0

非常感謝尼克,但我仍然得到同樣的行順序:的Het1 het10 het15的Het3 het5 het7 ...我需要的是團Het 3的Het1 het5 het7 het10 het15 ... – Cisco

+0

您是否重新運行了整個代碼?當我運行這段代碼時,它會產生你正在尋找的y軸順序。 –

+0

對不起。訂購是錯誤的。名稱(a)聲明後的重新級別。我編輯了答案。 –