yihui 确实确实,我其实过了会也感觉到自己代码冗余度有点惊人,但是自己抽象能力不够,且对向量化编程了解不深,这一点该怎么提升?
举个栗子
我早些时候 开贴 解决了 “plot 函数的 xlab 和 ylab 的参数设置是否不一样” 的问题,优化之后代码如下
data(anscombe)
form <- paste(paste0("y", seq(4)), paste0("x", seq(4)), sep = "~")
fit <- lapply(form, lm, data = anscombe)
op <- par(mfrow = c(2, 2), mar = 0.1 + c(4, 4, 1, 1), oma = c(0, 0, 2, 0))
for (i in 1:4) {
plot(as.formula(form[i]),
data = anscombe, col = "black",
pch = 19, cex = 1.2,
xlim = c(3, 19), ylim = c(3, 13),
xlab = as.expression(substitute(bold(x[i]), list(i = i))),
ylab = as.expression(substitute(bold(y[i]), list(i = i)))
)
abline(fit[[i]], col = "red", lwd = 2)
text(7, 12, bquote(bold(R)^2 == .(round(summary(fit[[i]])$r.squared, 3))))
}
mtext("Anscombe's 4 Regression data sets", outer = TRUE, cex = 1.2)
par(op)
最近我把它用 ggplot2 重画
library(ggplot2)
library(patchwork)
data(anscombe)
## 想去掉的两行冗余代码
form <- paste(paste0("y", seq(4)), paste0("x", seq(4)), sep = "~")
fit <- lapply(form, lm, data = anscombe)
anscombe_lm <- function(i) {
p <- ggplot(data = anscombe, aes_string(x = paste0("x", i), y = paste0("y", i))) +
geom_point() +
geom_smooth(method = "lm", formula = "y~x", se = FALSE, colour = "red") +
theme_minimal() +
labs(
x = substitute(bold(x[i]), list(i = i)), y = substitute(bold(y[i]), list(i = i)),
title = bquote(bold(R)^2 == .(round(summary(fit[[i]])$r.squared, 3)))
)
p
}
Reduce("+", lapply(1:4, anscombe_lm))
想半天,硬是没想明白怎么去掉上面标注的两行冗余代码(因为它又做了一次线性回归),现在想来可能还有其它冗余的地方,geom_smooth()
做了线性回归这件事,我却不知道怎么提取回归的结果画在图上