title: stacked barplot with samples in x-axis arranged according to "early" and "late" groups
body: <p>Hi friends I'm working with Phyloseq package to analyse 16S metagenomic data. I want to make a stacked barplot with abundance in y-axis and samples in x-axis. I have 10 samples with 4 early and 6 late. How can I arrange in the barplot. I mean first four bars of controls and next six are of obese.
  I have tried reorder function. But found that reorder only works with numeric value not non-numeric &quot;ealy&quot; and &quot;late&quot; values.
  Here's my code:</p>
  <pre><code>    ggplot(mdata_class, aes(x = Sample, y = Abundance, fill = Class)) + 
        #facet_grid(time~.) +
        geom_bar(stat = &quot;identity&quot;) +
        scale_fill_manual(values = class_colors) +
        scale_x_discrete(
          breaks = map$sample,
          labels = map$time,
          drop = FALSE
        )  +
        # Remove x axis title, and rotate sample labels
        theme(axis.title.x = element_blank(),
              axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) + 
        
        # additional stuff
        guides(fill = guide_legend(reverse = TRUE, keywidth = 1, keyheight = 1)) +
        ylab(&quot;Relative Abundance (Class &gt; 1%) \n&quot;) +
        ggtitle(&quot;Class Composition of Mothur MiSeq SOP data per individual&quot;) 
  </code></pre>
  <p>Here's the output when I print <code>mdata_class</code></p>
  <pre><code>           OTU    Sample  Abundance    sample    type  time bodyweight     Kingdom            Phylum                  Class
      1  Otu00009 DRR046797 0.10213514 DRR046797 control early         20 k__Bacteria p__Actinobacteria      c__Actinobacteria
      2  Otu00009 DRR046803 0.08912090 DRR046803   obese  late         45 k__Bacteria p__Actinobacteria      c__Actinobacteria
      3  Otu00009 DRR046798 0.08548668 DRR046798 control early         40 k__Bacteria p__Actinobacteria      c__Actinobacteria
      4  Otu00009 DRR046801 0.05865369 DRR046801   obese  late         45 k__Bacteria p__Actinobacteria      c__Actinobacteria
      5  Otu00009 DRR046805 0.05629529 DRR046805   obese early         78 k__Bacteria p__Actinobacteria      c__Actinobacteria
      6  Otu00009 DRR046800 0.05349115 DRR046800 control  late         45 k__Bacteria p__Actinobacteria      c__Actinobacteria
      7  Otu00009 DRR046802 0.02630549 DRR046802   obese  late         45 k__Bacteria p__Actinobacteria      c__Actinobacteria
      8  Otu00020 DRR046804 0.14788058 DRR046804   obese  late         45 k__Bacteria     p__Firmicutes             c__Bacilli
      9  Otu00020 DRR046798 0.05880534 DRR046798 control early         40 k__Bacteria     p__Firmicutes             c__Bacilli
      10 Otu00020 DRR046802 0.02007156 DRR046802   obese  late         45 k__Bacteria     p__Firmicutes             c__Bacilli
      11 Otu00020 DRR046803 0.01713152 DRR046803   obese  late         45 k__Bacteria     p__Firmicutes             c__Bacilli
      12 Otu00020 DRR046797 0.01035021 DRR046797 control early         20 k__Bacteria     p__Firmicutes             c__Bacilli
      13 Otu00002 DRR046799 0.39862975 DRR046799 control  late         45 k__Bacteria  p__Bacteroidetes         c__Bacteroidia
      14 Otu00002 DRR046797 0.36894936 DRR046797 control early         20 k__Bacteria  p__Bacteroidetes         c__Bacteroidia
      15 Otu00002 DRR046800 0.35967465 DRR046800 control  late         45 k__Bacteria  p__Bacteroidetes         c__Bacteroidia
      16 Otu00002 DRR046804 0.34913663 DRR046804   obese  late         45 k__Bacteria  p__Bacteroidetes         c__Bacteroidia
      17 Otu00002 DRR046805 0.33464421 DRR046805   obese early         78 k__Bacteria  p__Bacteroidetes         c__Bacteroidia
      18 Otu00002 DRR046798 0.33061613 DRR046798 control early         40 k__Bacteria  p__Bacteroidetes         c__Bacteroidia
      19 Otu00002 DRR046803 0.26273936 DRR046803   obese  late         45 k__Bacteria  p__Bacteroidetes         c__Bacteroidia
      20 Otu00002 DRR046801 0.26121922 DRR046801   obese  late         45 k__Bacteria  p__Bacteroidetes         c__Bacteroidia
      21 Otu00002 DRR046806 0.18225234 DRR046806   obese early         32 k__Bacteria  p__Bacteroidetes         c__Bacteroidia
      22 Otu00002 DRR046802 0.17431231 DRR046802   obese  late         45 k__Bacteria  p__Bacteroidetes         c__Bacteroidia
      23 Otu00035 DRR046798 0.01992751 DRR046798 control early         40 k__Bacteria p__Proteobacteria  c__Betaproteobacteria
      24 Otu00035 DRR046799 0.01478939 DRR046799 control  late         45 k__Bacteria p__Proteobacteria  c__Betaproteobacteria
      25 Otu00035 DRR046803 0.01371867 DRR046803   obese  late         45 k__Bacteria p__Proteobacteria  c__Betaproteobacteria
      26 Otu00035 DRR046804 0.01245559 DRR046804   obese  late         45 k__Bacteria p__Proteobacteria  c__Betaproteobacteria
      27 Otu00035 DRR046801 0.01083689 DRR046801   obese  late         45 k__Bacteria p__Proteobacteria  c__Betaproteobacteria
      28 Otu00001 DRR046806 0.66847252 DRR046806   obese early         32 k__Bacteria     p__Firmicutes          c__Clostridia
      29 Otu00001 DRR046802 0.66746338 DRR046802   obese  late         45 k__Bacteria     p__Firmicutes          c__Clostridia
      30 Otu00001 DRR046801 0.63904709 DRR046801   obese  late         45 k__Bacteria     p__Firmicutes          c__Clostridia
      31 Otu00001 DRR046803 0.57818463 DRR046803   obese  late         45 k__Bacteria     p__Firmicutes          c__Clostridia
      32 Otu00001 DRR046800 0.56657288 DRR046800 control  late         45 k__Bacteria     p__Firmicutes          c__Clostridia
      33 Otu00001 DRR046799 0.56348693 DRR046799 control  late         45 k__Bacteria     p__Firmicutes          c__Clostridia
      34 Otu00001 DRR046805 0.54092091 DRR046805   obese early         78 k__Bacteria     p__Firmicutes          c__Clostridia
      35 Otu00001 DRR046797 0.47789350 DRR046797 control early         20 k__Bacteria     p__Firmicutes          c__Clostridia
      36 Otu00001 DRR046798 0.47385673 DRR046798 control early         40 k__Bacteria     p__Firmicutes          c__Clostridia
      37 Otu00001 DRR046804 0.44762754 DRR046804   obese  late         45 k__Bacteria     p__Firmicutes          c__Clostridia
      38 Otu00014 DRR046802 0.06449178 DRR046802   obese  late         45 k__Bacteria p__Actinobacteria      c__Coriobacteriia
      39 Otu00014 DRR046805 0.03359757 DRR046805   obese early         78 k__Bacteria p__Actinobacteria      c__Coriobacteriia
      40 Otu00014 DRR046797 0.03141518 DRR046797 control early         20 k__Bacteria p__Actinobacteria      c__Coriobacteriia
      41 Otu00014 DRR046803 0.03140499 DRR046803   obese  late         45 k__Bacteria p__Actinobacteria      c__Coriobacteriia
      42 Otu00014 DRR046804 0.01792293 DRR046804   obese  late         45 k__Bacteria p__Actinobacteria      c__Coriobacteriia
      43 Otu00014 DRR046806 0.01570523 DRR046806   obese early         32 k__Bacteria p__Actinobacteria      c__Coriobacteriia
      44 Otu00014 DRR046801 0.01404050 DRR046801   obese  late         45 k__Bacteria p__Actinobacteria      c__Coriobacteriia
      45 Otu00058 DRR046802 0.03083417 DRR046802   obese  late         45 k__Bacteria     p__Firmicutes     c__Erysipelotrichi
      46 Otu00058 DRR046805 0.01128693 DRR046805   obese early         78 k__Bacteria     p__Firmicutes     c__Erysipelotrichi
      47 Otu00031 DRR046806 0.09179629 DRR046806   obese early         32 k__Bacteria   p__Fusobacteria       c__Fusobacteriia
      48 Otu00066 DRR046798 0.02033571 DRR046798 control early         40 k__Bacteria p__Proteobacteria c__Gammaproteobacteria
  </code></pre>
  <p>And here's when I print <code>map</code></p>
  <pre><code>Sample Data:        [10 samples by 4 sample variables]:
               sample    type  time bodyweight
  DRR046797 DRR046797 control early         20
  DRR046798 DRR046798 control early         40
  DRR046799 DRR046799 control  late         45
  DRR046800 DRR046800 control  late         45
  DRR046801 DRR046801   obese  late         45
  DRR046802 DRR046802   obese  late         45
  DRR046803 DRR046803   obese  late         45
  DRR046804 DRR046804   obese  late         45
  DRR046805 DRR046805   obese early         78
  DRR046806 DRR046806   obese early         32
  </code></pre>
  <p><a href="https://i.stack.imgur.com/C3avB.png" rel="nofollow noreferrer">Here's my image link</a></p>
  <p>Thanks and regards,
  DC7</p>