Hi:
This is just a Q & D reproducible example to give you an idea how the
game works. There are many ways in which the details could be
modified. There are also several past examples of how to do this in
the group archives.
# Generate some fake data
x <- seq(10)
DF <- data.frame(x = x, y1 = 0.5 + 0.8 * x + rnorm(10),
y2 = 0.7 - 0.2 * x + rnorm(10))
# Fit a linear model to each of y1 and y2
m1 <- lm(y1 ~ x, data = DF)
m2 <- lm(y2 ~ x, data = DF)
# Load some packages
library(ggplot2)
library(reshape2)
library(plyr)
# Create a vector of the R^2 values from each model
r2 <- c(summary(m1)[["r.squared"]], summary(m2)[["r.squared"]])
# Melt the original data to stack the two y-variables - this will allow
# you to facet them in the ggplot
DFm <- melt(DF, id = "x")
# Create a data frame for the labels, which has several pieces:
# * a variable for the levels of the faceting variable, so ggplot2 knows
# that different labels go in different panels
# * a vector of (x, y) positions for the labels
# * text string representations of plotmath calls for each of the fitted
# models and R^2s. The parse = TRUE argument in geom_text()
# will convert them into expressions and render them in the plot
#
# I renamed the intercept and slope terms to simplify the text string
# creation and rounded off the input values from the models up front
# to avoid complicating the text string code.
DFeq <- data.frame(variable = c("y1", "y2"),
x = 2.5, y0 = 9.0, y1 = 8.5,
rbind(round(coef(m1), 3), round(coef(m2), 3)),
rsq = round(r2, 3))
names(DFeq)[5:6] <- c("b0", "b1")
DFeq[["modeq"]] <- with(DFeq, paste0("hat(y) == ", b0, " + ", b1, "~X"))
DFeq[["rsquare"]] <- with(DFeq, paste0("R^2 == ", rsq))
# Now generate the plot. Note the use of DFeq as the input
# data frame for geom_text() and the use of parse = TRUE to convert
# the text strings into plotmath expressions. Use geom_smooth() to
# plot the fitted least squares line in each panel with method = "lm".
ggplot(data = DFm, aes(x = x, y = value)) +
geom_point() +
geom_smooth(method = "lm", size = 1, se = FALSE) +
geom_text(data = DFeq, aes(x = x, y = y0, label = modeq),
parse = TRUE, hjust = 0) +
geom_text(data = DFeq, aes(x = x, y = y1, label = rsquare),
parse = TRUE, hjust = 0) +
facet_wrap(~ variable)
The formula for the model with the negative slope is ugly and could be
easily fixed with an if statement to generate the + or - sign that
could then be passed to paste0, but I'll leave that as an exercise.
Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
>
https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email
ggp...@googlegroups.com
> To unsubscribe: email
ggplot2+u...@googlegroups.com
> More options:
http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
ggplot2+u...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.