问题
To my fellow programmers,
I have been searching all over the web for an answer to this question and I am completely stumped.
Quite simply, I am trying to display a slope (y=mx+b) and an R-squared value on my ggplot2 figure (using RStudio).
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.
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).
Then I created a second table which only displays the R-squared values of the 6 subsets:
Determine the correlation of each subset
Correlation <- ddply(MasterTable, .(Assay), summarise, cor = round(cor(maxRFU, Conc.nM), 3))
Correlation$Rsquared <- (Correlation$cor)^2
Correlation$Rsquared <- round(Correlation$Rsquared,3)
With this command, I have managed to display the R^2 in all of my subsets of my figure (ggplot command bellow).
Calculating the regression's slope and intercept:
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])
Creating the 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 is not allowing me to post an image yet... So I have uploaded it to my box account:
Calibration curves
Now you see my problem?
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.
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,
Marty
回答1:
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)
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
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)
As simple as that.
Thanks again!
来源:https://stackoverflow.com/questions/28162486/display-regression-slopes-for-multiple-subsets-in-ggplot2-facet-grid