Too narrow boxplot when two positions are close to each other (using group parameter)

678 views
Skip to first unread message

Peng Yu

unread,
Mar 8, 2011, 11:11:56 PM3/8/11
to ggplot2
Hi,

The boxes plotted by the following code are very narrow. If I'm not
mistaken, I think that it is because to neighboring x locations are
too close to each other, and the width of the boxes is smaller than
the closest distance between two neighboring positions. I'm wondering
if there is a way to make the boxplot wider, even if there are
overlaps between boxes.

library(ggplot2)
dframe=data.frame(
x=do.call(
c
, lapply(
0:10
, function(i) {rep(i*1000,10)}
)
)
, y=do.call(
c
, lapply(
0:10
, function(i) {rnorm(10,i)}
)
)
)
dframe=rbind(
dframe
, data.frame(
x=rep(-1,10)
, y= rnorm(10,-1)
)
)
qplot(x,y,data=dframe,geom='boxplot', group=round(x))

--
Regards,
Peng

Kohske Takahashi

unread,
Mar 9, 2011, 3:35:59 AM3/9/11
to Peng Yu, ggplot2
Hi,

you can set the width of box like this:

ggplot(dframe, aes(x,y,group=round(x))) + geom_boxplot(width=1)

--
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

> --
> 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
>

Dennis Murphy

unread,
Mar 9, 2011, 4:24:57 AM3/9/11
to Peng Yu, ggplot2
Hi:

Clearly, this is a pathological example:

with(dframe, table(x))
x
   -1     0  1000  2000  3000  4000  5000  6000  7000  8000  9000 10000
   10    10    10    10    10    10    10    10    10    10    10    10

Compare the following:

# (1) May as well use geom_linerange...
ggplot(dframe, aes(x, y)) + geom_point() + geom_boxplot(aes(group = x))
# (2) Still very narrow
ggplot(dframe, aes(x, y)) + geom_point() + geom_boxplot(aes(group = x), width = 100)
Warning message:
position_dodge requires non-overlapping x intervals
# (3) A bit wider...
ggplot(dframe, aes(x, y)) + geom_point() + geom_boxplot(aes(group = x), width = 300)
Warning message:
position_dodge requires non-overlapping x intervals
# (4) Remove the values at x = -1:
ggplot(subset(dframe, x >= 0), aes(x, y)) + geom_point() + geom_boxplot(aes(group = x))

Dennis


Reply all
Reply to author
Forward
0 new messages