Skip to content

ggplot: dodge positions

8 messages · Rui Barradas, Thaler,Thorn,LAUSANNE,Applied Mathematics, John Kane

#
Dear all,

I want to get a series of boxplots (grouped by two factors) and I want to overlay the original observations and the following code does almost what I want:

library(ggplot)
ddf <- data.frame(x=factor(rep(LETTERS[1:4], each=30)), y = runif(120,0,10), grp = factor(rep(rep(1:3, 10), 4)))
ggplot(ddf, aes(x, y, colour=grp)) + geom_boxplot() + geom_point()

Yet the position of the points and the position of the boxes on the x-axis is not the same. I would like that the points are shifted accordingly, such that they line up with the boxplots. I tried position_dodge:

ggplot(ddf, aes(x, y, colour=grp)) + geom_boxplot() + geom_point(aes(ymax=max(y)), position = position_dodge(width=.75))

but that did not really help, as all points are now dodged and I just want to have a fixed offset for each subgroup of points such that the boxplot and the points are aligned. Any ideas?


Kind Regards,

Thorn Thaler
Mathematician

Applied Mathematics 
Nestec Ltd,
Nestl? Research Center
PO Box 44 
CH-1000 Lausanne 26
Phone: +41 21 785 8220
Fax: +41 21 785 9486
#
Hello,

Though I'm not the most fluent user of ggplot, I've seen no problem with 
the graph, each subgroup of points is over each boxplot.

Maybe what made the difference was the use of --->  ggplot2
not ggplot.

Hope this helps,

Rui Barradas

Em 02-07-2012 10:43, Thaler,Thorn,LAUSANNE,Applied Mathematics escreveu:
#
Yes indeed. Sorry for the typo, I just added the library(ggplot) thing afterwards and I did not check for the spelling. So it should read as library(ggplot2) and there the issue is still unsettled.

Thx for pointing that out.

KR,

-Thorn
#
Can you expand a bit on what is wrong with the dodge option?  From what I see it looks lovely witht the points exactly lined with the boxplots for each group but perhaps I don't understand exactly what you want .



John Kane
Kingston ON Canada
____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails
#
I guess it works with ggplot but not with ggplot2. I'm using only the latter but had a typo in my first post. So the code (which does not do what I want) is:

library(ggplot2)
ddf <- data.frame(x=factor(rep(LETTERS[1:4], each=30)), y =
 runif(120,0,10), grp = factor(rep(rep(1:3, 10), 4)))
ggplot(ddf, aes(x, y, colour=grp)) + geom_boxplot() + geom_point()

Thinking of it, I would need to find out which offset ggplot uses to dodge the nested factors. If I knew the exact quantity, I could do something like

geom_point(aes(x = offset.used.by.geom_boxplot))

So how are the exact positions on the x-axis for geom_boxplot determined? Any ideas?

Thanks for the help, anyways.

KR,

-Thorn
#
I don't think I was clear. Sorry.  What I was refering to was the 

ggplot(ddf, aes(x, y, colour=grp)) + geom_boxplot() + geom_point(aes(ymax=max(y)),
position = position_dodge(width=.75))

which is giving me http://www.mediafire.com/i/?fdurpq6e6l8cu35 which was what I though you want.  

I have no idea how the x axis points on the boxplot are  determined.  It may be relatively clear in the code but I don't really have the knowledge to ferret it out.

Sorry that I cannot be of more help.

John Kane
Kingston ON Canada
____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails
#
Unfortunately I can't see your example as the page is blocked by our firewall. Anyways, if I try the dodge code, the points are shifted, yet they are all shifted by another offset. It makes that the green points for instance are indeed closer to the green boxplot, yet they are not aligned meaning that all green plots seem to have a different position on the x-axis, while all the green points for x == "A" should align exactly with "A". Am I clearer now? 

KR,

-Thorn
#
Damn firewalls.
However as I see the graph, the points are lined up exactly on the centreline of each boxplot. So for example the lowest outlier on grp1 boxplot for A is exactlly where it should be at the end of the whisker.  Real outliers for grp1 for D are exactly above the 'non-existant' whisker for that boxplot.  So I looks to me as if in my version of the plot it is what you want.

Any change that I can email directly to you and get an attactment through?
John Kane
Kingston ON Canada
____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails