2014-01-13 74 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 answer](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