I have the following code which works fine:
ggplot(SinceAprilNoZeros, aes(x=MonthClosed, y=Total.Redress.Value, group=SimpleBusinessArea, fill=SimpleBusinessArea)) +
facet_wrap(~Subject.Category) +
geom_bar(stat='summary', fun.y=sum, size = 2) +
labs( title="Redress Paid by Subject Category", x="Category", y="Redress") +
scale_y_continuous(labels=dollar_format(prefix = "£")) +
theme_classic()+
scale_color_discrete(guide=guide_legend(ncol=1))+
scale_fill_manual(values = colpal3)+
theme(axis.text.x=element_text(angle=25,hjust=1,vjust=0.9), axis.title.x = element_blank(), plot.margin = unit(c(0.2,0.2,0.5,0.2), "cm"))
The only problem is the fill is ordered alphabetically and not by the proportion it takes in the bars. As a result I get the larger sections of the bars looking similar in colour out of coincidence.
I tried using colpal3 from the BacArena package to make sure they contrast well but there are a few colours that still look similar.
It looks like ordering the legend by how big the bars are (biggest to smallest) would fix the problem as the similar colours are further down in the list.
I've looked up where people have manually ordered via "order = C(...)" but i'm looking for a way to do the same but using (presumably) the total y value for each "SimpleBusinessArea".
Dummy Data:
SinceAprilNoZeros <- data.frame(MonthClosed=c("April 2017","May 2017", "June 2017","April 2017","May 2017", "June 2017"))
SinceAprilNoZeros$SimpleBusinessArea <- c("Area1", "Area2", "Area3", "Area3", "Area3", "Area1")
SinceAprilNoZeros$Total.Redress.Value <- c(13,23,27,13,12,26)
SinceAprilNoZeros$Subject.Category <- c("Payments", "Experience", "Payments", "Payments", "Experience", "Experience")
Using this code on the dummy data you get:
1 - Yellow - Area1
2 - Purple - Area2
3 - Orange - Area3
I want it to be:
1 - Yellow - Area3 (largest sum of y)
2 - Purple - Area1 (2nd largest sum of y)
3 - Orange - Area2 (smallest sum of y)