ggplot 更改图例中的几何顺序 [英] ggplot change geom order in legend

查看:79
本文介绍了ggplot 更改图例中的几何顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个堆积面积图,顶部绘制了一条线.在这两种情况下,我的绘图顺序都是这样的:

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

  1. 创建 ggplot
  2. 添加堆叠区域(geom_area);更改堆叠区域颜色
  3. 添加行(geom_line);更改线条颜色
  1. Create ggplot
  2. Add stacked area (geom_area); change stacked area colors
  3. Add line (geom_line); change line color

在我的第一个图表中,堆积区域颜色显示在我的图例报告捕获线上方,而在我的第二个图表中,报告捕获线显示在​​堆积区域颜色上方.两者在 R 代码方面似乎没有太大区别,所以我不知道为什么它们在图例顺序上不同.

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.

如何更改第一个图形的图例以匹配第二个图形的图例,以便Reported Catch"线位于堆叠区域颜色上方?

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?

图 1.图例顺序不正确.

Plot 1. Incorrect legend order.

绘图 2.正确的图例顺序.

Plot 2. Correct legend order.

如果您想要我的 R 代码的简化版本,请告诉我.我使用的数据集可以在这里下载:我们周围的海

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

推荐答案

guides(fill = guide_legend(reverse = TRUE) 应该可以工作!

这篇关于ggplot 更改图例中的几何顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆