2017-06-24 140 views
0

我有一個ggplot,其中我使用的顏色是我的geom_points作爲我的一列(我的處理)的函數,然後我使用scale_color_manual來選擇顏色。ggplot2 - 爲多個圖層添加手動圖例

我自動獲得我的傳說權

的問題是我需要繪製一個與實驗設置,其中我與geom_vline做做一些水平線,但我不知道如何手動添加一個單獨的圖例,不會混淆我已經擁有的圖例,並說明這些圖線是什麼。

我有以下代碼

ggplot(dcons.summary, aes(x = meters, y = ymean, color = treatment, shape = treatment)) + 
    geom_point(size = 4) + 
    geom_errorbar(aes(ymin = ymin, ymax = ymax)) + 
    scale_color_manual(values=c("navy","seagreen3"))+ 
    theme_classic() + 
    geom_vline(xintercept = c(0.23,3.23, 6.23,9.23), color= "bisque3", size=0.4) + 
    scale_x_continuous(limits = c(-5, 25)) + 
    labs(title= "Sediment erosion", subtitle= "-5 -> 25 meters; standard deviation; consistent measurements BESE & Control", x= "distance (meters)", y="erosion (cm)", color="Treatment", shape="Treatment") 

enter image description here

所以我只需要在「處理」一個說下一個額外的傳說「BESE PLOTS位置」,這是關係到灰線

我一直在尋找一個解決方案,我已經使用"scale_linetype_manual""guides"嘗試過,但我沒有收到有

回答

0

由於您沒有提供可重複的示例,因此我使用了mtcars數據集中的數據。 另外我修改了this similar answer一點點。正如你已經指定的顏色,除了填充因子不能在這裏工作,你可以使用線型爲aes至極內的第二個參數可以在圖例中顯示:

xid <- data.frame(xintercept = c(15,20,30), lty=factor(1)) 
mtcars %>% 
    ggplot(aes(mpg ,cyl, col=factor(gear))) + 
     geom_point() + 
     geom_vline(data=xid, aes(xintercept=xintercept, lty=lty) , col = "red", size=0.4) + 
     scale_linetype_manual(values = 1, name="",label="BESE PLOTS LOCATION") 

enter image description here

或者不第二個數據幀:

ggplot() + 
     geom_point(data = mtcars,aes(mpg ,cyl, col=factor(gear))) + 
     geom_vline(aes(xintercept=c(15,20,30), lty=factor(1)), col = "red", size=0.4)+ 
     scale_linetype_manual(values = 1, name="",label="BESE PLOTS LOCATION") 
+0

哦,這很簡單。我已經在嘗試scale_linetype_manual,但並未像你那樣巧妙地將aes添加到geom_vline。 – Locean