2015-09-27 62 views
2

我使用下面的代碼來生成一個簡單的圖表:從ggplot圖例中刪除元素繪製與多個AES設置面時

# 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傳奇元素。

+1

[在ggplot關閉一些傳說]的可能重複(HTTP:/ /stackoverflow.com/questions/14604435/turning-off-some-legends-in-a-ggplot) – scoa

+1

將'show_guide = FALSE'添加到'geom_smooth' – scoa

+0

@scoa'show_guide'已棄用,但他可以使用show。 legend = FALSE'來代替。這實際上比我的答案簡單。 –

回答

3

關鍵是要覆蓋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()