i have created a bar chart using ggplot2. However i want to be able to change the order of the bars in a specific order, i.e. not by value or ordered alphabetically as ggplot does automatically. This is my code:
ggplot(data = data1, aes(x = Virus.hpi, y = ct, fill = Virus.hpi)) +
scale_fill_manual(values = c("BTV-8 0hpsi" = "purple", "BTV-8 72hpsi" = "purple", "BTV-1 0hpsi" = "blue", "BTV-1 72hpsi" = "blue")) +
stat_summary(fun = mean, geom = "bar") +
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.3) +
geom_point(data = data1, aes(x = Virus.hpi, y = ct, fill = Virus.hpi)) +
scale_y_continuous(breaks = seq(0, 40, 10), labels = rev(seq(0, 40, 10)))
The is the bar chart I get with this code:
However I want the bars ordered "BTV-8 0hpsi", "BTV-8 72hpsi", "BTV-1 0hpsi", BTV-1 72hpsi"
I have tried to use the reorder
function but i can't seem to get it to work. Below is my code with the reorder
function.
ggplot(data=data1, aes(x = reorder(BTV-8 0hpsi, BTV-8 72hpsi, BTV-1 0hpsi, BTV-1 72hpsi, mean), y=ct, fill=Virus.hpi)) + scale_fill_manual(values = c("BTV-8 0hpsi" = "purple", "BTV-8 72hpsi" = "purple", "BTV-1 0hpsi" = "blue", "BTV-1 72hpsi" = "blue")) + stat_summary(fun = mean, geom = "bar") + stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.3) + geom_point(data=data1, aes(x = Virus.hpi, y=ct, fill=Virus.hpi)) + scale_y_continuous(breaks = seq(0, 40, 10), labels = rev(seq(0, 40, 10)))