I'm trying to create a bar chart based on the data below. There are two independent variables, namely Last Win (factors: Rewarded, Unrewarded) and Last Transition (Common, Rare)
#make data and put into table
library(ggplot2)
values_MB <- matrix (c(0.75,0.4, 0.4, 0.75),ncol = 1)
lastWin <- matrix (c("Rewarded","Rewarded","Unrewarded","Unrewarded"),ncol = 1)
lastTransition <- matrix (c("Common","Rare","Common","Rare"),ncol = 1)
MB_table <- cbind(lastWin, lastTransition,values_MB)
colnames(MB_table) <- c('lastWin', 'lastTransition', 'meanStay')
MB_table <- as.data.frame(MB_table)
## plot data ##
ggplot(MB_table, aes(x = lastWin, fill = lastTransition, y = meanStay)) +
geom_bar(colour = "black", stat = "identity", position = position_dodge(width = .9)) +
coord_cartesian(ylim = c(.5, 2)) +
ylab("p(First-Stage Stays)") +
xlab("Previous Reward") +
ggtitle("Model-Free")+
theme(text = element_text(size=15))+
scale_fill_manual(values = c("#00AFBB", "#E7B800"), name = "Transition Type", labels = c("Common", "Rare")) +
theme ( text = element_text (family = "sans", color = "grey20"),
panel.background = element_rect(fill = "white"),
axis.line = element_line(linetype = "solid"))
Ideally I would like the 'Common' bars to always be first, followed by Rare bars, at each level of lastWin but this isn't happening (see image below). I'm wondering why this is as I'd have thought ggplot2 would have ordered the bars alphabetically? Is there any way I can rectify this? Thank you!


