0

This is my code:

col1<-c(rep("first",2), rep("second",2), rep("third",2), rep("fourth",2), rep("fifth",2), rep("sixth",2), rep("seventh",2), rep("eighth",2))
col2<-gl(2,1,8, labels=c("one","two"))
col3<-Values
d1 <- data.frame(column1=col1, column2=col2, column3=col3)
ggplot(d, aes(x=column1, y=column3, fill=column2)) + geom_bar(position=position_dodge())

The bars on my plot are in alphabetical order, but I need them to be in the order as in col1. How can I do this?

user2080209
  • 749
  • 3
  • 8
  • 25

1 Answers1

1

substitute:

d1 <- data.frame(column1=col1, column2=col2, column3=col3)

for:

d1 <- data.frame(column1=factor(col1, levels=unique(col1)), column2=col2, column3=col3)

the factor(col1, levels=unique(col1)) akes a factor of col1 and the levels= allows you to pick the order of the levels which will then be subsequently used by ggplot to define the order of your bars.

user1317221_G
  • 15,087
  • 3
  • 52
  • 78