I have the following data, and am having some issues with the y axis ordering =
> str(dat)
'data.frame': 3678 obs. of 41 variables:
$ highest_bid : Factor w/ 140 levels "\\N","0","10",..: 2 2 2 2 2 2 2 2 2 2 ...
$ age_of_oldest_driver : Factor w/ 75 levels "18","19","20",..: 66 11 33 24 57 3 17 5 32 22 ...
When I plot highest bid on age, I get the desired plot but the age’s are in order of the factor, which is not what I want. The y axis goes from 180, 19, 200, 2300, 25, 230, 250, etc. Because of values like 19, 25, and 2300, it throws the ordering of the y axis off. See below plot.
ggplot(dat, aes(x=factor(age_of_oldest_driver), y=highest_bid)) +
stat_summary(fun.y="mean", geom="bar")
Now, I generated some sample data to play around with this problem, but I can’t seem to reproduce it. Here’s what I did. Once again, this works perfectly.
df=data.frame(score=c(400,200,3000,500,751,630,554,630,100,250,330,5100,4100,800),
age=c(18,18,23,50,19,39,19,23,22,22,40,35,22,16))
str(df)
ggplot(df, aes(x=factor(age), y=factor(score))) + geom_bar()
library(plyr)
library(ggplot2)
ggplot(ddply(df, .(age), mean), aes(x=factor(age), y=factor(score))) + geom_bar()
Any idea on what I’m doing wrong in the initial code that I published.
Thanks!!
Here’s the wrong image. Notice the y-axis.

You can reorder the original factor (age) before ploting it
EDIT : if it doesn’t work