I created the bar char as below:
mydata<-data.frame(id=c(1,1,2,2,3,3,4,4,5,5),
total=c(24,24,24,24,22,22,22,22,22,22),
category=rep(c("uncomplete","complete"),5),
value=c(0,24,0,24,1,21,0,22,2,20))
mydata%>%
ggplot(aes(x=reorder(id, -total),
y=value, fill=factor(category,levels=c("uncomplete","complete"))))+
geom_bar(position=position_stack(),stat="identity",width=0.7) +
geom_text(data=subset(mydata,value != 0),
aes(label = value),position=position_stack(vjust=0.5),size=3) +
theme(legend.position = c(0.90,0.85)) +
labs(x="Site ID",y="",fill="") +
scale_y_continuous(breaks = seq (0, 30, 5), limits=c(0,30), name="Enrollment")
As you can see, the chart was ordered by total
number in a descending order. However, I don’t know how to let the chart be ordered by the complete
number in a descending order at the same time, i.e. I’d like to order the id as 1,2,4,3,5.