0

Possible Duplicate:
Order Bars in ggplot2 bar graph

I edited this considerably after closed. (Thank you Andrie for the advice.)

(1) Problem

The order of the labels are autumn, spring, summer, winter. How do you make them ordered as spring, summer, autumn, winter?

Problem Code:

ssn <- c("spring", "summer", "autumn", "winter")
cnt <- c(2,4,3,7)
d <- data.frame(season=ssn, count=cnt)
library(ggplot2)
ggplot() + geom_bar(data=d,aes(x=season, y=count))

(2) Solution

Solution Code:

ssn <- c("spring", "summer", "autumn", "winter")
ssn <- factor(ssn, level=ssn)
cnt <- c(2,4,3,7)
d <- data.frame(season=ssn, count=cnt)
library(ggplot2)
ggplot() + geom_bar(data=d,aes(x=season, y=count)) 

The 2nd line does the trick.

Community
  • 1
  • 1
weis26
  • 391
  • 5
  • 16
  • 1
    Duplicate of: http://stackoverflow.com/q/6738571/602276, http://stackoverflow.com/q/5208679/602276 – Andrie Nov 08 '11 at 13:52
  • Thank you for the links, Andrie. However, since I still cannot make it work, I'd appreciate any suggestion. – weis26 Nov 08 '11 at 14:18
  • 2
    You need to have your x variable as a factor, with the order of the labels being the order that you desire. If you *really* have tried the advice in the other answers, then rephrase your question as a **minimally reproducible** example, showing what you have tried. – Andrie Nov 08 '11 at 14:20
  • Andrie, thank you for the further imput. Yesterday, after reading the suggested posts, I tried putting factor() around the right side of the 1st line. That didn't work, and I also tried again now without success. I understand that I should ask a question with the simplest possible example, so that other people and I can later generalize to their own problem. I have asked 2 questions prior to this one (2nd and 3rd reference) with this in mind. However, this time I have no idea what part is failing, so I have posted the whole problem. I'd stiil appreciate further assistance. Thank you again. – weis26 Nov 09 '11 at 04:15
  • Your data needs to be a factor, not your labels. The point of a minimal example is that you reduce the problem to the simplest form in which the problem still persists. Try making a very simple plot and reorder the labels. Then step up to something a bit more complicated. Rinse and repeat. – Andrie Nov 09 '11 at 07:47
  • I could sort this out. Thank you very much for all the advice Andrie. – weis26 Nov 09 '11 at 11:49

0 Answers0