使用多个 aes 设置绘制构面时从 ggplot 中的图例中删除元素

Removing elements from the legend in ggplot when plotting facets with multiple aes settings

我正在使用下面的代码生成一个简单的图表:

# Data and libs
data(mtcars)
reuire(ggplot2); require(ggthemes)

# Chart def
ggplot(mtcars, aes(wt, mpg, colour = as.factor(vs))) +
    geom_point(aes(colour = factor(cyl))) + 
    facet_wrap(~ cyl) +
    guides(colour = guide_legend(title = "Something")) +
    geom_smooth(method = "lm", se = FALSE) +
    theme_pander()

如何删除图例中的线条?我对图例感兴趣,只显示具有相应颜色的点,而没有来自 geom_smooth(method = "lm", se = FALSE).

的线

我查看了 Turning off some legends in a ggplot 上的问题,但是,在阅读之后我不清楚如何禁用与特定 [=12= 有关的图例元素].

诀窍是覆盖 aes:

guides(colour = guide_legend(title = "Something", override.aes = list(linetype = 0)))
# Data and libs
library(ggplot2)
library(ggthemes)

# Chart def
ggplot(mtcars, aes(wt, mpg, colour = as.factor(vs))) +
  geom_point(aes(colour = factor(cyl))) + 
  facet_wrap(~cyl) +
  geom_smooth(method = "lm", se = FALSE) + 
  guides(colour = guide_legend(title = "Something", override.aes = list(linetype = 0))) +
  theme_pander() 

出于某种原因,theme_pander 对我来说看起来不一样。

更新: 或者,您可以使用 show.legend = FALSE,正如 scoa 指出的那样,我实际上更喜欢:

ggplot(mtcars, aes(wt, mpg, colour = as.factor(vs))) +
  geom_point(aes(colour = factor(cyl))) + 
  facet_wrap(~cyl) +
  geom_smooth(method = "lm", se = FALSE, show.legend = FALSE) + 
  guides(colour = guide_legend(title = "Something")) +
  theme_pander()