ggplot2 - x axis levels not in order in plot

I have a DF in which the levels of the column I use as the X axis variable are organized in an order that I want. However, when I plot the data using the following code:

plot.name = ggplot(df, aes(x=type, y=rating, fill=answer)) + 
  geom_bar(position=position_dodge(), stat="identity", width = .5) +
  geom_errorbar(aes(ymin=rating-se, ymax=rating+se),
                width=.2,                    # Width of the error bars
                position=position_dodge(.5)) +
  coord_cartesian(ylim=c(1,5))+
  scale_fill_manual(values = c("#aaadad", "#343837")) +
  theme(legend.position = "bottom")

The plot seems to have the x-axis variable organized in alphebatical order. How can I resolve this? Shouldn't the plotting order reflect the df order?

The problematic variable is 'type'

The default order will be alphabetical if the column type is a character. To change this to the order you can apply this function:

I did not get to fully test this yet, because installing this pack made ggplot2 stop working, so now I have to figure that out...

Ok, after reinstalling ggplot2 - this worked! thank you!

I still don't get why ggplot2 doesn't print the levels in their order, if I set the variable to be a factor... but i'll take the solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.