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 <stats.student4...@gmail.com> 
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
><bgunter.4...@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.student4...@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.4...@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.student4...@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@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.
>>>>>
>>>>
>>>>
>
>______________________________________________
>R-help@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.

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

______________________________________________
R-help@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.

Reply via email to