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:
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