本文介绍了使用show.legend = FALSE放下图例无法持续美观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过设置show.legend = FALSE删除图例.当fill变量为离散时,它可以按预期工作:

I try to drop the legend by setting show.legend = FALSE. It works as expected when the fill variable is discrete:

library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = wt, fill = factor(mpg))) +
  geom_bar(stat = "identity", show.legend = FALSE)

但是,当fill映射到连续变量时,show.legend = FALSE不会删除图例:

However, when fill is mapped to a continuous variable, show.legend = FALSE does not drop the legend:

ggplot(mtcars, aes(x = mpg, y = wt, fill = mpg)) + 
   geom_bar(stat = "identity", show.legend = FALSE) 

为什么show.legend = FALSE不会为连续刻度省略图例?我该如何解决?

Why doesn't show.legend = FALSE omit the legend for a continuous scale? How can I solve this?

我有ggplot2 v.2.0.0(作者:Hadley Wickham)

I have ggplot2 v.2.0.0 (author: Hadley Wickham)

参考: http://docs.ggplot2.org/current/geom_bar.html

推荐答案

对于您的示例案例,您可以使用theme()

For your example case, you can use theme()

ggplot(mtcars, aes(mpg, wt, fill = mpg)) + 
  geom_bar(stat = "identity") +
  theme(legend.position = 'none')

这篇关于使用show.legend = FALSE放下图例无法持续美观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 21:52