title: How to creat a bar graph of microbiota data with one color for higher taxonomic rank and gradient color
body: <p>I have a Phyloseq object with my OTU table and TAX table.
  I would like to create a bar plot, at for instance family level, but families belonging to the same Phylum will be displayed with the same colour and be distinguished by a gradient of this color.</p>
  <p>The final result should be similar to this:</p>
  <p><img src="https://i.stack.imgur.com/mu56c.png" alt="Good Bar plot" /></p>
  <p>I converted my phyloseq object into a dataframe using <code>psmelt()</code> and tried to adapt the code from this post : <a href="https://stackoverflow.com/questions/49818271/stacked-barplot-with-colour-gradients-for-each-bar?rq=1">Stacked barplot with colour gradients for each bar</a></p>
  <p>But I'm currently unable to create a correct graph.</p>
  <pre><code>library(phyloseq)
  library(ggplot2)
  
  df &lt;- psmelt(GlobalPatterns)
  df$group &lt;- paste0(df$Phylum, &quot;-&quot;, df$Family, sep = &quot;&quot;)
  colours &lt;-ColourPalleteMulti(df, &quot;Phylum&quot;, &quot;Family&quot;)
  ggplot(df, aes(Sample)) + 
    geom_bar(aes(fill = group), colour = &quot;grey&quot;) +
    scale_fill_manual(&quot;Subject&quot;, values=colours, guide = &quot;none&quot;)
  </code></pre>
  <blockquote>
  <p>Erreur : Insufficient values in manual scale. 395 needed but only 334 provided.</p>
  </blockquote>
  <p>Thank you in advance for any help !</p>
  <p><strong>Edit: here the dput of the data</strong></p>
  <pre><code>    dput(head(df, 10))
  structure(list(OTU = c(&quot;549656&quot;, &quot;279599&quot;, &quot;549656&quot;, &quot;549656&quot;, 
  &quot;360229&quot;, &quot;331820&quot;, &quot;94166&quot;, &quot;331820&quot;, &quot;329744&quot;, &quot;189047&quot;), Sample = c(&quot;AQC4cm&quot;, 
  &quot;LMEpi24M&quot;, &quot;AQC7cm&quot;, &quot;AQC1cm&quot;, &quot;M31Tong&quot;, &quot;M11Fcsw&quot;, &quot;M31Tong&quot;, 
  &quot;M31Fcsw&quot;, &quot;SLEpi20M&quot;, &quot;TS29&quot;), Abundance = c(1177685, 914209, 
  711043, 554198, 540850, 452219, 396201, 354695, 323914, 251215
  ), X.SampleID = structure(c(2L, 10L, 3L, 1L, 16L, 11L, 16L, 14L, 
  20L, 26L), .Label = c(&quot;AQC1cm&quot;, &quot;AQC4cm&quot;, &quot;AQC7cm&quot;, &quot;CC1&quot;, &quot;CL3&quot;, 
  &quot;Even1&quot;, &quot;Even2&quot;, &quot;Even3&quot;, &quot;F21Plmr&quot;, &quot;LMEpi24M&quot;, &quot;M11Fcsw&quot;, 
  &quot;M11Plmr&quot;, &quot;M11Tong&quot;, &quot;M31Fcsw&quot;, &quot;M31Plmr&quot;, &quot;M31Tong&quot;, &quot;NP2&quot;, 
  &quot;NP3&quot;, &quot;NP5&quot;, &quot;SLEpi20M&quot;, &quot;SV1&quot;, &quot;TRRsed1&quot;, &quot;TRRsed2&quot;, &quot;TRRsed3&quot;, 
  &quot;TS28&quot;, &quot;TS29&quot;), class = &quot;factor&quot;), Primer = structure(c(14L, 
  11L, 15L, 13L, 9L, 5L, 9L, 4L, 12L, 23L), .Label = c(&quot;ILBC_01&quot;, 
  &quot;ILBC_02&quot;, &quot;ILBC_03&quot;, &quot;ILBC_04&quot;, &quot;ILBC_05&quot;, &quot;ILBC_07&quot;, &quot;ILBC_08&quot;, 
  &quot;ILBC_09&quot;, &quot;ILBC_10&quot;, &quot;ILBC_11&quot;, &quot;ILBC_13&quot;, &quot;ILBC_15&quot;, &quot;ILBC_16&quot;, 
  &quot;ILBC_17&quot;, &quot;ILBC_18&quot;, &quot;ILBC_19&quot;, &quot;ILBC_20&quot;, &quot;ILBC_21&quot;, &quot;ILBC_22&quot;, 
  &quot;ILBC_23&quot;, &quot;ILBC_24&quot;, &quot;ILBC_25&quot;, &quot;ILBC_26&quot;, &quot;ILBC_27&quot;, &quot;ILBC_28&quot;, 
  &quot;ILBC_29&quot;), class = &quot;factor&quot;), Final_Barcode = structure(c(14L, 
  11L, 15L, 13L, 9L, 5L, 9L, 4L, 12L, 23L), .Label = c(&quot;AACGCA&quot;, 
  &quot;AACTCG&quot;, &quot;AACTGT&quot;, &quot;AAGAGA&quot;, &quot;AAGCTG&quot;, &quot;AATCGT&quot;, &quot;ACACAC&quot;, &quot;ACACAT&quot;, 
  &quot;ACACGA&quot;, &quot;ACACGG&quot;, &quot;ACACTG&quot;, &quot;ACAGAG&quot;, &quot;ACAGCA&quot;, &quot;ACAGCT&quot;, &quot;ACAGTG&quot;, 
  &quot;ACAGTT&quot;, &quot;ACATCA&quot;, &quot;ACATGA&quot;, &quot;ACATGT&quot;, &quot;ACATTC&quot;, &quot;ACCACA&quot;, &quot;ACCAGA&quot;, 
  &quot;ACCAGC&quot;, &quot;ACCGCA&quot;, &quot;ACCTCG&quot;, &quot;ACCTGT&quot;), class = &quot;factor&quot;), Barcode_truncated_plus_T = structure(c(6L, 
  10L, 8L, 25L, 19L, 9L, 19L, 20L, 14L, 16L), .Label = c(&quot;AACTGT&quot;, 
  &quot;ACAGGT&quot;, &quot;ACAGTT&quot;, &quot;ACATGT&quot;, &quot;ACGATT&quot;, &quot;AGCTGT&quot;, &quot;ATGTGT&quot;, &quot;CACTGT&quot;, 
  &quot;CAGCTT&quot;, &quot;CAGTGT&quot;, &quot;CCGTGT&quot;, &quot;CGAGGT&quot;, &quot;CGAGTT&quot;, &quot;CTCTGT&quot;, &quot;GAATGT&quot;, 
  &quot;GCTGGT&quot;, &quot;GTGTGT&quot;, &quot;TCATGT&quot;, &quot;TCGTGT&quot;, &quot;TCTCTT&quot;, &quot;TCTGGT&quot;, &quot;TGATGT&quot;, 
  &quot;TGCGGT&quot;, &quot;TGCGTT&quot;, &quot;TGCTGT&quot;, &quot;TGTGGT&quot;), class = &quot;factor&quot;), Barcode_full_length = structure(c(4L, 
  7L, 3L, 13L, 26L, 8L, 26L, 21L, 2L, 11L), .Label = c(&quot;AGAGAGACAGG&quot;, 
  &quot;AGCCGACTCTG&quot;, &quot;ATGAAGCACTG&quot;, &quot;CAAGCTAGCTG&quot;, &quot;CACGTGACATG&quot;, &quot;CATCGACGAGT&quot;, 
  &quot;CATGAACAGTG&quot;, &quot;CGACTGCAGCT&quot;, &quot;CGAGTCACGAT&quot;, &quot;CTAGCGTGCGT&quot;, &quot;CTAGTCGCTGG&quot;, 
  &quot;GAACGATCATG&quot;, &quot;GACCACTGCTG&quot;, &quot;GATGTATGTGG&quot;, &quot;GCATCGTCTGG&quot;, &quot;GCCATAGTGTG&quot;, 
  &quot;GCTAAGTGATG&quot;, &quot;GTACGCACAGT&quot;, &quot;GTAGACATGTG&quot;, &quot;TAGACACCGTG&quot;, &quot;TCGACATCTCT&quot;, 
  &quot;TCGCGCAACTG&quot;, &quot;TCTGATCGAGG&quot;, &quot;TGACTCTGCGG&quot;, &quot;TGCGCTGAATG&quot;, &quot;TGTGGCTCGTG&quot;
  ), class = &quot;factor&quot;), SampleType = structure(c(3L, 2L, 3L, 3L, 
  9L, 1L, 9L, 1L, 2L, 1L), .Label = c(&quot;Feces&quot;, &quot;Freshwater&quot;, &quot;Freshwater (creek)&quot;, 
  &quot;Mock&quot;, &quot;Ocean&quot;, &quot;Sediment (estuary)&quot;, &quot;Skin&quot;, &quot;Soil&quot;, &quot;Tongue&quot;
  ), class = &quot;factor&quot;), Description = structure(c(2L, 10L, 3L, 
  1L, 16L, 11L, 16L, 14L, 21L, 25L), .Label = c(&quot;Allequash Creek, 0-1cm depth&quot;, 
  &quot;Allequash Creek, 3-4 cm depth&quot;, &quot;Allequash Creek, 6-7 cm depth&quot;, 
  &quot;Calhoun South Carolina Pine soil, pH 4.9&quot;, &quot;Cedar Creek Minnesota, grassland, pH 6.1&quot;, 
  &quot;Even1&quot;, &quot;Even2&quot;, &quot;Even3&quot;, &quot;F1, Day 1,  right palm, whole body study &quot;, 
  &quot;Lake Mendota Minnesota, 24 meter epilimnion &quot;, &quot;M1, Day 1, fecal swab, whole body study &quot;, 
  &quot;M1, Day 1, right palm, whole body study &quot;, &quot;M1, Day 1, tongue, whole body study &quot;, 
  &quot;M3, Day 1, fecal swab, whole body study&quot;, &quot;M3, Day 1, right palm, whole body study&quot;, 
  &quot;M3, Day 1, tongue, whole body study &quot;, &quot;Newport Pier, CA surface water, Time 1&quot;, 
  &quot;Newport Pier, CA surface water, Time 2&quot;, &quot;Newport Pier, CA surface water, Time 3&quot;, 
  &quot;Sevilleta new Mexico, desert scrub, pH 8.3&quot;, &quot;Sparkling Lake Wisconsin, 20 meter eplimnion&quot;, 
  &quot;Tijuana River Reserve, depth 1&quot;, &quot;Tijuana River Reserve, depth 2&quot;, 
  &quot;Twin #1&quot;, &quot;Twin #2&quot;), class = &quot;factor&quot;), Kingdom = c(&quot;Bacteria&quot;, 
  &quot;Bacteria&quot;, &quot;Bacteria&quot;, &quot;Bacteria&quot;, &quot;Bacteria&quot;, &quot;Bacteria&quot;, &quot;Bacteria&quot;, 
  &quot;Bacteria&quot;, &quot;Bacteria&quot;, &quot;Bacteria&quot;), Phylum = c(&quot;Cyanobacteria&quot;, 
  &quot;Cyanobacteria&quot;, &quot;Cyanobacteria&quot;, &quot;Cyanobacteria&quot;, &quot;Proteobacteria&quot;, 
  &quot;Bacteroidetes&quot;, &quot;Proteobacteria&quot;, &quot;Bacteroidetes&quot;, &quot;Actinobacteria&quot;, 
  &quot;Firmicutes&quot;), Class = c(&quot;Chloroplast&quot;, &quot;Nostocophycideae&quot;, &quot;Chloroplast&quot;, 
  &quot;Chloroplast&quot;, &quot;Betaproteobacteria&quot;, &quot;Bacteroidia&quot;, &quot;Gammaproteobacteria&quot;, 
  &quot;Bacteroidia&quot;, &quot;Actinobacteria&quot;, &quot;Clostridia&quot;), Order = c(&quot;Stramenopiles&quot;, 
  &quot;Nostocales&quot;, &quot;Stramenopiles&quot;, &quot;Stramenopiles&quot;, &quot;Neisseriales&quot;, 
  &quot;Bacteroidales&quot;, &quot;Pasteurellales&quot;, &quot;Bacteroidales&quot;, &quot;Actinomycetales&quot;, 
  &quot;Clostridiales&quot;), Family = c(NA, &quot;Nostocaceae&quot;, NA, NA, &quot;Neisseriaceae&quot;, 
  &quot;Bacteroidaceae&quot;, &quot;Pasteurellaceae&quot;, &quot;Bacteroidaceae&quot;, &quot;ACK-M1&quot;, 
  &quot;Ruminococcaceae&quot;), Genus = c(NA, &quot;Dolichospermum&quot;, NA, NA, &quot;Neisseria&quot;, 
  &quot;Bacteroides&quot;, &quot;Haemophilus&quot;, &quot;Bacteroides&quot;, NA, NA), Species = c(NA, 
  NA, NA, NA, NA, NA, &quot;Haemophilusparainfluenzae&quot;, NA, NA, NA), 
      group = c(&quot;Cyanobacteria-NA&quot;, &quot;Cyanobacteria-Nostocaceae&quot;, 
      &quot;Cyanobacteria-NA&quot;, &quot;Cyanobacteria-NA&quot;, &quot;Proteobacteria-Neisseriaceae&quot;, 
      &quot;Bacteroidetes-Bacteroidaceae&quot;, &quot;Proteobacteria-Pasteurellaceae&quot;, 
      &quot;Bacteroidetes-Bacteroidaceae&quot;, &quot;Actinobacteria-ACK-M1&quot;, 
      &quot;Firmicutes-Ruminococcaceae&quot;), group = c(&quot;Cyanobacteria-NA&quot;, 
      &quot;Cyanobacteria-Nostocaceae&quot;, &quot;Cyanobacteria-NA&quot;, &quot;Cyanobacteria-NA&quot;, 
      &quot;Proteobacteria-Neisseriaceae&quot;, &quot;Bacteroidetes-Bacteroidaceae&quot;, 
      &quot;Proteobacteria-Pasteurellaceae&quot;, &quot;Bacteroidetes-Bacteroidaceae&quot;, 
      &quot;Actinobacteria-ACK-M1&quot;, &quot;Firmicutes-Ruminococcaceae&quot;)), row.names = c(406582L, 
  241435L, 406580L, 406574L, 329873L, 300794L, 494797L, 300772L, 
  298689L, 114279L), class = &quot;data.frame&quot;)
  </code></pre>
  <p><strong>Edit 2: We are on the good way</strong></p>
  <p>So, your code seems to work perfectly in term of color but I have some doubts about the values of the bar plot (the percentage for each family).</p>
  <p>I plotted a proportional bar plot of the data with this code:</p>
  <pre><code>GlobalPatterns_prop = transform_sample_counts(GlobalPatterns, function(x)       100 * x/sum(x)) 
  plot_bar(GlobalPatterns_prop , fill = &quot;Phylum&quot;)
  </code></pre>
  <p>and obtained this :</p>
  <p><a href="https://i.stack.imgur.com/bLB4J.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bLB4J.png" alt="GlobalPatterns_proportional_Phylum" /></a></p>
  <p>If I understand well, using your method a majority of phylum and bar height should be &quot;Others&quot;.
  I did the same with my data and I clearly see a difference in Phylum proportional abundance.</p>
  <p>I have for the moment no clue on what is happening...</p>