[R] Ordering of facet_wrap() panels

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Wed Aug 15 18:23:42 CEST 2018


1. Unless there is good reason to keep a reply private, always cc the list.
This allows more brains, possible corrections, etc.

2. Have you read ?factor and ?unique ? Always study the docs carefully.
They are generally terse but complete, especially the base docs, and you
can often find your answers there.

3. Your "solution" may work in this case, but if I understand correctly
what you're after,  won't in general. unique() gives the unique values in
the order they appear, which may not be the order you want:

## want ordering to be "a" < "b" < "c"

> f <- rep(letters[3:1],2)

> factor(f, levels = unique(f))
[1] c b a c b a
Levels: c b a  ## not your desired order

Again, please consult the docs and perhaps a tutorial or two as necessary.

-- Bert



On Wed, Aug 15, 2018 at 8:22 AM, Stats Student <stats.student4647 using gmail.com>
wrote:

> Many thanks, Bert.
>
> I did -
>
> facet_wrap(~factor(var, levels=unique (var))
>
> And it seems to be working fine.
> Do you see any issues with this?
>
> I'm fairly new to R so want to make sure I'm not doing something stupid.
>
> Thanks again.
>
> On Wed, Aug 15, 2018, 7:50 AM Bert Gunter <bgunter.4567 using gmail.com> wrote:
>
>> See ?factor.
>>
>> You can either use ?ordered to create an ordered factor to sort the
>> levels as you desire or sort them with factor(). e.g.
>>
>> > f <- factor(letters[3:1])
>> > f
>> [1] c b a
>> Levels: a b c   ## default ordering
>>
>> > f <- factor(f, levels = letters[3:1])
>> > f
>> [1] c b a
>> Levels: c b a  ## explicit ordering
>>
>> Cheers,
>> Bert
>>
>>
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>> On Wed, Aug 15, 2018 at 7:21 AM, Stats Student <
>> stats.student4647 using gmail.com> wrote:
>>
>>> Hi, I am generating multiple charts with facet_wrap() and what what I
>>> see, R/ggplot sorts the panels by the facet variable. So adding an index to
>>> the facet variable (1 - bucket, 2 - bucket, etc) does solve the sorting
>>> issue but it's ugly.
>>>
>>> I also read this post which, if I understand correctly, claims that
>>> ggplot should be using the initial ordering of the data for ordering the
>>> charts (instead of ordering the data itself).
>>>
>>> https://mvuorre.github.io/post/2016/order-ggplot-panel-plots/
>>>
>>> Wondering if anyone knows how to direct ggplot use the initial sorting
>>> of the data to order the panels.
>>>
>>> Thank you.
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>>>
>>
>>

	[[alternative HTML version deleted]]




More information about the R-help mailing list