0

This is a very basic problem but I do not find an answer. How can I order my variable "Month" so that when I make a bar plot January is first, then February...?

Thank you so much,

siva974
  • 33
  • 1
  • 6

3 Answers3

5

@PhilipPham's answer is correct: this is equivalent but a little simpler:

Month <- factor(Month,levels=month.name)

since there is a built-in month.name variable in R that gives the English month names in order.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
2
df$Month <- factor(df$Month,format(seq(as.Date("2013-01-01"),by="1 month",length=12),"%B"))

Then, plot again.

ppham27
  • 813
  • 6
  • 10
  • Thank you! In fact my column already contains only the month : "January", "February"... but I a wondering what to write in the barplot script to get January before February... because right now it is in alphabetical order... – siva974 Apr 04 '13 at 21:49
2

Adding to the answer from Ben (since I cannot add comments yet), you can do the following, assuming you have a dataframe with a Month column:

df$Month <- factor(df$Month,levels=month.name)
df = df[order(df$Month,decreasing=FALSE),]
Joben R. Ilagan
  • 1,900
  • 1
  • 15
  • 10