2014-01-13 2 views
1

의 건의에 따라 플롯의 열 나는 다음과 같은 데이터 프레임이 있습니다주문 수준

ddat <- data.frame(Canonical_Hugo_Symbol=sample(letters[1:4], 20, replace = TRUE), ID.name=rep(c("0", "1"), each=10), allele.fraction =runif(20,min=0,max=1), Canonical_Variant_Classification =sample(letters[1:4], 20, replace = TRUE)) 

    Canonical_Hugo_Symbol ID.name allele.fraction Canonical_Variant_Classification 
1      b  0  0.47877015        a 
2      a  0  0.98445203        a 
3      d  0  0.91065285        d 
4      b  0  0.93833143        c 
5      d  0  0.53332525        d 
6      a  0  0.14730869        a 
7      b  0  0.71067695        b 
8      a  0  0.46656093        d 
9      d  0  0.64203393        b 
10      a  0  0.48894393        b 
11      a  1  0.17165993        a 
12      a  1  0.02641931        c 
13      b  1  0.46169460        b 
14      b  1  0.96254767        c 
15      a  1  0.81565680        d 
16      c  1  0.95940276        d 
17      b  1  0.28574428        c 
18      c  1  0.10000156        c 
19      c  1  0.19250335        c 
20      c  1  0.72370884        b 

내가 Canonical_Hugo_Symbol 수에 주문한 바 플롯을 플롯 할을.

ddat$count <- ave(as.numeric(ddat$Canonical_Hugo_Symbol), ddat$Canonical_Hugo_Symbol, FUN = length) 
ddat <- ddat[order(ddat$count, decreasing = T),] 
qplot(Canonical_Hugo_Symbol, data= ddat, fill= Canonical_Variant_Classification, geom="bar") 

하지만 플롯의 열이 여전히 정렬되지 않습니다

나는 명령을 사용합니다.

어떻게하면됩니까?

+0

이것은 거의 중복되는 질문입니다. 이 봐 [stackoverflow 대답] (http://stackoverflow.com/questions/3744178/ggplot2-sorting-a-plot) – BrodieG

+0

죄송합니다 전에 그것을 찾지 못했습니다! 감사합니다 – user3186183

답변

0

당신이 가지고이 무엇인가 염두에두고 :

library(ggplot2) 
set.seed(1) 
ddat <- data.frame(Canonical_Hugo_Symbol=sample(letters[1:4], 20, replace = TRUE), ID.name=rep(c("0", "1"), each=10), allele.fraction =runif(20,min=0,max=1), Canonical_Variant_Classification =sample(letters[1:4], 20, replace = TRUE)) 
ddat$count <- ave(as.numeric(ddat$Canonical_Hugo_Symbol), 
        ddat$Canonical_Hugo_Symbol, FUN = length) 
ddat  <- ddat[order(ddat$count,ddat$Canonical_Hugo_Symbol, decreasing = T),] 
ddat$Canonical_Hugo_Symbol <- factor(ddat$Canonical_Hugo_Symbol, 
            levels=unique(ddat$Canonical_Hugo_Symbol)) 
qplot(Canonical_Hugo_Symbol, data= ddat, 
     fill= Canonical_Variant_Classification, geom="bar") 

1

당신은 기능 reorder을 사용할 수 있습니다 : 당신이 내림차순으로 막대를 정렬 할 경우

qplot(reorder(Canonical_Hugo_Symbol, count), 
     data = ddat, fill = Canonical_Variant_Classification, geom = "bar") 

enter image description here

, 당신은 사용할 수 있습니다

reorder(Canonical_Hugo_Symbol, -count). 
+0

어떤면에서 내 경우에는 열을 재정렬하지 않습니다. 나는 전에 시도했다. 감사! – user3186183