Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot boxplot for multiple columns with a factor

Tags:

r

ggplot2

I am new to ggplot2, I want to plot boxplot of multiple numerical columns in the same plot, and group the boxplot by a factor from the last column the header of the dataframe is like:

id var1 var2 var3 factor

I know I can do:

ggplot(df,aes(x=factor,y=var1)+geom_boxplot()

For each variable.

How can I plot them together with ggplot2? The base graphics utility boxplot(df[,-c(id,factor)] will plot them together.

like image 761
crazyhottommy Avatar asked May 07 '26 20:05

crazyhottommy


1 Answers

Following works "to plot boxplot of multiple numerical columns in the same plot, and group the boxplot by a factor":

mm = melt(df, id=c('id','factor.col'))
ggplot(mm)+geom_boxplot(aes(x=paste(variable,factor.col,sep="_"), y=value))

enter image description here

Data:

> df
   id          var1       var2       var3 factor.col
1   1  0.0562941632  1.3055892 -1.7553986          c
2   2  0.3187487914 -0.6434729  0.7582403          a
3   3 -1.0120881740  0.3898366  0.4790115          c
4   4 -0.7576871261  0.9474247  0.2180341          b
5   5 -0.2895981608  0.2388043 -0.4407693          a
6   6  0.0005306311  0.9494667 -0.2604795          c
7   7  0.2022893934 -0.3095420 -1.9483369          c
8   8 -0.8179580833 -0.7891642  0.7662824          b
9   9 -0.7553554525  0.2734337 -0.7145471          b
10 10  0.4206429078 -0.6171774  0.6274320          c
> 

Following can also be used:

ggplot(mm)+geom_boxplot(aes(x=variable, y=value))+facet_grid(.~factor.col)

enter image description here

like image 188
rnso Avatar answered May 09 '26 11:05

rnso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!