簡體   English   中英

ggplot2:如何從geom_density圖例中刪除斜杠

[英]ggplot2: how to remove slash from geom_density legend

我試圖在ggplot2中繪制一些重疊的密度圖。 我遇到了一個問題,我無法從圖例中刪除斜線。 我嘗試過使用scale_fill_manual()和legend.key以及來自R Cookbook的黑客攻擊,但我似乎無法正確使用它。

data(iris)
iris=iris
cols=brewer.pal(3,"Set1")

ggplot(iris) + 
    geom_density(position="identity",aes(x=iris$Sepal.Length,fill=cols[1]),
        colour="black",alpha=.5) +
    geom_density(position="identity",aes(x=iris$Sepal.Width,fill=cols[2]),
        colour="black",alpha=.5)+  
    theme_bw() +
    scale_fill_identity(guide="legend",labels=c("Sepal Width","Sepal Length"))+
    xlab("X axis") +
    theme(panel.background=element_blank(),
        legend.title=element_blank(),
        legend.key = element_rect(),
        legend.background = element_blank(),
        legend.justification=c(1,0), 
        legend.position=c(.75,.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())

在此輸入圖像描述

我該怎么做才能解決這個問題?

嘗試這個:

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

雖然這也刪除了黑色輪廓......可以通過將theme更改為:

legend.key = element_rect(colour = "black")

我完全忘了添加這個重要的注意事項: 不要通過指定美觀x=iris$Sepal.Length使用$操作! 這不是使用aes()的預期方式,它將導致錯誤和意外問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM