2

I have two stacked area plots with a line plotted on top. In both cases, my plotting order has been this:

  1. Create ggplot
  2. Add stacked area (geom_area); change stacked area colors
  3. Add line (geom_line); change line color

In my first graph, the stacked area colors show up above the Reported Catch line my legend, while in my second graph, the Reported Catch line shows up above the stacked area colors. There appears to be no major difference between the two in terms of R code, so I have no idea why are they different in legend order.

How can I change my first graph's legend to match the second graph's legend, so that the "Reported Catch" line is above the stacked area colors?

Plot 1. Incorrect legend order. plot 1. Incorrect legend order

Plot 2. Correct legend order. plot 2. Correct legend order

Please let me know if you'd like a simplified version of my R code. The dataset I am using can be downloaded here: Sea Around Us

spops
  • 572
  • 1
  • 7
  • 25
  • 1
    Please show some example code, it's hard to tell how your legend is actually produced at the moment. Also, look at the final example in `?ggplot2::guides` that shows how to change legend order. – Marius Jun 18 '17 at 23:15
  • 1
    `guides(fill = guide_legend(reverse = TRUE)` – RobertMyles Jun 18 '17 at 23:20
  • 2
    @RobertMc that reverses the order of plotting within a legend, i.e. discards-landings becomes landings-discards – Scransom Jun 18 '17 at 23:45
  • 1
    `guides(fill = guide_legend(order = 1)` nice plots btw – Scransom Jun 18 '17 at 23:45
  • Will add in sample code in a minute! @geryan - correct on the first thing; thanks for the second!; and for the third - I couldn't get that answer to work (I think) because in the question you linked to, it was all one type of geom (geom point). I'm not very good at R however so maybe I did something wrong. – spops Jun 19 '17 at 00:06
  • 1
    Actually just kidding, wow, turns out `guides(fill = guide_legend(reverse = TRUE)` DOES reverse _everything_ within a legend. I had that line snuck into my first plot and deleted it to switch the order of Industrial and Small-scale, and it turns out it fixed everything. Thank you everyone. How should I close this question? @RobertMc if you answer I can mark as correct? – spops Jun 19 '17 at 00:12
  • 2
    I'm not 100% sure that using `reverse = TRUE` *should* fix this issue, given that it's supposed to change the order within a legend. It might work in this case because of some side effect and not work in other situations, e.g. a side effect of modifying one of your scales through a `guides()` call might be changing the order of the legends. – Marius Jun 19 '17 at 00:26
  • @Marius I'm not using guides anywhere - do you think it is some sort of bug? – spops Jun 19 '17 at 00:42
  • @spops If you're happy with it and it works, I'll add it as answer. – RobertMyles Jun 19 '17 at 00:43

1 Answers1

3

guides(fill = guide_legend(reverse = TRUE) should work!

RobertMyles
  • 2,673
  • 3
  • 30
  • 45
  • 1
    Though for clarification on my specific question, for anybody visiting this question - DELETING a sneaky `guides(fill = guide_legend(reverse = TRUE)` within my code fixed the issue. – spops Jun 19 '17 at 00:49