Home > Back-end >  Grouped boxplots from two columns using ggplot2
Grouped boxplots from two columns using ggplot2

Time:09-27

I would like to group boxplots for data from two columns of my table (az and iz), plotting the data of both columns side by side and grouped by habitat

My dataset looks like this:

habitat az  iz
Gi   2,8    3,0
We   1,3    2,0
Mw  11,3    9,0
Htr  4,0    7,0
Gi   1,4    8,0
Mw   3,3    5,0
Mw  23,6   12,0
Gi   3,8   12,0
Gi   0,0    0,0
We   1,0    5,0
We   0,6    3,0
We   0,9    3,0
Mw  13,1    6,0
Mw  13,4   13,0
Mw  12,7   10,0
Htr 14,1    9,0
Htr 18,1   13,0
Htr 19,8   19,0

I only manage it to plot the data from one column (iz) and I don't see how to add the data from the second column (az)?

data$habitat <- factor(data$habitat , levels=c("We","Gi","Mw","Htr"))
ggplot(data, aes(x=habitat, y=iz, colour=habitat))   
      geom_boxplot()

This gives me the boxplot for the (iz) data

boxplot

I don't know how to add a second boxplot for the az data to the corresponding groups? I found solutions to group boxplots by data of a second column, but I didn't find an example that explained how to add data from a second column in the same group.

CodePudding user response:

library(tidyverse)

df %>% 
  mutate(habitat = factor(habitat , levels=c("We","Gi","Mw","Htr"))) %>% 
  pivot_longer(cols = -habitat) %>% 
  ggplot(aes(x = habitat, y = value, fill = name))   
  geom_boxplot()

enter image description here

Data

structure(list(habitat = c("Gi", "We", "Mw", "Htr", "Gi", "Mw", 
"Mw", "Gi", "Gi", "We", "We", "We", "Mw", "Mw", "Mw", "Htr", 
"Htr", "Htr"), az = c(2.8, 1.3, 11.3, 4, 1.4, 3.3, 23.6, 3.8, 
0, 1, 0.6, 0.9, 13.1, 13.4, 12.7, 14.1, 18.1, 19.8), iz = c(3, 
2, 9, 7, 8, 5, 12, 12, 0, 5, 3, 3, 6, 13, 10, 9, 13, 19)), class = "data.frame", row.names = c(NA, 
-18L))

CodePudding user response:

When trying to reproduce your code I always get the following error:

Error: Problem with `mutate()` column `habitat`.
ℹ `habitat = factor(habitat, levels = c("We", "Gi", "Mw", "Htr"))`.
x object 'habitat' not found

I've tested the following and as result got exactly the same data You used.

> df <- data.frame(data_1$habitat,data_1$iz,data_1$az)
> dput(df)
structure(list(data_1.habitat = structure(c(2L, 1L, 3L, 4L, 2L, 
3L, 3L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("We", 
"Gi", "Mw", "Htr"), class = "factor"), data_1.iz = c(3, 2, 9, 
7, 8, 5, 12, 12, 0, 5, 3, 3, 6, 13, 10, 9, 13, 19), data_1.az = c(2.8, 
1.3, 11.3, 4, 1.4, 3.3, 23.6, 3.8, 0, 1, 0.6, 0.9, 13.1, 13.4, 
12.7, 14.1, 18.1, 19.8)), class = "data.frame", row.names = c(NA, 
-18L))
  • Related