[R] ggplot2: ordering categorial data

Christoph Krammer ck at altaica.de
Mon Oct 15 10:57:29 CEST 2007

I got some further with this issue by myself, using a temporary rank number
to get the bars sortet:

data_rb <- read.csv("c421_recipient_country.txt", header=TRUE, nrows=10)
data_rb$i <- factor(seq(1,10))
levels(data_rb$i) <- data_rb$tld

ggplot(data_rb) + geom_bar(aes(x=i, y=spam), colour="navy", fill="navy") +
geom_bar(aes(x=i, y=stockspam), fill="maroon")

But the problem is that both bars got stacked up. The lower part is blue and
the upper one is red. I did not get how to use position_dodge() to separate

With the following idea, I get an error:

> ggplot(data_rb) + geom_bar(aes(x=i, y=spam), fill="navy",
position="dodge") + geom_bar(aes(x=i, y=stockspam), fill="maroon",
Fehler in as.vector(x, "double") : kann nicht nach Vektor umwandeln

In english: "Error in as.vector(x, "double") : can not convert to vector"

Can anybody help here?

Thanks and regards,

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Christoph Krammer
Sent: Sunday, October 14, 2007 11:30 AM
To: r-help at r-project.org
Subject: [R] ggplot2: ordering categorial data

Hello again,

everytime I think I got something to work, the next issue comes up...

I have the following data.frame, I want to visualize:

> data_rb
   tld  spam1     spam2 share
1   ca 826436     73452 0.0889
2  org 470550     25740 0.0547
3   de 156042     15531 0.0995
4  com 140753      7527 0.0535
5  edu  34845      2507 0.0719
6  net  12781       382 0.0299
7   ru   7648        18 0.0024
8   tw   2290         2 0.0009
9   uk   2283        22 0.0096
10  kr    971         7 0.0072

The perfect version of the final plot would look like this:
TLD is the X axis, ordered by the spam1 values
spam1 and spam2 are on the same Y axis, both as line plots share is on a
second Y axis, again as line plots the three lines should appear in
different colours

I first tried to do a bar plot with the following command:
qplot(tld, spam, data=data_rb, geom="bar", position="dodge") +
geom_bar(aes(y=data_rb$stockspam), position="dodge")

But this seems to stack spam1 and spam2 values.

When I try to do a lineplot, I get the following error message:
> qplot(tld, spam, data=data_rb, geom="line")
Error in unit(x0, default.units) : 'x' and 'units' must have length > 0

Can anyone help me with this?

Thanks and regards,

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list