Dawid Sikorski
Dawid Sikorski

Reputation: 3

Problems with sorting stacked bars - ggplot2

I'm working with R and ggplot and i have problem with sorting bars in stacked bar plot. All i want to do, is sorting freq descending on each bar.

This is how the plot looks now. enter image description here

Here is sample of data frame:

Variant                            Chromosome  Count
frameshift_variant                      1      65388
missense_variant                        1      1604657
other                                   1      849
splicing                                1      340442
start_lost                              1      4254
stop_gained                             1      48865
stop_lost                               1      1759
synonymous_variant                      1      765529
incomplete_terminal_codon_variant       1      255
stop_retained_variant                   1      1011 

This is my code in R:

ggplot(mean_freq,aes(x = Chromosome, y = Freq, fill = Variant))
+ geom_bar(position = "fill",stat = "identity",colour = "black")
+ guides(fill=guide_legend(title="Type of Variant")) 
+ xlab("\nChromosome") + ylab("Freq\n") 
+ scale_x_continuous(breaks=1:22)

Thanks for any help.

Upvotes: 0

Views: 65

Answers (1)

myincas
myincas

Reputation: 1550

You should reorder Variant on Freq.

ggplot(mean_freq, aes(x = Chromosome, y = Freq, fill = reorder(Variant, -Freq)))

Upvotes: 1

Related Questions