2015-09-25 90 views
2

我的數據:Legend在GGPLOT2,刪除級別

df <- data.frame(sp = c(LETTERS[1:8]), 
       tr = c("NS", "LS", "NS", "LS", "LS", "HS", "HS", "HS"), 
       bv = c(14, 5, 11, 5.6, 21, 5.4, 2, 4.8), 
       av = c(0.0, 14, 21, 48.4, 15, 55.6, 37, 66.2)) 

我做柱狀圖

library(reshape2) 
df1 <- melt(df, id.vars = c("sp", "tr")) 

ggplot(aes(sp, value, fill = variable) , data = df1) + theme_classic() + 
    geom_bar(aes(lty = tr), lwd = 1.2, data = df1, stat = "identity", colour = "black", width =.8) + 
    theme(legend.position = "bottom") + 
    scale_linetype_discrete(name = "ja") 

輸出 enter image description here 什麼我不喜歡的是傳奇。我想從第二部分「ja」中刪除「variable」部分的行類型。我想在圖例框中有白色背景,而不是灰色背景。

回答

5

您可以通過設置在guidesfill = FALSE刪除variable傳說和你guide_legend(也在裏面guides)改變的backgroundColor與override.aes如下:

ggplot(df1, aes(sp, value, fill = variable)) + 
    geom_bar(aes(lty = tr), lwd = 1.2, stat = "identity", colour = "black", width =.8) + 
    scale_linetype_discrete(name = "ja") + 
    guides(fill = FALSE, 
     lty = guide_legend(override.aes = list(lty = c('dotted', 'dashed', 'solid'), 
               fill = "white"))) + 
    theme_classic() + 
    theme(legend.position = "bottom") 

這導致了以下情節:

enter image description here