Hi all, I was trying to draw a stacked density plot like that : 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("") + ylab("") + scale_colour_manual(name = "Pallet", labels = c("X", "Y")) 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? 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? Thanks -- View this message in context: http://www.nabble.com/Density-plot-in-ggplot2-tp24702858p24702858.html Sent from the R help mailing list archive at Nabble.com.
Hi Ron, I'm not sure why ylab doesn't work. Maybe a bug. I note the label doesn't get removed with labs() either. However using scale_y_continuous(name="") does remove the label. For the legend, you are using a fill scale, not a colour scale i.e. fill=factor(dat[,2]), not colour=factor(dat[,2]) use scale_fill_hue(name = "Pallet", labels = c("X", "Y")) instead. Regards, Paul RON70 wrote:> Hi all, I was trying to draw a stacked density plot like that : > 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("") + ylab("") + > scale_colour_manual(name = "Pallet", labels = c("X", "Y")) > > 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? > > 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? > > Thanks
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