TYL
TYL

Reputation: 1637

Boxplot by group and then column in r

How do I make a boxplot such that each group of boxes in the boxplot contains columns of variables from a dataframe.

For example using the mpg dataset:

head(mpg)

# A tibble: 234 x 11
   manufacturer model      displ  year   cyl trans      drv     cty   hwy fl    class  
   <chr>        <chr>      <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr>  
 1 audi         a4           1.8  1999     4 auto(l5)   f        18    29 p     compact
 2 audi         a4           1.8  1999     4 manual(m5) f        21    29 p     compact
 3 audi         a4           2    2008     4 manual(m6) f        20    31 p     compact
 4 audi         a4           2    2008     4 auto(av)   f        21    30 p     compact
 5 audi         a4           2.8  1999     6 auto(l5)   f        16    26 p     compact
 6 audi         a4           2.8  1999     6 manual(m5) f        18    26 p     compact
 7 audi         a4           3.1  2008     6 auto(av)   f        18    27 p     compact
 8 audi         a4 quattro   1.8  1999     4 manual(m5) 4        18    26 p     compact
 9 audi         a4 quattro   1.8  1999     4 auto(l5)   4        16    25 p     compact
10 audi         a4 quattro   2    2008     4 manual(m6) 4        20    28 p     compact
# ... with 224 more rows

So within each cyl group (4,5,6,8), I want to have boxplots for each variable/column cty,hwy, and displ.

Usually, one will set the fill in ggplot to be a factor variable but in this case, I have 3 variables.

It should look something like this:

enter image description here

Upvotes: 0

Views: 1835

Answers (1)

denis
denis

Reputation: 5673

You need to tranform your data to long format on your three variables. Here an example with data.table and melt function, but you will easily find the same with tydr:

library(ggplot2)
library(data.table)

mpg <- setDT(copy(mpg))
mpg_plot <- melt(mpg,measure.vars = c("cty","hwy","displ"),value.name = "val",variable.name = "var")

ggplot(mpg_plot, aes(x = as.factor(cyl),y = val,fill = var))+
  geom_boxplot()+
  theme_light()

enter image description here

Upvotes: 1

Related Questions