简体   繁体   中英

ggplot2 - ordering dodge(d) columns

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")

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM