# Creating 100% stacked bar charts with merge\_samples

**URL:** <https://help.c-moor.org/t/creating-100-stacked-bar-charts-with-merge-samples/565>\
**Category:** Code Cookbook\
**Tags:** R\
**Created:** [July 22, 2025, 1:26am UTC](https://help.c-moor.org/t/creating-100-stacked-bar-charts-with-merge-samples/565 "2025-07-22T01:26:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![syork](https://yyz1.discourse-cdn.com/flex027/user_avatar/help.c-moor.org/syork/32/418_2.png) [@syork](https://help.c-moor.org/u/syork)\
**Post date:** [July 22, 2025, 1:26am UTC](https://help.c-moor.org/t/creating-100-stacked-bar-charts-with-merge-samples/565/1 "2025-07-22T01:26:04Z")

</div>

## How to create a 100% stacked bar chart using data in phyloseq!

- There are some extra steps here because we are working with a categorical variable. It seems that when you use a continuous variable like age there may not be a need to re-add variables (step 3 in the example code below).
- Don’t use the merge object for anything besides this (ex. subsetting). The majority of the metadata (all non numerical data) gets destroyed during the merge.
- Add a n=# on the top of your bars manually in Google Slides or PowerPoint to be clear with your viewers how many samples are in each merged bar.

@gauriipaul

```auto
#Combine all samples with the same OCE2Dosage value into a single sample. #NOTE: We use proportions here so that all samples are weighted equally.
merge <- merge_samples(oc, "OCE2Dosage")

#Turn the merged samples into proportions rather than counts (this corrects for variables that have an unequal sampling effort. ex. more individuals at dosage 30mcg than at dosage 20mcg)
merge <- transform_sample_counts(merge, function(x) 100 * x/sum(x))

#The new merge object does not have OCE2Dosage values. When merging they become the new sample names. Use the sample names to re-add the OCE2Dosage values for plotting
sample_data(merge)$OCE2Dosage = sample_names(merge)

#Plot the merge object
plot_bar(merge, x="OCE2Dosage", fill = "Class", title = "Dosage") +
  geom_bar(aes(color = Class, fill = Class), stat = "identity", position = "stack")

```

 ![Untitled](https://canada1.discourse-cdn.com/flex027/uploads/c_moor/original/1X/d1dd4c5053560770a4861be7f31b4904f7549628.png)
