I am trying to create a side by side bar graph in ggplot2 sorted numerically by the left bar of the side by side graph. I've tried the reorder function, but that seems to sort by the average of the two bars and not just one of them.
Example side by side bar plot
library(ggplot2)
a<-(c(1:10))
e<-c("group a","group b", "group c", "group d", "group e", "group a","group b", "group c", "group d", "group e")
fillvariable<-c(1,2,2,1,2,2,1,1,2,1)
data<-cbind(a,e,fillvariable)
data<-as.data.frame(data)
data
plot <- ggplot(data, aes(x=e, y=a,fill=factor(fillvariable))) + geom_bar(stat = "identity", position = 'dodge')
plot
I would like to sort the bars numerically by the left (red) bar (see sample bar graph). My real x axis has many groups so it would be less than idea to type each label and set the order that way. Does anyone have a suggestion on how to do this using a function in R?
I'm realizing that I am having another issue too. I'm sure how to make sure my bar plot puts the 1 fill factor on the left every time. Any advice for that would be appreciated as well.