I have a small dataset that looks like this:
Name<-c("Al","Al","Ellen","Ellen","Luisa","Luisa","Mark","Mark","Randy","Randy")
Sex<-c("M","F","M","F","M","F","M","F","M","F")
Value<-c(0,1,2,3,6,4,7,3,5,1)
mydata<-data.frame(Name,Sex,Value)
I would like to have a plot like this one:
p<-ggplot(mydata,
aes(x=reorder(Name,-Value),
y=Value,
fill=Sex)) +
geom_bar(stat = "identity",
position="stack") +
scale_fill_manual(values=c("#3CBB75FF","#95D840FF")) +
xlab("Name") +
ylab("Value") +
scale_y_continuous(limits = c(0, 10),
breaks = seq(0, 10, by = 2)) +
theme(axis.line = element_blank(),
axis.text.x=element_text(angle = 90,
vjust = 0.35,
hjust=1,
size=13,
face="italic",
margin=margin(b=10),
colour="black"),
axis.text.y=element_text(size=13,
margin=margin(l=10),
colour="black"),
axis.ticks = element_blank(),
axis.title=element_text(size=18,
face="bold"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.text = element_text(size=14),
legend.title = element_blank())
p
I would like to have the names ordered by the highest value of M. Is there a way to maintain the alphabetical order when M is equal (as in the case of Al and Randy) but having as first the highest value of M when the sum M+F is equal (as in the case of Luisa and Mark)? I would like Mark before Luisa. Any suggestions?