0

I have tried to plot a standard bar plot using code:

    dat2<-data.frame(Ramp = rep(c("Low","Mid","MidHigh", "High"),each = 2),
             score=rep(c("Average Score", "Top Score"), 2),
             score.1=c(23.89,23.89,7.31,2.54,10.18,8.70,
                      3.17,2.00))

    plot2<- ggplot(data=dat2,aes(x=score,  y=score.1, fill = Ramp)) + 
      geom_bar(stat="identity", position = position_dodge(), 
               width = .8) +
      theme_classic() 

    colour2<- scale_fill_grey(limits = c("Low","Mid","MidHigh","High"))

    plot2 + colour2                  

This results in a plot in which the legend is in the correct order, but the factors in the x-axis are not in the correct order. How do I change it to match the order of the legend?

The plot currently looks like:

enter image description here

h3rm4n
  • 4,126
  • 15
  • 21
  • 1
    Possible duplicate of [Order Bars in ggplot2 bar graph](http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph) – Roman Apr 21 '17 at 14:44

1 Answers1

0

Change the Ramp variable to a factor variable, specifying the levels.

RampF <- factor(c("Low","Mid","MidHigh", "High"), levels = c("Low","Mid","MidHigh", "High"))

dat2<-data.frame(Ramp = rep(RampF,each = 2),
                 score=rep(c("Average Score", "Top Score"), 2),
                 score.1=c(23.89,23.89,7.31,2.54,10.18,8.70,
                           3.17,2.00))

plot2<- ggplot(data=dat2,aes(x=score,  y=score.1, fill = Ramp)) + 
    geom_bar(stat="identity", position = position_dodge(), 
             width = .8) +
    theme_classic() 

colour2<- scale_fill_grey(limits = c("Low","Mid","MidHigh","High"))

plot2 + colour2  
Jeremy Voisey
  • 1,257
  • 9
  • 13