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.
#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")