Split boxplot into 2

Hi, I am making a boxplot on insect species.
The only thing that does not work for me at the moment, is splitting the plot into terrestrial and aquatic species.
The dataset that I use for making the graph consists of the following columns: taxon_order, value, variable and area.
The area column contains information on whether the insect is terrestrial (Coleoptera, Hymenoptera and Lepidoptera) or aquatic (Ephmeroptera, Odonata, Plecoptera). I would like to display this information on the x-axis as well, but do not know and cannot find how.

This is my current graph:

and this is my code:

ggplot(fig1_combined, aes(x=taxon_order, y=value, fill=variable))+ 
  geom_boxplot(alpha =1)+ 
  labs(x = "Taxon order", y ="Percentage (%)")+ 
  scale_fill_manual(values = fillcolors, name="test", breaks = fill_variable, labels = grouplabels) + 
  guides(fill=guide_legend(title = NULL))+
  theme(text = element_text(size = 16))+ 
  theme_classic()

Can someone help me?
Thank you!

Would you be able to provide some example data?

My suggestion would be to create a type column that lets you know if the insect is terrestial or aquatic using mutate() (Create, modify, and delete columns — mutate • dplyr) and then have it show up in your x-axis using facet_grid() (Lay out panels in a grid — facet_grid • ggplot2).

I have made an example dataset and graph

taxon_order <- rep(c( "Coleoptera", "Hymenoptera", "Odonata", "Plecoptera"), times = 5)
area <- rep(c("Terrestrial", "Terrestrial", "Aquatic", "Aquatic"), times = 5)
value <- rep(seq(from=1, to = 10, by = 1), times = 2)
variable <- rep(c("Declining", "Vulnerable"), times =10)
variable <- sample(variable)


df <- data.frame(taxon_order, area, value, variable)

ggplot(df, aes(x=taxon_order, y=value, fill=variable))+
  geom_boxplot()

The column area of this example data indicates where the taxon_order lives

Coleoptera and Hymenoptera are terrestrial
Odonata and Plecoptera are aquatic

Thanks! How is this?

taxon_order <- rep(c( "Coleoptera", "Hymenoptera", "Odonata", "Plecoptera"), times = 5)
area <- rep(c("Terrestrial", "Terrestrial", "Aquatic", "Aquatic"), times = 5)
value <- rep(seq(from=1, to = 10, by = 1), times = 2)
variable <- rep(c("Declining", "Vulnerable"), times =10)
variable <- sample(variable)

df <- data.frame(taxon_order, area, value, variable)

ggplot(df, aes(x = taxon_order, y = value, fill = variable)) +
  geom_boxplot() +
  facet_grid( ~ area, scales = "free", switch = "x")

Rplot

1 Like

It worked, thank you!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.