0

I am tring to create stacked bar plot, and I want to sort it that the largest stack to be at the buttom of plot, the nest one will be on top of it and so on.

This is my code:

g <- ggplot(df, aes(x = Year, y = Value))
g + geom_bar(aes(fill=Type), width = 0.5, stat="identity") + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6),
        axis.text.y = element_blank()) +
  scale_fill_brewer(palette="Set3") +
  labs(title="GEO Types", 
       subtitle="")

This is my data frame:

       Year                       Type Value
tmp    2010   Genome binding/occupancy   664
tmp.1  2010 Genome variation profiling   313
tmp.2  2010     Methylation profailing     0
tmp.3  2010   Non-coding RNA profiling   344
tmp.4  2010                      Other    71
tmp.5  2010          Protein profiling     8
tmp.6  2010             SNP genotyping    75
tmp.7  2010     Third-party reanalysis    15
tmp.8  2010       Expression profiling  4615
tmp.9  2011   Genome binding/occupancy   929
tmp.10 2011 Genome variation profiling   334
tmp.11 2011     Methylation profailing     0
tmp.12 2011   Non-coding RNA profiling   463
tmp.13 2011                      Other   123
tmp.14 2011          Protein profiling    21
tmp.15 2011             SNP genotyping    79
tmp.16 2011     Third-party reanalysis    18
tmp.17 2011       Expression profiling  5629
tmp.18 2012   Genome binding/occupancy   792
tmp.19 2012 Genome variation profiling   345
tmp.20 2012     Methylation profailing     0
tmp.21 2012   Non-coding RNA profiling   548
tmp.22 2012                      Other   175
tmp.23 2012          Protein profiling    21
tmp.24 2012             SNP genotyping    65
tmp.25 2012     Third-party reanalysis    23
tmp.26 2012       Expression profiling  6297
tmp.27 2013   Genome binding/occupancy  1527
tmp.28 2013 Genome variation profiling   270
tmp.29 2013     Methylation profailing     0
tmp.30 2013   Non-coding RNA profiling   688
tmp.31 2013                      Other   332
tmp.32 2013          Protein profiling    39
tmp.33 2013             SNP genotyping    85
tmp.34 2013     Third-party reanalysis    23
tmp.35 2013       Expression profiling  7037

And this is the plot I get:

plot

I want "Expression profiling" will be at the buttom of the plot.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Rachel
  • 41
  • 1
  • 4
  • Yep duplicate. Use factors to order. – MDEWITT May 12 '19 at 14:28
  • Use ... + geom_bar(aes(fill=fct_reorder(Type, Value, sum, desc=TRUE)), width = 0.5, stat="identity") + ... the variable will be converted to factor and reordered. forcats::fct_xxxx functions are more convenient in this role. – Jacek Kotowski May 12 '19 at 17:41

0 Answers0