Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing 2D boxplots with R (ggplot)

Tags:

r

ggplot2

I have grouped data with, say, 2 parameters:

set.seed(1)
dat <- data.frame( xx=sample(10,9),
                   yy=sample(20,9),
                   group=c('A','B', 'C')  )

I can draw boxplots for each dimension:

ggplot(dat, aes(x=group, y=yy, fill=group)) + geom_boxplot() 
ggplot(dat, aes(x=group, y=xx, fill=group)) + geom_boxplot() + coord_flip()

Now I would to combine these and draw boxplots reflecting data on both variables, something like this: (this image was made manually in graphic editor)

Is there any easy way to draw this kind of boxplots?

like image 476
Vasily A Avatar asked Nov 09 '22 14:11

Vasily A


1 Answers

That is not good way of representation. It can be very confusing. Usually that data is represented like this.

datPlot <- melt(dat)
ggplot(datPlot, aes(x=group, y=value, fill=variable)) + geom_boxplot() 

enter image description here

or if the range of two variables is very different you can try faceting

ggplot(datPlot, aes(x=group, y=value, fill=group)) + geom_boxplot()
 +facet_wrap(~variable,scale="free_y")

enter image description here

or this

ggplot(dat, aes(x=xx, y=yy, color=group)) + geom_point(size=3) 

enter image description here

like image 100
Koundy Avatar answered Nov 15 '22 05:11

Koundy