I am trying to order the bars in descending order according to the frequency (y axis) of the blue area (1 in the legend).
Here is a dummy data and my attempt.
Thank you for you help!
library(ggplot2)
Region <- c('A','B','C', 'D', 'E')
Factor <- c(1, 0)
Freq <- c(0.6, 0.2, 0.5, 0.3, 0.5, 0.4, 0.8, 0.5, 0.7, 0.5)
Value <- c(100, 250, 30, 300, 120, 50, 400, 90, 150, 320)
d <- data.frame(Region, Factor, Freq, Value)
d$Factor <- factor(d$Factor)
ggplot(d, aes(x= reorder(Region, -Freq), y= Freq, fill = Factor)) +
geom_bar(stat = "identity") +
scale_fill_manual(values=c("green", "blue"))+
scale_y_continuous(labels = scales::percent) +
ylab("%") +
xlab(" ") +
theme(legend.title=element_blank())+
geom_text(aes(label = Value), position = position_stack(vjust = 0.5), color
= "white", check_overlap = T, size = 3) +
coord_flip() +
guides(fill=guide_legend(reverse=T))
Update: The answer showed in the "duplicate" question does not work. The answer showed in this post Order Bars in ggplot2 bar graph seems to do not work.