[R] Ordering of facet_wrap() panels

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Wed Aug 15 16:50:49 CEST 2018


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