0

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)

Saleem Khan
  • 700
  • 2
  • 6
  • 20
  • Your code does not work with your dummy data. Please fix and make sure your code actually runs. – Axeman Dec 01 '17 at 13:13
  • Missed a letter my bad – Saleem Khan Dec 01 '17 at 14:16
  • 1
    Use `reorder` on your factor [as in this answer](https://stackoverflow.com/a/27448463/903061) or [this answer](https://stackoverflow.com/a/5967705/903061). – Gregor Thomas Dec 01 '17 at 17:18
  • Used the one from the first answer and it seems to order it by the count rather than the size of the bar (sum.y). seems to do the trick however. Second one reordered it somehow but not the way I was after. – Saleem Khan Dec 01 '17 at 17:36
  • 1
    See [here](https://github.com/tidyverse/ggplot2/issues/1902) and [here](https://drsimonj.svbtle.com/ordering-categories-within-ggplot2-facets) – TMrtSmith Jun 19 '18 at 08:28

0 Answers0