致我的程序员伙伴们,
我一直在网上搜索这个问题的答案,我完全被难住了。
很简单,我试图在我的 ggplot2 图形上显示斜率 (y=mx+b) 和一个 R 平方值(使用 RStudio)。
在我的实验中,我测量细菌对不同培养基成分(食物来源)的反应。因此,在一个图中,我有许多面板(或子集),每个面板都有不同的 R^2 和斜率。在这个例子中,我有 6 个不同的子集,所有这些子集都有完全相同的轴。
为此,我创建了一个"主表"
MasterTable <- inner_join(LongRFU, LongOD, by= c("Time.h", "Well", "Conc.nM", "Assay"))
我所有的原始值都采用长表格式(子集称为分析)。
然后我创建了第二个表,它只显示 6 个子集的 R 平方值:
确定每个子集的相关性
Correlation <- ddply(MasterTable, .(Assay), summarise, cor = round(cor(maxRFU, Conc.nM), 3))
Correlation$Rsquared <- (Correlation$cor)^2
Correlation$Rsquared <- round(Correlation$Rsquared,3)
使用此命令,我设法在图形的所有子集中显示 R^2(ggplot 命令波纹管)。
计算回归的斜率和截距:
slope <- dlply(MasterTable, .(Assay), lm, formula = (maxRFU ~ Conc.nM))
m <- lm(MasterTable$Conc.nM ~ MasterTable$maxRFU)
a <- signif(coef(m)[1], digits = 2)
b <- signif(coef(m)[2], digits = 2)
textlab <- paste("y = ",b,"x + ",a, sep="")
print(textlab[1])
创建 ggplot
ggplot(data=MasterTable, aes(x=Conc.nM, y=maxRFU)) +
geom_point(shape = 21, size = 2, colour = "black", fill = "#606060") +
geom_smooth(method = "lm", colour = "#001170", se=TRUE) +
geom_text(data=Correlation,
aes(label=paste("R^2=", " ", Rsquared, sep="")), x=200, y=550) +
annotate("text", x = 200, y = 500, label = textlab, color="black",
size = 5, parse=FALSE) +
geom_errorbar(aes(ymin=maxRFU-sdRFU, ymax=maxRFU+sdRFU), width=.05,
colour="#4b4b4b", linetype = "solid", size = 0.1) +
theme(panel.background = element_rect(fill="white",
linetype = "solid", colour = "black"),
legend.key = element_rect(fill = "white"),
panel.grid.minor = element_blank(), panel.grid.major = element_blank()) +
facet_grid(. ~ Assay) +
labs(title="Calibration curves", y="Max RFU",
x="As(III) concentration (nM)")
StackOverflow还不允许我发布图像...所以我已将其上传到我的盒子帐户:
校准曲线
现在你看到我的问题了吗?
所有子集都显示完全相同的斜率。我似乎找不到解决这个问题的方法,我真的很感激你在这件事上的帮助。
我还有一个额外的问题。这是一个愚蠢的问题,但如果有人知道如何修复它,那就太棒了。我真的很想在下标中显示 R^2 值,就像这个链接所示:ggplot 中的下标
哦,最后一个奖励问题:我似乎无法在误差线上设置"上限"......
再次感谢在这件事上的所有帮助,
马蒂
eipi已经找到了答案。我对我的代码进行了一些修改,它现在可以工作了。
Intercept <- ddply(MasterTable, .(Assay),function(x) coefficients(lm(maxRFU~Conc.nM,x)))
names (Intercept) <- c("Assay", "Intercept", "Slope")
Intercept$Intercept <- round(Intercept$Intercept,0)
Intercept$Slope <- round(Intercept$Slope,3)
ddply 行添加了一个如下所示的表:
Assay Intercept Slope
1 B-GMM 601.2766 0.3758356
2 B-GMM + As(V) 1271.3436 -0.5405553
3 B-GMM + As(V) + PO4 699.9420 0.8970737
4 B-GMM + PO4 720.5684 1.0486098
5 GMM 749.9787 1.9294352
6 GMM + As(V) 768.1253 1.4962517
然后,在 ggplot 中,我添加了以下行:
geom_text(data=Intercept, aes(label=paste("y = ",Slope,"x + ",Intercept, sep="")), x=200, y=500)
就这么简单。
再次感谢!