使用 show.legend = FALSE 删除图例不适用于连续美感

Pat*_*ckT 5 r ggplot2

我尝试通过设置删除图例show.legend = FALSEfill当变量是离散的时,它按预期工作:

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

在此输入图像描述 但是,当fill映射到连续变量时,show.legend = FALSE不会删除图例:

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

在此输入图像描述

为什么不show.legend = FALSE省略连续比例的图例?我该如何解决这个问题?

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

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

arv*_*000 8

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

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

  • 这是因为添加 `theme_bw()` _afterwards_ 会取消您在 `theme()` 中应用的设置。首先添加 `+ theme_bw()` 然后添加 `+ theme(legend.position = 'none')` ,然后就可以了 (3认同)