Barplot with groups and subgroups

587 Views Asked by At

I need to make a boxplot with groups and variables using the data below :

df<-as.data.frame(cbind(c(1,2,3),
                        c(0.4,-0.11,-0.07),
                        c(0.31,0.07,0),
                        c(0.45,-0.23,0.02)))
names(df)<-c('cat','var1','var2','var3')

I need to make a barplot with the cat1 on the abscissa and the measurements of each variables on the ordinate.

For example concerning the cat=1, I need in the abscissa the number of cat1 with 3 barplots representing the value of (var1,..var3).

1

There are 1 best solutions below

0
Adam Quek On BEST ANSWER
library(tidyverse)
df <- df %>% 
     gather(var, val, -cat)

ggplot(df, aes(cat, val, fill=var)) + 
    geom_col(position="dodge")

enter image description here