1

I am trying to create a barplot using ggplot. The barplot is coming fine but the bars are in lexicographical order which don't make sense. A sample data frame is The age column is as follows

 Levels: 0-4 10 11 12 13 14 15 16 17 18 19 20-24 25-29 30-34 35-59 5 6 60+ 7 8 9 Age not stated All ages

And when I plot with ggplot(m2,aes(x=reorder(age),y=value,fill=variable)) + geom_bar(stat = "identity")

I get the plot below

The Age on x axis is lexicographically ordered. So 5,6,7,8,9 are on right of 10,11 etc.

How can this be fixed?

Tinniam V. Ganesh
  • 1,979
  • 6
  • 26
  • 51
  • 1
    This has been asked many times. You can have a look [here](http://stackoverflow.com/questions/27040749/ggplot-plot-x-axis-in-a-specific-order/27040968#27040968) and also check the duplicate links there. – LyzandeR Oct 25 '15 at 12:06

1 Answers1

1

you need to refactor the age variable to the order you want ggplot to plot by

age <- factor(age, levels = c("..1st..","..2nd.."))

where "..1st.." is the first age level/label