GiPi
GiPi

Reputation: 1

How can I obtain a grouped boxplot using ggplot2?

I am trying to make a grouped boxplot of the numerical data (Relative Bacterial Abundance) grouped by category (Bacteria), where the variable is 'DPF'. The data.frame (below here) is called 'trial':

data.frame(trial)

enter image description here

However, when I try to make a box plot using the following scripts:

library(ggplot2) ggplot(data = trial, aes(x=DPF, y='Relative Bacterial Abundance')) + geom_boxplot(aes(fill=Bacteria))

I obtain this plot:

enter image description here

What is wrong with what I am doing? I thought the data were already formatted in the right way for ggplot. Is there a problem with the fact that my variable contains numerical data (4,7,10,13,18,23,28)? I cannot find a clue to resolve this. Sorry for the probably stupid question, but I am new to R.

Thank you all very much in advance for your help!

Upvotes: 0

Views: 116

Answers (1)

Marco Torchiano
Marco Torchiano

Reputation: 732

The problem is that you should provide factor or character data to get separate boxes.

Try something like:

library(ggplot2)
ggplot(data = trial, 
       aes(x=as.factor(DPF), y='Relative Bacterial Abundance')) +
   geom_boxplot(aes(fill=Bacteria))

Upvotes: 0

Related Questions