在ggplot2中显示多个子集的回归斜率(facet_grid) [英] Display regression slopes for multiple subsets in ggplot2 (facet_grid)

查看:143
本文介绍了在ggplot2中显示多个子集的回归斜率(facet_grid)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我的程序员来说,

我一直在网上搜索有关此问题的答案,但我对此深感沮丧.

I have been searching all over the web for an answer to this question and I am completely stumped.

很简单,我试图在ggplot2图形上显示斜率(y = mx + b)和R平方值(使用RStudio).

Quite simply, I am trying to display a slope (y=mx+b) and an R-squared value on my ggplot2 figure (using RStudio).

在我的实验中,我测量了细菌对不同培养基成分(食物来源)的反应.因此,在一个图中,我有很多面板(或子集),每个面板具有不同的R ^ 2和斜率.在此示例中,我有6个不同的子集,它们都具有完全相同的轴.

In my experiment, I measure the response of a bacteria to different media compositions (food sources). Therefore, in one figure, I have many panels (or subsets) and each have a different R^2 and slope. In this example, I have 6 different subsets, all of which have the exact same axes.

为此,我创建了一个"MasterTable"

To do so, I have created a "MasterTable"

MasterTable <- inner_join(LongRFU, LongOD, by= c("Time.h", "Well", "Conc.nM", "Assay"))

所有我的原始值都以长表格式(子集称为分析").

with all of my raw values in a long table format (subsets are called Assay).

然后我创建了第二个表,该表仅显示6个子集的R平方值:

Then I created a second table which only displays the R-squared values of the 6 subsets:

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命令如下).

With this command, I have managed to display the R^2 in all of my subsets of my figure (ggplot command bellow).

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还不允许我发布图像...因此,我已将其上传到我的Box帐户:

StackOverflow is not allowing me to post an image yet... So I have uploaded it to my box account:

校准曲线

现在您看到我的问题了吗?

所有子集都显示完全相同的斜率.我似乎无法找到解决办法,非常感谢您在此问题上的帮助.

All of the subsets display the exact same slope. I cannot seem to find a fix for this and I would really appreciate your help in the matter.

我还有一个奖励问题.这是一个愚蠢的做法,但是如果有人知道如何解决它,那就太了不起了.我真的很想在下标中显示R ^ 2值,如该链接所示: ggplot中的下标

I also have a bonus question. This is a silly one, but would be awesome if someone knows how to fix it. I would really like to display the R^2 value in subscript like it is shown in this link: Subscript in ggplot

哦,最后一个奖励问题:我似乎无法在误差条上加一个上限" ...

Oh and final bonus question: I can't seem to place a "cap" on the error bars...

再次感谢您提供的所有帮助,

Thanks again for all of the help in the matter,

马蒂

推荐答案

eipi找到了答案.我对代码做了一些修改,现在可以正常工作了.

eipi has figured out the answer. I made a few modifications to my code and it is working now.

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行添加了一个如下表:

The ddply line added a table that looked like this:

                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中,添加以下行:

Then, in the ggplot, I added the following line:

geom_text(data=Intercept, aes(label=paste("y = ",Slope,"x + ",Intercept, sep="")), x=200, y=500)

就这么简单.

再次感谢!

这篇关于在ggplot2中显示多个子集的回归斜率(facet_grid)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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