This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Making multiple box plots with relative abundance in R

I am creating a relative abundance boxplot comparing two groups (pet, stray) using eight genera. However, the resulting plot displays almost zero values on the y-axis and only one value on the x-axis. I aim to generate a boxplot comparing the relative abundance of eight genera across two groups (pet, stray). How can I modify my code to achieve this?

I suspect that one genus has a significantly different scale of relative abundance, potentially causing the issue with the y-axis. If transforming the y-axis is an effective solution, please suggest appropriate transformations for the relative abundance values with revised code.

my R code is

library(ggplot2)
data <- read.csv("relative abundance raw data (putative pathogen).csv")
p<-ggplot(data, aes(x="Genus", y="Relative_abundance", fill="Group")) +
geom_boxplot(position = position_dodge(width = 0.8), alpha = 0.8) +
labs(title = "Relative Abundance Comparison",
x = "Genus",
y = "Relative Abundance",
fill = "Group") +
theme_minimal() +
scale_fill_manual(values = c("stray" = "blue", "pet" = "red"))
p + geom_jitter(shape=16, position=position_jitter(0.2))

the result figure : enter image description here

my raw data format is : enter image description here

My raw data file can be downloaded here : https://drive.google.com/file/d/1Dxy2EqqgC2BQK6b92gRHSI5t7DtA29YA/view?usp=sharing

Thank you for your assistance !

r boxplots

1 answer

When you pass variables to aes you don't want to quote them.

ggplot(data, aes(x="Genus", y="Relative_abundance", fill="Group"))

should be

ggplot(data, aes(x=Genus, y=Relative_abundance, fill=Group))

Also with such small values you want to greatly reduce your y-axis jitter. Try something like like height=0.001 in geom_jitter first and increase as needed.

enter image description here

Thank you dear !!!

I have one more question, the box doesn't show color as I intended... What should be chaged from the code ?

In scale_fill_manual you have e.g. "stray" but in your data it's "Stray". Just make sure the cases match.

Thank you!! I really appreciate your help.

Log in to answer this question.