1

I have edited the data below so that it's dummy data. The column for Source has proprietary names.

I have data that look like this and I would like to rearrange my stacked chart by rank and NOT source but would like my legend to reflect Source.

TotalBySource <- read.table(header=TRUE, stringsAsFactors=FALSE,
text="FY        Source  Rank TotalExpense   TotalforFY  Percent
2006    Option1 1    46753094.2     121493809.7 0.384818735
2006    Option7 7    7113652.3      121493809.7 0.058551562
2006    Option6 6    34461918.9     121493809.7 0.283651644
2006    Option8 8    10586263.1     121493809.7 0.087134177
2006    Option5 5    1777846.8      121493809.7 0.014633229
2006    Option4 4    17886868.4     121493809.7 0.147224525
2006    Option3 3    2118019.3      121493809.7 0.017433146
2006    Option2 2    796146.7       121493809.7 0.006552982
2007    Option7 7    6833612        118232170.7 0.057798245
2007    Option5 5    1676225.2      118232170.7 0.014177404
2007    Option6 6    35245142.1     118232170.7 0.298101117
2007    Option3 3    2283154.9      118232170.7 0.019310775
2007    Option4 4    17948447.3     118232170.7 0.151806798
2007    Option8 8    10279117       118232170.7 0.086940102
2007    Option1 1    43397313.8     118232170.7 0.367051654
2007    Option2 2    569158.4       118232170.7 0.004813905
2008    Option1 1    43962329.7     115013461.7 0.382236384
2008    Option7 7    6745206.4      115013461.7 0.058647104
2008    Option6 6    34288244.4     115013461.7 0.298123749
2008    Option8 8    10304301.5     115013461.7 0.089592134
2008    Option5 5    1551682.9      115013461.7 0.013491316
2008    Option4 4    15180864       115013461.7 0.131992062
2008    Option3 3    2398345.7      115013461.7 0.020852739
2008    Option2 2    582487.1       115013461.7 0.005064512")

I constructed this code:

 library(ggplot2)
 p4 <- ggplot() + 
   geom_bar(aes(y = Percent, x = FY, fill = Source), data = TotalBySource, stat="identity")

and got a lovely stacked bar graph

but would really want the stacks and the legend to be sorted by rank and the legend to read the respective Source. Can I get some assistance?

bdemarest
  • 14,397
  • 3
  • 53
  • 56
Laura Walker
  • 307
  • 2
  • 6
  • 16

1 Answers1

1

I got a stacked bar chart with "Option1" to "Option8" from top to bottom, i. e. sorted alphabetically. Did you mean to sort the other way round, i. e. "Option8" at the top, then going down?

If so, you could use Hadley Wickhams forcats package to reverse factor order like this:

library(forcats)
TotalBySource$Source <- fct_rev(TotalBySource$Source)

p4 <- ggplot() + 
      geom_bar(aes(y = Percent, x = FY, fill = Source), 
               data = TotalBySource, stat = "identity")
p4

enter image description here

bdemarest
  • 14,397
  • 3
  • 53
  • 56
Wolf
  • 126
  • 4
  • thank you @wolf but that didn't sort the graph or the legend by how I wanted it to be. – Laura Walker Feb 21 '17 at 23:43
  • I wanted it to be listed like: Option8 Option7 Option6 Option5 Option4 Option3 Option2 Option1 – Laura Walker Feb 21 '17 at 23:43
  • @laura, I'm not sure why this didn't work for you. It worked for me. Give it another try with the new edit to your `TotalBySource` dataset above. – bdemarest Feb 22 '17 at 06:51
  • @laura, if you do it twice, you will have the original order again. The order is reversed each time you call the fct_rev function. It doesn't explicitly say that Option8 should be at the top. It just reverses the alphabetical order that R uses first time. Next run it will restore the alphabetical order, the following run will put Option8 at the top again, and so on. Did you get the result you wanted? – Wolf Feb 22 '17 at 10:36
  • No I didn't. Nothing changed between the times I ran the code – Laura Walker Feb 22 '17 at 14:59
  • I wanted both the legend and the stacks to be sorted by rank but the legend to reflect Sources. Should I change TotalBySource$Source <- fct_rev(TotalBySource$Source) to TotalBySource$Rank <- fct_rev(TotalBySource$Rank) – Laura Walker Feb 22 '17 at 15:05
  • You're not using the Rank variable in the ggplot call, so that wouldn't help. Did the fct_rev command run without error message? Have you got the forcats library installed and loaded? Is your Source variable a factor? If not, change it to a factor first, before the call to fct_rev. TotalBySource$Source <- factor(TotalBySource$Source) – Wolf Feb 22 '17 at 17:04
  • @laura If you want to sort the factor levels by overall rank of `Percent`, see `fct_reorder` from package forcats. If each stack should have a different order you face a harder problem, and you should likely clarify that in your question. – aosmith Feb 22 '17 at 20:17
  • @laura, I am still unclear, does the above picture meet your requirements? If not, what exactly would need to be different? If that picture is good, then it becomes a question of implementing the solution. – bdemarest Feb 22 '17 at 21:22