geom_boxplot box width & alignment

3,305 views
Skip to first unread message

Jokel Meyer

unread,
Dec 5, 2010, 7:44:59 PM12/5/10
to ggplot2
Dear ggplot2 experts,

I am new to this package and would have a question that I failed to
solve now for quiet some time:

I am plotting a boxplot (continues variable on y-axis) and the boxes
group by two grouping variables (one with 2 levels, one with 3
levels). Using geom_boxplot brings me quiet nice results. However
boxes are aligned in a way that they are directly next to each other.
How could I control the box alignment in a way that there is a little
gap between the boxes?

Also I would like to add a little star which would indicate
significant differences between groups as it is sometimes used in
boxplots. Which would be the easiest way to get this?


Many thanks for your help!
Jokel

James Howison

unread,
Dec 5, 2010, 8:18:02 PM12/5/10
to ggplot2
Minimal example please. It's just much easier to help if there is code to copy into R.

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Jokel Meyer

unread,
Dec 6, 2010, 5:28:45 PM12/6/10
to ggp...@googlegroups.com
Dear ggplot2 experts,

I am new to this package and would have a question that I failed to
solve now for quiet some time:

I am plotting a boxplot (continues variable on y-axis) and the boxes
group by two grouping variables (one with 2 levels, one with 3
levels). Using geom_boxplot brings me quiet nice results. However
boxes are aligned in a way that they are directly next to each other.
How could I control the box alignment in a way that there is a little
gap between the boxes?

R-code:

library(ggplot2)

ass<-structure(list(assort = c(0.1994, 0.3696, 0.4056, 0.3933, 0.4322, 

0.4192, 0.1923, 0.3957, 0.2347, 0.3576, 0.2381, 0.143, 0.2908
), task = c(0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1), diag = c(0, 
0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2)), .Names = c("assort", "task", 
"diag"), class = "data.frame", row.names = c(NA, -13L))

p<-ggplot(ass, aes(factor(diag),assort)) 

p + geom_boxplot(aes(fill = factor(task)))

Kohske Takahashi

unread,
Dec 6, 2010, 6:59:55 PM12/6/10
to Jokel Meyer, ggp...@googlegroups.com
Hi, here is an example:

ggplot(ass, aes(factor(diag),assort)) +
geom_boxplot(aes(fill = factor(task)), position=position_dodge(width=0.9)) +
geom_text(aes(x,y,label=v), data.frame(x=factor(0:2),
y=rep(0.45,3),v=c("*","","*")))

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Reply all
Reply to author
Forward
0 new messages