Several boxes grouped by two conditions

I have real problems with creating multiple boxes in one plot ... I have five variables, three are numerical, and two are Factor. I want boxplot for all three numeric variables, but grouped by two factor variables ... The plot has two groups for Low and High with three cells for MM, ND and BB. Legends should contain abbreviations for MM, ND and BB.

Group Class Sal Wal Daa MM Low 21 34 4 ND Low 23 65 3 BB High 21 34 2 MM High 25 23 4 MM High 23 23 5 MM High 13 54 6 MM High 56 32 4 MM Low 34 13 3 ND Low 12 35 7 ND Low 34 34 2 ND Low 54 54 1 ND High 32 34 6 ND High 43 32 7 BB Low 54 13 3 BB Low 12 56 2 BB Low 45 34 6 BB High 32 32 3 BB High 13 12 2 BB High 54 12 5 
+4
source share
2 answers

If you want to have a separate group of boxes for each numerical variable, you can use the interaction() function to group the variables into groups and classes:

 test.data <- data.frame(Sal=rnorm(100), group=factor(sample(LETTERS[1:3], 100, replace=TRUE)), class=factor(sample(c("low","high"), 100, replace=TRUE))) boxplot(Sal ~ interaction(group,class), data=test.data) 
+6
source

First you need to change your data:

 dta <- read.table(text="Group Class Sal Wal Daa MM Low 21 34 4 ND Low 23 65 3 BB High 21 34 2 MM High 25 23 4 MM High 23 23 5 MM High 13 54 6 MM High 56 32 4 MM Low 34 13 3 ND Low 12 35 7 ND Low 34 34 2 ND Low 54 54 1 ND High 32 34 6 ND High 43 32 7 BB Low 54 13 3 BB Low 12 56 2 BB Low 45 34 6 BB High 32 32 3 BB High 13 12 2 BB High 54 12 5", header=TRUE) dtaLong <- stack(dta, select=cbind(Sal, Wal, Daa)) dtaLong <- data.frame(dtaLong, dta[,1:2]) 

Now ind is a new factor that determines the values โ€‹โ€‹of the three variables in the original dataset.

 library(lattice) bwplot(values~ind | Group + Class, data=dtaLong) 
+2
source

Source: https://habr.com/ru/post/1442784/


All Articles