[R] mosaic row and column order

Duncan Murdoch murdoch at stats.uwo.ca
Thu Nov 1 00:11:54 CET 2007


On 31/10/2007 5:50 PM, Achim Zeileis wrote:
> On Wed, 31 Oct 2007, Michael Kubovy wrote:
> 
>> Dear R-helpers,
>>
>> I wasn't able to find out how to override the alphabetical ordering
>> of the rows and columns in a vcd::mosaic plot. I would like to have
>> them each ordered by numerical values in a different column of the
>> data frame that contains the contingency data.
> 
> mosaic() uses the same ordering as in the levels() of your factor. You
> probably created the factor with an alphabetical ordering (which is the
> default if the input is a character vector), e.g.:
> 
>   ## character vector
>   x <- c("a", "b", "b", "a", "c")
> 
>   ## use alphabetical default
>   f <- factor(x)
>   f

This prints

[1] a b b a c
Levels: a b c

as you'd expect.

> 
>   ## change ordering in existing factor
>   levels(f) <- c("b", "c", "a")
>   f

This prints

[1] b c c b a
Levels: b c a

which might be a surprise if you wanted the order c("a", "b", "b", "a", 
"c").  If you don't want to change the values, use

f <- factor(f, levels=c("b", "c", "a"))
f

which prints

[1] a b b a c
Levels: b c a

Duncan Murdoch
> 
>   ## create from scratch
> 
> 
> 
>> I would be most grateful for a pointer toward the solution.
>>
>> Thanks,
>> MK
>> _____________________________
>> Professor Michael Kubovy
>> University of Virginia
>> Department of Psychology
>> USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
>> Parcels:    Room 102        Gilmer Hall
>>          McCormick Road    Charlottesville, VA 22903
>> Office:    B011    +1-434-982-4729
>> Lab:        B019    +1-434-982-4751
>> Fax:        +1-434-982-4766
>> WWW:    http://www.people.virginia.edu/~mk9y/
>>
>> ______________________________________________
>> 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.
>>
>>
> 
> ______________________________________________
> 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