robert
robert

Reputation: 3726

ggplot2: specify shape for density plot

In order to improve the visibility of my density plot if printed in black and white I would like to add markers to it:

foo <- data.frame(
  v2=sample(c(1,2,3),size=10,rep=T),
  v3=as.factor(sample(2,10,rep=T))
)

p <- ggplot(foo, aes(x=foo$v2, colour=foo$v3, shape=foo$v3))
p <- p + geom_line(stat="density")
p

I understand that the density curve is a continuous line, but it would be nice to put markers on it at given points. Is there a way to do this?

Upvotes: 0

Views: 1077

Answers (2)

LyzandeR
LyzandeR

Reputation: 37889

This should do it then:

p <- ggplot(foo, aes(x=v2))
p <- p + geom_line(aes(linetype=v3,colour=v3),stat="density",size=2)
p

enter image description here

Now the difference should be clear.

Upvotes: 2

Modifying a little the answer from @LyzandeR maybe you could achieve something similar to what you are asking for by using the group parameter:

p <- ggplot(foo, aes(x = v2, colour = v3, group = v3))
p <- p + 
    geom_line(stat='density', aes(linetype = v3), size = 3) +
    geom_line(stat='density', size = 1) +
    scale_linetype_manual(values=c("dashed", "dotted"))    
p

enter image description here

You can explore the different linetypes here, for example.

Upvotes: 2

Related Questions