Hi:
The general process is this:
1. Fit both of your models outside of ggplot2 and extract the coefficients from each.
2. Create a data frame that contains the x and y positions where you want to place the fitted equations along with a third column that contains a text string representation of the fitted line using plotmath code. You can either write a function to generate this string or produce it manually - for only two lines, the manual approach probably takes less time. There should be one line in this data frame for each model.
3. Melt the data such that your two explanatory variables are stacked. This makes it easier to plot lines with different colors or line types, for example.
4. Create a ggplot using the result of the melt operation in step 3 as the input data. Use geom_smooth(method = "lm") to generate the two lines and geom_text() to insert the fitted line equations. You'll need to use parse = TRUE as an argument to geom_text() in order to convert the text string to a mathematical equation.
ggplot2 can generate the fitted lines internally, but it does not "remember" the model coefficients, so you can't use ggplot2 directly to get at the fitted coefficients. There is a way to get them after the fact, but it is not any less complicated than fitting the models beforehand and generating a data frame to create and position the fitted lines by hand.
Since your data are unreadable as is (it is never a good idea to copy/paste data from the R console or a spreadsheet into a message to a text-based list), you'll have to figure out several of these steps on your own, but here are a few lines of code to get you started, taking the name of your data frame as DF:
# Get the model coefficients
coef1 <- coef(lm(S ~ SP, data = DF))
coef2 <- coef(lm(P ~ SP, data = DF))
# Generate the data frame for the equations on your own
# See ?plotmath - you'll need at least hat[y], ~, == and +
# The examples of ?plotmath are helpful - there are also
# examples of how to do this with ggplot2 in the list archives.
# How you want to input the coefficients is up to you.
# For the first call to ggplot() below, assume this data frame has columns
# x, y and eqn for the x-position, y-position and equation as text string,
# respectively.
# Melt the data
library(reshape2)
DFm <- melt(DF, id = "SP") # generates two new vars named variable and value
# Do the plot - puts both lines in the same panel
library(ggplot2)
ggplot(DFm, aes(x = SP, y = value, colour = variable)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, size = 1) +
geom_text(data = coefDF, aes(x = x, y = y, label = eqn), parse = TRUE) +
scale_colour_manual(values = c("darkorange", "blue"))
This will give an inane plot because the range of P is much wider than the range of S, so you're better off faceting the two plots into separate panels (no, ggplot2 does not produce multiple y-scales...by design). In this case, you need to add another column to the data frame containing the fitted equation: variable = factor(c("S", "P")), which represents the y-variable in each row. Then,
# separate plot for each line
ggplot(DFm, aes(x = SP, y = value, colour = variable)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, size = 1) +
geom_text(data = coefDF, aes(x = x, y = y, label = eqn), parse = TRUE) +
scale_colour_manual(values = c("darkorange", "blue")) +
facet_wrap(~ variable, ncol = 1, scales = "free_y")
Getting the data frame for the fitted equations is the hard part: you'll have to decide where to position each equation and you have to remember that ggplot2 expects correct plotmath code encased in a text string. The argument parse = TRUE parses and evaluates the string before plotting the result.
Dennis