gpt4 book ai didi

r - abline 和 stat_smooth 的 ggplot2 图例

转载 作者:行者123 更新时间:2023-12-04 23:45:29 26 4
gpt4 key购买 nike

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

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

最佳答案

最后,我用一个技巧找到了另一种方法。首先,我计算了线性回归并将结果转换为一个数据框,我添加了我的最佳拟合(截距 = 0 和斜率 = 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))")

也许有一种方法可以为这两行使用颜色,但我不知道如何使用?

谢谢你们的帮助!

关于r - abline 和 stat_smooth 的 ggplot2 图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29844161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com