我试图在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())
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决这个问题?
jor*_*ran 38
试试这个:
+ guides(fill = guide_legend(override.aes = list(colour = NULL)))
Run Code Online (Sandbox Code Playgroud)
虽然这也删除了黑色轮廓......可以通过更改theme为:
legend.key = element_rect(colour = "black")
Run Code Online (Sandbox Code Playgroud)
我完全忘了添加这个重要的注意事项:不要不经指定美学x=iris$Sepal.Length使用$运营商!这不是预期的使用方式,aes()它将导致错误和意外问题.