abline和stat_smooth的{ggplot2图例

时间:2015-04-24 09:46:00

标签: r ggplot2 legend

我对ggplot传说有一些问题,这是我的第一个代码,只有corrGenes的图例,这很好。

gene1=c(1.041,0.699,0.602,0.602,2.585,0.602,1.000,0.602,1.230,1.176,0.699,0.477,1.322)
    BIME = c(0.477,0.477,0.301,0.477,2.398,0.301,0.602,0.301,0.602,0.699,0.602,0.477,1.176)
    corrGenes=c(0.922,0.982,0.934,0.917,0.993,0.697,0.000,0.440,0.859,0.788,0.912,0.687,0.894)

DF=data.frame(gene1,BIME,corrGenes)

plot= ggplot(data=DF,aes(x=gene1,y=BIME))+
  geom_point(aes(colour=corrGenes),size=5)+
  ylab("BIME normalized counts (log10(RPKM))")+
  xlab("gene1 normalized counts (log10(RPKM))")

当我添加abline和smooth时,我得到了正确的情节:

plot= ggplot(data=DF,aes(x=gene1,y=BIME))+
  geom_point(aes(colour=corrGenes),size=5)+
  geom_abline(intercept=0, slope=1)+
  stat_smooth(method = "lm",se=FALSE)+
  ylab("BIME normalized counts (log10(RPKM))")+
  xlab("gene1 normalized counts (log10(RPKM))")

但无法获得他们的传奇,我尝试了许多其他组合:

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)+
  stat_smooth(aes(colour="data"),method = "lm",se=FALSE)+
  scale_colour_manual(name="Fit", values=c("data"="blue", "best"="black"))+
  ylab("BIME normalized counts (log10(RPKM))")+
  xlab("gene1 normalized counts (log10(RPKM))")

如果有人有想法解决这个微小但非常烦人的问题,那将非常有帮助!

3 个答案:

答案 0 :(得分:2)

show_guide=TRUE参数应显示geom_ablinestat_smooth的图例。尝试运行以下代码。

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))")

答案 1 :(得分:2)

不确定这是否是最佳解决方案,但我能够告诉ggplot有两个刻度,一个用于颜色(你的点),另一个用于填充颜色。你可能会问哪种颜色?我在aes中为两行添加的那个:

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))")

enter image description here

答案 2 :(得分:2)

最后,我找到了使用技巧的方式。首先,我计算了线性回归并将结果转换为数据框,我添加了最合适的数据(Intercept = 0和slope = 1),然后我添加了一个数据类型列(数据或最佳)。

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)

然后我用一个独特的geom_abline命令绘制它并将DF从ggplot()移动到geom_point()并使用linetype参数来区分这两行:

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))")

也许有一种方法可以为这两行使用颜色,但我找不到怎么样?

感谢您的帮助!