I have some problems with ggplot legends, here is my first code with only the legend for corrGenes, which is fine.
gene1=c(1.041,0.699,0.602,0.602,2.585,0.60
Finally, I found anther way using a trick. First, I've computed the linear regression and convert the results to a data frame which I add my best fit (Intercept = 0 and slope =1), then I added a column for type of data (data or best).
modele = lm(BIME ~ gene1, data=DF)
coefs = data.frame(intercept=coef(modele)[1],slope=coef(modele)[2])
coefs= rbind(coefs,list(0,1))
regression=as.factor(c('data','best'))
coefs=cbind(coefs,regression)
then I plotted it with a unique geom_abline command and moving the DF from ggplot() to geom_point() and used the linetype parameter to differenciate the two lines :
plot = ggplot()+
geom_point(data=pointSameStrandDF,aes(x=gene1,y=BIME,colour=corrGenes),size=5)+
geom_abline(data=coefs, aes(intercept=intercept,slope=slope,linetype=regression), show_guide=TRUE)+
ylab("BIME normalized counts (log10(RPKM))")+
xlab("gene1 normalized counts (log10(RPKM))")
There is maybe a way to use colors for those 2 lines, but I can't find out how?
Thanks for your help guys!
Not sure if this is the best solution, but I was able to tell ggplot to have two scales, one for the colours (your points), the other one for the fill colour. Which fill colour you are probably asking? The one I added in the aes
for the two lines:
plot = ggplot(data=DF,aes(x=gene1,y=BIME)) +
geom_point(size=5, aes(colour=corrGenes)) +
geom_abline(aes(fill="black"),intercept=0, slope=1) +
stat_smooth(aes(fill="blue"), method = "lm",se=FALSE) +
scale_fill_manual(name='My Lines', values=c("black", "blue"))+
ylab("BIME normalized counts (log10(RPKM))")+
xlab("gene1 normalized counts (log10(RPKM))")
The show_guide=TRUE
argument should display the legends for both geom_abline
and stat_smooth
. Try running the below code.
plot= ggplot(data=DF,aes(x=gene1,y=BIME))+
geom_point(aes(colour=corrGenes),size=5)+
geom_abline(aes(colour="best"),intercept=0, slope=1, show_guide=TRUE)+
stat_smooth(aes(colour="data"),method = "lm",se=FALSE, show_guide=TRUE)+
scale_colour_manual(name="Fit", values=c("data"="blue", "best"="black"))+
ylab("BIME normalized counts (log10(RPKM))")+
xlab("gene1 normalized counts (log10(RPKM))")