r - regroup elements from the column in drawing boxplots -


i want ask dataframe below, how combine groupa, , bs groupb first column, , draw 2 boxplots on figure data?

the dataframe this:

sample  product1    product2    product3    product4    product5 group a1    1   1   2   0.1 0.2 group a1    3   3   4   0.2 0.5 group a1    2   5   6   0.7 0.6 group a1    7   6   7   0.8 0.7 group a2    6   2   1   0.5 0.2 group a2    7   4   2   0.4 0.4 group a3    8   7   2   0.3 0.5 group a3    9   8   6   0.2 0.3 group a3    5   2   6   0.2 0.2 group a3    6   3   7   0.1 0.1 group b1    6   5   8   0.6 0.9 group b1    2   7   9   0.7 0.7 group b1    3   9   4   0.9 0.8 group b1    1   2   5   0.2 0.6 group b2    5   2   2   0.3 0.5 group b2    7   3   4   0.5 0.5 group b2    8   1   3   0.5 0.4 group b2    9   1   3   0.7 0.3 

the code draws each sign in first column is, don't know how combine. help.

 pproduct1 = ggplot(data=df,aes(x=factor(sample),y=product1))+                 geom_boxplot()+                 xlab('')+                 ylab('unit(mg)') show(pproduct1) 

you can use regex match rows , b

ggplot(dat, aes(x=factor(grepl("group a", sample), labels=c("a", "b")), y=product1)) +                 geom_boxplot()+                 xlab('')+                 ylab('unit(mg)') 

enter image description here

or, more general version multiple groups suggested @ulfelder

ggplot(dat, aes(x=factor(gsub("group ([a-z]+).*", "\\1", dat$sample)), y=product1)) +                 geom_boxplot()+                 xlab('')+                 ylab('unit(mg)') 

Comments