abline 和 stat_smooth 的 ggplot2 图例
ggplot2 legend for abline and stat_smooth
我对 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))")
如果有人有解决这个微小但非常烦人的问题的想法,那将非常有帮助!
show_guide=TRUE
参数应显示 geom_abline
和 stat_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))")
不确定这是否是最佳解决方案,但我能够告诉 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))")
终于,我找到了另一种使用技巧的方法。首先,我计算了线性回归并将结果转换为一个数据框,我添加了我的最佳拟合(截距 = 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() 并使用线型参数来区分两条线:
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))")
也许有一种方法可以为这两行使用颜色,但我不知道如何使用?
感谢大家的帮助!
我对 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))")
如果有人有解决这个微小但非常烦人的问题的想法,那将非常有帮助!
show_guide=TRUE
参数应显示 geom_abline
和 stat_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))")
不确定这是否是最佳解决方案,但我能够告诉 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))")
终于,我找到了另一种使用技巧的方法。首先,我计算了线性回归并将结果转换为一个数据框,我添加了我的最佳拟合(截距 = 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() 并使用线型参数来区分两条线:
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))")
也许有一种方法可以为这两行使用颜色,但我不知道如何使用?
感谢大家的帮助!