1

I have the following data frame:

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

I want to plot a bar plot ordered on the Canonical_Hugo_Symbol count.

I use the command :

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

but the column in the plot are still not ordered.

How can I do that?

BenMorel
  • 34,448
  • 50
  • 182
  • 322

2 Answers2

1

You can use the function reorder:

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

enter image description here

If you want to sort the bars in decreasing order, you can use:

reorder(Canonical_Hugo_Symbol, -count).
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
0

Is this what you had in mind:

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

jlhoward
  • 58,004
  • 7
  • 97
  • 140