[R] Reshape2, melt, order of categorical variable and ggplot2

Stathis Kamperis ekamperi at gmail.com
Sun Oct 7 22:40:28 CEST 2012


Hello everyone,

I have the following data frame:

> df
           V1     V2      V3
1   bench1_10  16675   16678
2   bench1_10  16585   16672
3  bench1_100 183924  185563
4  bench1_100 169310  184806
5  bench1_300 514430  516834
6  bench1_300 510743  514062
7  bench1_500 880146  877882
8  bench1_500 880146  857359
9   bench1_1k 880146 1589600
10  bench1_1k 880146 1709990
>

I'd like to melt it by using id=V1:

> mdf <- melt(df)
Using V1 as id variables
> mdf
           V1 variable   value
1   bench1_10       V2   16675
2   bench1_10       V2   16585
3  bench1_100       V2  183924
4  bench1_100       V2  169310
5  bench1_300       V2  514430
6  bench1_300       V2  510743
7  bench1_500       V2  880146
8  bench1_500       V2  880146
9   bench1_1k       V2  880146
10  bench1_1k       V2  880146
11  bench1_10       V3   16678
12  bench1_10       V3   16672
13 bench1_100       V3  185563
14 bench1_100       V3  184806
15 bench1_300       V3  516834
16 bench1_300       V3  514062
17 bench1_500       V3  877882
18 bench1_500       V3  857359
19  bench1_1k       V3 1589600
20  bench1_1k       V3 1709990
>

My problem is that V1 in mdf has bench1_1k before bench1_500 and so
on. This is a problem because when I try to plot the results with:

p <- ggplot(mdf, aes(x=mdf$V1, y=mdf$value))
p <- p + geom_point(aes(colour=factor(mdf$variable)))
p

I get bench1_1k in the horizontal axis before say bench1_500.

Is there any way to have V1 in mdf "sorted" just like in df ? Or do I
have to rename my files from bench1_1k to bench1_1000 and be done ?

Best regards,
Stathis



More information about the R-help mailing list