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). 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.
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 at 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). > > 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
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 at 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 at 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 at 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). >>> >>> 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide R-project.org >>> posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >>> >> >>[[alternative HTML version deleted]]
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 <bgunter.4567 at gmail.com> 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 ><stats.student4647 at 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 at 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 at 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). >>>> >>>> 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>>> stat.ethz.ch/mailman/listinfo/r-help >>>> PLEASE do read the posting guide R-project.org >>>> posting-guide.html >>>> and provide commented, minimal, self-contained, reproducible code. >>>> >>> >>>
On Wed, 15 Aug 2018 07:21:55 -0700 Stats Student <stats.student4647 at 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. >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