scale_fill_discrete和scale_fill_manual - 图例选项混淆

Mar*_*ras 20 plot r colors scale ggplot2

我在ggplot2对象上应用两个不同的更改时遇到问题.我有一个有3组的条形图.我想要:

  1. 为每个组设置颜色
  2. 重新排列图例标签的顺序

我找到了以下解决上述问题的方法:

  1. + scale_fill_manual(values = c("red", "green", "blue"))
  2. + scale_fill_discrete(guide = guide_legend(reverse=TRUE))

问题是第二个application(+ scale_fill_discrete(...))覆盖了第一个:

Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.
Run Code Online (Sandbox Code Playgroud)

(分别两者都很好).怎么处理呢?

BBr*_*ill 29

你不需要把它加倍,这应该工作:

  scale_fill_manual(values = c("red", "green", "blue"),name= "My name", guide = guide_legend(reverse = TRUE))
Run Code Online (Sandbox Code Playgroud)