I'd like to have ordered bars in dodge geom_bar
. Do you know how to deal with it?
My code:
ttt <- data.frame(typ=rep(c("main", "boks", "cuk"), 2),
klaster=rep(c("1", "2"), 3),
ile=c(5, 4, 6, 1, 8, 7))
ggplot()+
geom_bar(data=ttt, aes(x=klaster, y=ile, fill=typ),
stat="identity", color="black", position="dodge")
And example plots to better understand the problem:
What I have:
What I would like to have:
One option would be to make a new variable to represent the order the bars should be within each group and and add this variable as the
group
argument in your plot.Lots of ways to that task of making the variable, here's a way using function from dplyr. The new variable is based on ranking
ile
in descending order within eachklaster
group. If you have ties in any group you'll want to figure out what you want to do in that case (what order should the bars be in given ties?). You may want to set theties.method
argument inrank
away from the default, probably to"first"
or"random"
.Now just add
group = position
into your plot code.