Home > Net >  How to Boxplot two variables dependent on the same categories in one frame
How to Boxplot two variables dependent on the same categories in one frame

Time:06-01

I have a basic question in making a Boxplot in R. I have a factor table with species richness and species abundance numbers on 4 different plots like this:

plot  sr   abun
A     1    4  
A     2    10
B     3    3
B     4    6
C     4    4
C     1    2
D     3    4
D     2    5

So i made a Boxplot with the species richness and for abundance which worked (yay)

boxplot(sr~Plot,data=factors)
boxplot(abun~Plot,data=factors)

but those are two different plots where at the bottom are the plots (A,B,C,D) and the boxes represent the sr or abun. But I would like to have ONE plot where I have at the bottom the four plots (A,B,C,D) and then side by side the species richness and abundance per plot along the y axis. Is this possible? Thank you for your help!

CodePudding user response:

You'll probably want to use ggplot to achieve what you want:

library(tidyverse)

factors |>
  pivot_longer(-plot) |>
  ggplot(aes(x    = plot,
             y    = value,
             fill = name)
         )  
  geom_boxplot()

enter image description here

  • Related