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

标签 r ggplot2 scatter-plot facet legend-properties

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

# 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()

Plot

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


我查看了 Turning off some legends in a ggplot 上的问题然而,在阅读完它之后,我不清楚如何禁用与特定geom有关的图例元素。

最佳答案

诀窍是覆盖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() 

关于使用多个 aes 设置绘制构面时从 ggplot 中的图例中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32806424/

相关文章:

r - 使用 R 美化 Sankey/Alluvial 可视化

python - 在 plt.scatter 中明确设置颜色条箱

r - 使用 ggarrange 遍历列表

r - 按列名称中的 ID 操作数据框列

r - 使用一个相似的列对两个数据框列表执行函数

r - ggplot : colour points by groups based on user defined colours

python - 检查seaborn散点图函数是否正在采样数据

r - POSIXct/POSIXlt 和亚秒级精度的奇怪行为

r - 在r中的ggplot中添加 map 中心点

r - 如何在 ggplot2 R 图中设置轴限制?