I found the answer to my problem. I had to change the data type of x
to "ordered" as follows:
x = ordered(x,levels=c("first","second","last").
Qplot respects this order, so my problem is solved!
Greetz,
Adi
On 9/9/10, Adi <bhagwa...@gmail.com> wrote:
> Dear all,
>
> SITUATION
> x: categorical variable, values from {first, second, last}
> y: quantitative variable.
>
> QUESTION
> qplot(x,y,data=myData) -> plots results in alphabetical order for x:
> "first", "last", "second"
> But I do not want this alphabetical ordering. I want the results to be
> plotted in the order "first", "second", "last"
> How to do this?
>
> Thanks for your help!
>
> Adi
--
Aditya Bhagwat
Ph.D student at KERMIT research group
Ghent University (Belgium)
Dept. Appl. Math., Biometrics and Process Control
Currently Erasmus Mundus visiting Ph.D fellow
University of Pune (India)
Center for Modeling and Simulation
Ggplot does not change the order of your data. It uses the order from
the levels in your factor. Have a look at the examples below. Note that
the ordering in the second example comes from factor() and not from
ggplot.
library(ggplot2)
dataset <- data.frame(ABC = LETTERS[3:1], DEF = rnorm(3))
ggplot(dataset, aes(x = ABC, y = DEF)) + geom_point()
ggplot(dataset, aes(x = factor(ABC), y = DEF)) + geom_point()
ggplot(dataset, aes(x = factor(ABC, levels = c("B", "A", "C")), y =
DEF)) + geom_point()
HTH,
Thierry
------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium
Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry....@inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
> -----Oorspronkelijk bericht-----
> Van: ggp...@googlegroups.com
> [mailto:ggp...@googlegroups.com] Namens Aditya Bhagwat
> Verzonden: donderdag 9 september 2010 10:53
> Aan: ggplot2
> Onderwerp: Re: I do not want qplot to sort categorical x variable
> --
> You received this message because you are subscribed to the
> ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.
On Thu, Sep 9, 2010 at 5:39 AM, Brandon Hurr <bhi...@gmail.com> wrote:
> I should add something. reordering the levels does reorder the legend, but
> then the colors don't reorder with the factors.
Sure they do... at least it works for me.
> You needed to use the breaks in order to do that.
No
> Forgive me for saying this, but that seems counterintuitive. If you reorder
> factor levels, why shouldn't the colors follow suit?
They do! Maybe you can provide an example that illustrates the issue
you are trying to describe.
Best,
Ista
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
> test <- factor(letters[1:10])
> test2 <- test
> levels(test2) <- rev(levels(test2))
> test3 <- factor(test, levels = rev(levels(test)))
>
> test
[1] a b c d e f g h i j
Levels: a b c d e f g h i j
> test2
[1] j i h g f e d c b a
Levels: j i h g f e d c b a
> test3
[1] a b c d e f g h i j
Levels: j i h g f e d c b a
It looks like levels() changes the actual values, rather than just the
order of the levels of a factor. So, back to your example: This works
for me as I would expect:
require(ggplot2)
data(diamonds)
print(qplot(cut, data=diamonds, geom="bar",fill=color))
# reverse levels of color using factor() instead of levels()
diamonds$color<-factor(diamonds$color, levels=rev(levels(diamonds$color)))
levels(diamonds$color)
qplot(cut, data=diamonds, geom="bar",fill=color)
Best,
Ista
On Thu, Sep 9, 2010 at 10:56 AM, Brandon Hurr <bhi...@gmail.com> wrote:
> Ista,
> The graph you made isn't as messed up as the one I made using levels(rev()),
> but it still isn't really what the original poster wanted.
I assume you are talking about the OP of this thread:
http://groups.google.com/group/ggplot2/browse_thread/thread/5e4cb9d6d3b36b2f/31f69c8fc32f2558
rather than the current one.
> The color names (D,E,F) are stacked in the right order (top to bottom), but
> the bars on the graph are stacked in reverse order. Look at the colors and
> letters. On the first graph D is "salmon" and E is "mustard," and they are
> on the bottom. On the second graph, D is "pink" and E is "lavendar" and they
> are at the top.
> It seems that it plots the bars in the order of the factor (bottom to top),
> but then when it plots the legend it puts them from top to bottom.
Right. So the _only_ issue I see is that some people would like to
have legends ordered from bottom to top instead of top to bottom. I'm
not saying I disagree with that, I'm just trying to clear away the
noise about "reordering factors doesn't work for color scales" or
"doesn't work for plot colors". Those are non-issues, because it does
work, provided that you reorder the levels of your factors correctly.
Furthermore, we have already established that you can change the order
in which the legend is printed using the breaks argument to your color
scale. So this all boils down to "hey Hadley, can you change the
default legend order so that it is ordered from bottom to top instead
of top to bottom?". I think that might make some sense, but then again
I can see arguments against it. I think most people read legends
starting at the top and visually scanning down, so ordering from the
top down also makes sense. Further, if you dodge the bars instead of
stacking them then the default ordering makes more sense: try
qplot(cut, data=diamonds, geom="bar",fill=color, position="dodge")
and notice that the legend colors read from top to bottom match the
plot colors from left to right. Since left-right top-bottom is a
natural order in which western people tend to scan a page, that makes
a lot of sense to me. So now maybe we ask "hey Hadley, can you change
the default legend order so that it is ordered from bottom to top
instead of top to bottom, but only if position = 'stacked' ?".
Personally I'm fine with manually changing the order using the breaks
argument if I need to.
Sorry this turned into a rant I'm afraid...
Best wishes all,
Ista
> Is this only an issue with stacked barplots?
> Brandon
> P.S. It seems I may have had my levels() reversed when I started saving the
> graphs so my original in the last email is the screwed up one and the
> reversed one is the right one to start with. :/
>