如何重新排序qplot(R)中的行

时间:2016-07-14 13:20:12

标签: r sorting ggplot2

我使用这个脚本创建一个彩色网格:

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轴的行未正确排序。我需要对此进行排序,以正确的顺序获取het1het3het5het7het10het15。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

在创建情节之前,只需重新考虑因素:

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')