[R] ggplot2: ordering categorial data

hadley wickham h.wickham at gmail.com
Tue Oct 16 16:52:38 CEST 2007


Hi Christoph,

There's a couple of things you need to do.  Firstly, you need to
reorder the factor according to how you want the data sorted

df$tld <- reorder_factor(df$tld, tapply(df$spam1, df$tld, mean))

is one way to do that.

The reason that position dodge isn't working for you is because it
only works within a layer, not across layers (I can't see any way to
do this, because of potential heterogeneity of objects across layers).
 To get around this, you need to transform your data into a long form
(what I call molten):

dfm <- melt(df, id="tld")
head(dfm)

You can find out more about melt in the reshape package,
http://had.co.nz/reshape.

It's then easy to create the plot you want:

qplot(tld, value, data = dfm, fill=variable, geom="bar", position="dodge")
or
qplot(tld, value, data = dfm, colour=variable, geom="line", group=variable)

The problem is the radically different scales of the spam and share
variables.  ggplot doesn't support multiple y-axes (and never will)
because of their poor perceptual properties.  I would suggest drawing
a separate graph for share, or using a scatterplot if you want to
investigate the relationship between spam and share:

qplot(spam1, share, data=df, colour="1") + geom_point(aes(x=spam2,
colour="2")) + scale_colour_discrete("spam")

Hope this helps!

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list