[英]ggplot2 legend for abline and stat_smooth
I have some problems with ggplot legends, here is my first code with only the legend for corrGenes, which is fine. 我有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))")
When I add abline and smooth, I get the correct plot with : 当我添加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))")
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! 如果有人有想法解决这个微小但非常恼人的问题,那将非常有帮助!
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).
首先,我计算了线性回归并将结果转换为数据框,我添加了最合适的数据(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)
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 : 然后我用一个独特的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))")
There is maybe a way to use colors for those 2 lines, but I can't find out how? 也许有一种方法可以使用这两行的颜色,但我不知道如何?
Thanks for your help guys! 谢谢你的帮助!
The show_guide=TRUE
argument should display the legends for both geom_abline
and stat_smooth
. show_guide=TRUE
参数应显示geom_abline
和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))")
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. 不确定这是否是最佳解决方案,但我能够告诉ggplot有两个刻度,一个用于颜色(你的点),另一个用于填充颜色。 Which fill colour you are probably asking?
你可能会问哪种颜色? The one I added in the
aes
for the two lines: 我在两条线的
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))")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.