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

Stathis Kamperis ekamperi at gmail.com
Mon Oct 8 10:19:10 CEST 2012


Salute,

David and Jim,
thank you for your response.

I managed to solve my issue by using levels().

Best regards,
Stathis

On Mon, Oct 8, 2012 at 9:59 AM, Jim Holtman <jholtman at gmail.com> wrote:
> use the 'levels' option on 'factor' to define the explicit order of the factors.  This mightbwork on you data:
>
> df$V1 <- factor(df$V1, levels = unique(df$V1))
>
> Sent from my iPad
>
> On Oct 7, 2012, at 16:40, Stathis Kamperis <ekamperi at gmail.com> wrote:
>
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> 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