-1

I've created a group bar plot using data from a dataframe I have created to get the percentage changes from baseline. Within each group in the objvariable there is data at 5%,10%,15% and 20%. How to I order the bars so that it goes in that order and not in the order of 10%,15%,20%,5%, that it is in now?

maxprice_Pchange<-(kharif_fv_c_allocation_val[c(6,11,16,21,
                                                7,12,17,22,
                                                8,13,18,23,
                                                9,14,19,24,
                                                10,15,20,25),1] - 181184567630)/181184567630*100
dev_constraint<-rep(c("5%","10%","15%","20%"),5)
obj<-c(rep("Max kharif price",4), rep("Min BWF",4),rep("Min TWF",4),rep("Max b-carotene",4),rep("Max Iron",4))
maxprice_Pchang_df<-data.frame(maxprice_Pchange,dev_constraint,obj)

ggplot(maxprice_Pchang_df, aes(fill=dev_constraint,x= obj, y= maxprice_Pchange)) + 
  geom_bar(position="dodge", stat="identity")+ scale_x_discrete(limits = c("Max kharif price", "Min BWF",
                                                                          "Min TWF", "Max b-carotene","Max Iron"))

Grouped bar chart

Zara Liew
  • 1
  • 3

1 Answers1

0

You can reordered it outside of ggplot by doing:

library(ggplot2)
maxprice_Pchang_df$dev_constraint <- factor(maxprice_Pchang_df$dev_constraint, levels = c("5%","10%","15%","20%"))
ggplot(maxprice_Pchang_df, aes(fill=dev_constraint,x= obj, y= maxprice_Pchange)) + 
  geom_bar(position="dodge", stat="identity")+ 
  scale_x_discrete(limits = c("Max kharif price", "Min BWF", "Min TWF", "Max b-carotene","Max Iron"))
dc37
  • 15,840
  • 4
  • 15
  • 32