【发布时间】:2013-12-11 05:33:43
【问题描述】:
我想要一个可以应用于任何符合条件的对象的函数,并有一个漂亮的ggplot 散点图和回归线打印。
但是,我无法用代码概括我在REPL 上可以做什么。
所以我有这个工作:
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)
但这失败了
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
}
我做错了什么?
【问题讨论】: