我有以下数据框:
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 :(得分:1)
您可以使用reorder
:
qplot(reorder(Canonical_Hugo_Symbol, count),
data = ddat, fill = Canonical_Variant_Classification, geom = "bar")
如果要按降序排列条形,可以使用:
reorder(Canonical_Hugo_Symbol, -count).
答案 1 :(得分: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")