ggplot2图例,用于abline和stat_smooth [英] ggplot2 legend for abline and stat_smooth

查看:383
本文介绍了ggplot2图例,用于abline和stat_smooth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ggplot图例有一些问题,这是我的第一个仅带有corrGenes图例的代码,这很好.

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

当我添加斜率和平滑度时,我得到了正确的图:

When I add abline and smooth, I get the correct plot with :

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

但是我没有办法为他们获取图例,我尝试了许多其他组合:

but no way to get the legend for them, I tried and many other combinations:

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

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

If anyone has an idea to solve this tiny but very annoying problem, it would be very helpfull!

推荐答案

最后,我找到了另一种技巧.首先,我计算了线性回归并将结果转换为一个数据框,该框添加了我的最佳拟合(截距= 0且斜率= 1),然后添加了一个用于数据类型(数据或最佳)的列.

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)

然后我用一个独特的geom_abline命令将其绘制,并将DF从ggplot()移至geom_point(),并使用linetype参数区分两条线:

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?

感谢您的帮助!

这篇关于ggplot2图例,用于abline和stat_smooth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆