0

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.

2 Answers 2

1
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))
Sign up to request clarification or add additional context in comments.

2 Comments

Great! Thats what I'm looking for. I see, that I really need to dive deeper in R and tidyverse. Started just reading R4DS.
Glad it helped you! If possible close this question, to help further searches from other users
0

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))

1 Comment

Ok. In my case i need to use data_1.habitat instead of just habitat to get the desired result. Now it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.