ggplot: order of numeric factor levels?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

ggplot: order of numeric factor levels?

Marianne Promberger
Hi,

I'm having problems with qplot and the order of numeric factor levels.

Factors with numeric levels show up in the order in which they appear
in the data, not in the order of the levels (as far as I understand
factors!)

Here is a minimal example:

library(ggplot2)
y <- c(-1,2,0,0,-2,-1)
z <- factor(y,levels=c(-2,-1,0,1,2))
qplot(z)

For me, the resulting plot is ordered: -1,2,0,-2

By contrast,
plot(z) is neatly ordered -2,-1,0,1,2

What am I not getting?

Thanks for any pointers,

Marianne

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot: order of numeric factor levels?

hadley wickham
On Tue, Mar 31, 2009 at 5:01 PM, Marianne Promberger
<[hidden email]> wrote:

> Hi,
>
> I'm having problems with qplot and the order of numeric factor levels.
>
> Factors with numeric levels show up in the order in which they appear
> in the data, not in the order of the levels (as far as I understand
> factors!)
>
> Here is a minimal example:
>
> library(ggplot2)
> y <- c(-1,2,0,0,-2,-1)
> z <- factor(y,levels=c(-2,-1,0,1,2))
> qplot(z)
>
> For me, the resulting plot is ordered: -1,2,0,-2
>
> By contrast,
> plot(z) is neatly ordered -2,-1,0,1,2
>
> What am I not getting?

It's a bug in the current version.  You can fix it by explicitly
setting the limits for the x axis - + xlim("-2","-1","0","1","2")).
It will be fixed in the next release which I'm trying to get out soon.

Hadley


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

______________________________________________
[hidden email] 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.