[R] Density plot in ggplot2

Matthieu Dubois matthdub at gmail.com
Tue Jul 28 19:14:56 CEST 2009


Hi, 

small modifications to your code will do the trick

> Here everything is ok, except few points :
> 1. I want to remove the name of y-axis, which is by default "density". Here
> I put ylab(""), however although for x-axis it is working, for y-axis it is
> not. Is there any specific formula for that?

use scale_y_continuous('') instead of ylab

> 
> 2. I want to rename the color plot, as well as the title of the color. The
> function scale_colour_manual() seems not working. Can anyone please suggest
> me how to achieve desired thing?

use scale_fill_discrete() 

# new code, everything 's ok
library(ggplot2); library(plyr)
dat <- cbind(rnorm(300), rep(c(1,2), each=150))
ggplot() + geom_density(aes(x=dat[,1], fill=factor(dat[,2]), 
position="stack")) +
 xlab("") + scale_y_continuous("") +
scale_fill_discrete(name = "Pallet", labels = c("X", "Y"))

Regards,

Matthieu




More information about the R-help mailing list