2015-04-29 45 views
13

我有一個三個不同的圖例:一個用於linetype,一個用於color,另一個用於fill。在colorfill的傳說中,還有一些我希望刪除的行,但是如何?從顏色和填充圖例中刪除線條

下面是一些示例代碼:

# some data 
hline_df <- data.frame(name = c('a', 'b'), y = c(1, 2)) 
df <- data.frame(x = c(1, 2), y = c(0.5, 1.5), con = c('a', 'b'), col = c('d', 'e')) 

# the plot 
ggplot(df, aes(x, y, fill = con)) + 
    geom_bar(stat = 'identity') + 
    geom_point(aes(color = col)) + 
    geom_hline(data = hline_df, aes(yintercept = y, linetype = name), 
      color = 'red', show_guide = TRUE) 

enter image description here

我得到兩個紅線 「名稱」 指南,這是沒有問題。
「col」指南中有紅色線條橫過網點,我想刪除它們!
「con」指南也有紅線,應該刪除。

我可以修改傳說中的一些部分

guides(fill = guide_legend(override.aes = list(colour = NULL)), 
     color = guide_legend(override.aes = list(colour = NULL))) 

這消除了色彩,但線條仍然存在。

在此先感謝!

+0

嗯,重排以'ggplot(DF,AES(X,Y,填充= CON))+ geom_hline(數據= hline_df,AES(y截距= Y,線型=名), color ='red',show_guide = TRUE)+ geom_point(aes(color = col))+ geom_bar(stat ='identity')+ geom_hline(data = hline_df,aes(yintercept = y,linetype = name), color ='red',show_guide = F)似乎適用於'con',但紅線仍然在'col'中。說實話,我不明白,爲什麼它的工作:-) – drmariod

+1

啊,我試圖設置'linetype = NULL',並沒有像預期的那樣工作......還有兩次繪製hline的技巧,在後面和前面的一個很棒!你想發表一個答案,所以我可以把它標記爲固定的? – drmariod

回答

12

你可以設置你的override.aes通話linetype = 0"blank"(在不同的linetype小號here)爲fillcolorguide秒。

另請注意,我將fillaesggplot的「頂層」移至geom_bar

ggplot(df, aes(x, y)) + 
    geom_bar(aes(fill = con), stat = 'identity') + 
    geom_point(aes(color = col)) + 
    geom_hline(data = hline_df, aes(yintercept = y, linetype = name), color = 'red', show_guide = TRUE) + 
    guides(fill = guide_legend(override.aes = list(linetype = 0)), 
     color = guide_legend(override.aes = list(linetype = 0))) 

enter image description here

+0

謝謝,這甚至有點乾淨,並解釋了這個問題......它只是將填充和顏色傳說的aes線型設置爲0 ...太好了! – drmariod

+0

優秀的答案。我正在研究一個提取幾個傳說的解決方案,然後嘗試將它們放在一起。但是,這是一個更好,更優雅的解決方案! +1 – Jaap

1

使用:

ggplot(df, aes(x,y,fill=con)) + geom_bar(stat='identity') + 
    geom_point(aes(color=col)) + 
    geom_hline(data=hline_df,aes(yintercept=y,linetype=name),color='red',show_guide=FALSE) + 
    guides(linetype=FALSE,color=FALSE) 

給了我這樣的情節: enter image description here

+0

謝謝@Jaap,但我想擁有所有的指南......只是想從'con'和'col'指南中刪除紅線... – drmariod

+0

所以,基本上'ggplot(df,aes(x, (color = col))+ geom_hline(data = hline_df,aes(yintercept = y,linetype = name),color ='red')+ geom_bar(stat ='identity')+ ,show_guide = FALSE)'是con'和'col'的預期結果,但我需要一個紅線的指南以及... – drmariod

+0

好的,謝謝你的反饋。我會看看它。 – Jaap

2

正如user20650

ggplot(df, aes(x,y)) + 
    geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=TRUE) + 
    geom_point(aes(color=col), size=5) + 
    geom_bar(aes(fill=con), stat='identity') + 
    geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=F) + 
    guides(color = guide_legend(override.aes = list(linetype = 0))) 

建議所以第一geom_hline創造傳奇,但該行是酒吧的後面。 ..
第二次調用將酒吧前面的線條但不打印圖例(好想法)。
拉斯維加斯指南再次改寫審美觀型與0 ...這樣它會從神話傳說中的線......我試着用NULL但這並沒有工作過......

感謝。

enter image description here