I would like to have a function that i could apply to any object that meets a criteria, and have a nice ggplot scatter plot with regression line print.
However, i cannot generalise what i can do at the REPL with code.
so i have this working:
require(ggplot2)
require(xts)
set.seed(1)
dd = xts(cbind(rnorm(10), runif(10)), order.by = Sys.Date() + 1:10)
names(dd) <- c('d1', 'd2')
gp <- ggplot(data = dd,
aes(x = d1, y = d2)) +
geom_point(shape=1) +
geom_smooth(method = lm)
But this fails
PointReg <- function(Xts, a=1, b=2) {
stopifnot(is.xts(Xts),
ncol(Xts) >1)
tempData <- Xts[, c(a,b)]
gPlot <- ggplot(data = tempData,
aes(x = colnames(tempData)[1],
y = colnames(tempData)[2])) +
geom_point(shape=1) +
geom_smooth(method = lm)
gPlot
}
What am i doing wrong?
