Change the shape of the legend in density plots with ggplot2

Viewed 716

I want to change boxes shown in the density plot legend into lines, and I understand I need to use guides. However, the following codes are not working:

set.seed(0)    
df <- data.frame(Income=c(rnorm(500,1000,200),rnorm(500,900,10)),Type=c(rep("A",500),rep("K",500)))
p2p <- ggplot(df,aes(x=Income))+geom_density(aes(colour=Type))+
guides(colour = guide_legend(override.aes = list(linetype = 1, shape = 3)))
1 Answers

Are you looking for something like this?

ggplot(df,aes(x=Income))+ 
  geom_density(aes(colour=Type),show_guide=FALSE)+
  stat_density(aes(x=Income, colour=Type), 
               geom="line",position="identity")

enter image description here

Related