inconsistent column order using position dodge with facet_wrap

180 views
Skip to first unread message

James K

unread,
Jan 2, 2010, 10:40:36 AM1/2/10
to ggplot2
I'm comparing the fraction of a user population that uses site
features over time. To do so, I'm rendering bar charts comparing the
uptake of a feature at several time intervals by using
position="dodge". That works great for one chart, but when I try to
use faceting to render several charts, the dodging starts to look a
bit random.

I'd love to find out I'm doing something wrong. I'm using ggplot2
version 0.8.5 on R 2.10.0 (the problem also happened for me on ggplot2
version 0.8.3)

I have some sample code and a small data set to reproduce the problem.

Here's a working chart:

http://drop.io/iop0fnj/asset/consistent-order-png
note the columns are ordered red, green turqoise and purple

And with faceting:

http://drop.io/iop0fnj/asset/inconsistent-order-png

see that the ordering of the colors varies between time points.

I'm using this code: (posted here: http://drop.io/iop0fnj/asset/facet-wrap-issue-r)
data=read.csv("ggplot_example_data.csv");
data$days_on_site=ordered(data$days_on_site);
data_A = subset(data,feature=="A");
ggplot(data_A,aes(x=month,y=fraction,fill=days_on_site)) + geom_bar
(stat="identity",position="dodge") + opts(title="This chart has
consistent ordering");

dev.new();
ggplot(data,aes(x=month,y=fraction,fill=days_on_site)) + geom_bar
(stat="identity",position="dodge") + facet_wrap(~ feature) + opts
(title="This chart does not have consistent ordering");

My data set is posted here: http://drop.io/iop0fnj/asset/ggplot-example-data-csv

Thanks!

Luciano Selzer

unread,
Jan 2, 2010, 1:36:08 PM1/2/10
to James K, ggplot2
Hi James, i had the same issue a while ago. Hadley told me to try to reorder the data frame. It worked for me so I'm telling you the same ;)

data <- data[with(data,order(days_on_site)),]

Luciano


2010/1/2 James K <jkeb...@gmail.com>

--
You received this message because you are subscribed to the ggplot2 mailing list.
To post to this group, send email to ggp...@googlegroups.com
To unsubscribe from this group, send email to
ggplot2+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ggplot2

James Kebinger

unread,
Jan 3, 2010, 2:00:52 PM1/3/10
to Luciano Selzer, ggplot2
Thanks, that does the trick!
Reply all
Reply to author
Forward
0 new messages