1

I am trying to specify the order of columns in a ggplot2 bar plot where 'dodge' has been applied. The required order of cases is CC (for control) C2, C3 and C4. However, when plotted, these come out as C2, C3, C4, CC (no surprise since 'C' has a higher ASCII value than 4, I suppose).

Is there a way to specify the order of columns when 'dodge' has been applied? I have seen explanations where the order is reversed, but I simply want to specify the order.

The code (so far) is:

GeneExp <- ggplot(genePlot, aes(x=Gene, y=Value, fill=Case) ) +
  geom_bar(stat="identity", position="dodge")
Rorschach
  • 31,301
  • 5
  • 78
  • 129
Charles Brewer
  • 183
  • 2
  • 12

1 Answers1

11

Here is a toy example that shows you how to re-order factors. In the first plot a comes first, in the second, z comes first.

df <- data.frame(b=1:10, c=c("z", "a"))
ggplot(df, aes(x=1, y=b, fill=c)) + 
  geom_bar(stat="identity", position="dodge")

df$c <- factor(df$c, levels=c("z", "a"))
ggplot(df, aes(x=1, y=b, fill=c)) + 
  geom_bar(stat="identity", position="dodge")
BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • Fantastic! Thank you very much. I wasn't able to find anything in any of the documentation (R Graphics Cookbook, ggplot2: elegant graphics..., R in a nutshell are my main refs.) I'll print & include it in my copies. – Charles Brewer Apr 23 '14 at 15:08
  • @CharlesBrewer, to the extent this answers your question, please consider marking it as answered. – BrodieG Apr 23 '14 at 16:31