Ordering of facet_wrap() panels

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

Ordering of facet_wrap() panels

stats.student4647
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.

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

Re: Ordering of facet_wrap() panels

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

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

Re: Ordering of facet_wrap() panels

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

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

Re: Ordering of facet_wrap() panels

stats.student4647
Understood. Will review the docs again.

My data is from an external source which, among other things, ensures that it's sorted correctly. I was asking for a way to have ggplot use the ordering in place, instead of re-ordering everything. Apologies if it wasn't clear from the original post.

Anyway, if the data is correctly presorted, unique should work ok, I think.




On Aug 15, 2018, 9:23 AM, at 9:23 AM, Bert Gunter <[hidden email]> wrote:

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

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

Re: Ordering of facet_wrap() panels

JohnDee
In reply to this post by stats.student4647
On Wed, 15 Aug 2018 07:21:55 -0700
Stats Student <[hidden email]> 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.
>
You should also be looking at the ggplot help mailing list.  Since you
did not supply an example of your code it is not possible to really see
if anything in it might be overriding ggplot's normal behavior.

JDougherty

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

Re: Ordering of facet_wrap() panels

Jeff Newmiller
In reply to this post by stats.student4647
The result does NOT depend on whether or how the data itself are sorted, but rather on how the levels of the factors in the data are sorted. Best results will be obtained if you modify the data frame factors before giving the data frame to ggplot. Doing so will allow all of the ggplot functions to work on a consistent set of data columns.

Some example ways to change the ordering:

factor(f, levels = sort(unique(f))) #alpha
factor(f, levels = unique(f)) #as first encountered in data
factor(f, levels = c("b","a","c")) #explicit
factor(f, levels = c("b","a","c"), labels=c("Ok","Best","Other") "#presentation coding

If for some reason you don't want to modify your original data, make a temporary copy of your data frame and set the appropriate factor levels for your presentation. However, it will then be up to you to avoid making substantive changes to the data records between multiple plots (views of the data).

On August 15, 2018 12:18:12 PM PDT, Stats Student <[hidden email]> wrote:

>Understood. Will review the docs again.
>
>My data is from an external source which, among other things, ensures
>that it's sorted correctly. I was asking for a way to have ggplot use
>the ordering in place, instead of re-ordering everything. Apologies if
>it wasn't clear from the original post.
>
>Anyway, if the data is correctly presorted, unique should work ok, I
>think.
>
>
>
>
>On Aug 15, 2018, 9:23 AM, at 9:23 AM, Bert Gunter
><[hidden email]> wrote:
>>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
>><[hidden email]>
>>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 <[hidden email]>
>>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 <
>>>> [hidden email]> 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.
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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.
>>>>>
>>>>
>>>>
>
>______________________________________________
>[hidden email] 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.

--
Sent from my phone. Please excuse my brevity.

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