我使用下面的代码生成一个简单的图表:
# 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 上的问题然而,在阅读完它之后,我不清楚如何禁用与特定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/