2

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

4
  • stackoverflow.com/questions/19699858/… might help Commented Jan 27, 2015 at 3:08
  • 1
    I can't be certain without seeing samples of each data frame you used to make the plot (and you should generally post such samples in the future, so that we can run your code and reproduce the problem you're trying to solve), but it looks like you're using annotate to add the regression equation text to each plot. Instead, use geom_text. As long as the textlab data frame has a column for Assay, geom_text will put the regression equation in each panel that matches each value of Assay in textlab. Commented Jan 27, 2015 at 3:12
  • For completeness, I should have mentioned in my previous comment that the name of the column containing the text labels needs to go inside aes in geom_text (just as in your other call to geom_text). Commented Jan 27, 2015 at 3:25
  • Thank you eipi! You figured it out. How do I make your post the answer? Commented Jan 27, 2015 at 5:36

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Glad you were able to get the plot working the way you wanted. You should go ahead and accept your answer (although you might have to wait a day or two for the site to let you accept an answer to your own question).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.